<commit_before># encoding: utf-8
require 'json'
require_relative '../../../models/synchronization/member'
require_relative '../../../models/synchronization/collection'

class Api::Json::SynchronizationsController < Api::ApplicationController
  include CartoDB

  ssl_required :index, :show, :create, :update, :destroy

  def index
    collection = Synchronization::Collection.new.fetch
    representation = collection.map(&:to_hash)
    response  = {
      synchronizations: representation,
      total_entries:    collection.total_entries
    }
    render_jsonp(response)
  end

  def create
    member = Synchronization::Member.new(
      { name:     params[:table_name],
        user_id:  current_user.id
      }.merge(payload)
    ).store
    collection      = Synchronization::Collection.new.fetch
    collection.add(member)
    collection.store

    options = { 
      user_id:            current_user.id,
      table_name:         params[:table_name].presence,
      data_source:        params[:url],
      synchronization_id: member.id
    }
      
    data_import = DataImport.create(options)
    ::Resque.enqueue(::Resque::ImporterJobs, job_id: data_import.id)

    response = {
      data_import: { 
        endpoint: "/api/v1/imports",
        id:       data_import.id
      }
    }.merge(member.to_hash)
    render_jsonp(response)
  rescue CartoDB::InvalidMember => exception
    render_jsonp({ errors: member.full_errors }, 400)
    puts exception.to_s
    puts exception.backtrace
  end

  def show
    member = Synchronization::Member.new(id: params[:id]).fetch

    return(head 401) unless member.authorize?(current_user)
    render_jsonp(member)
  rescue KeyError => exception
    puts exception.to_s
    puts exception.backtrace<commit_msg>Return data_import id as queue_item_id in response to POST /api/v1/synchronizations<commit_after># encoding: utf-8
require 'json'
require_relative '../../../models/synchronization/member'
require_relative '../../../models/synchronization/collection'

class Api::Json::SynchronizationsController < Api::ApplicationController
  include CartoDB

  ssl_required :index, :show, :create, :update, :destroy

  def index
    collection = Synchronization::Collection.new.fetch
    representation = collection.map(&:to_hash)
    response  = {
      synchronizations: representation,
      total_entries:    collection.total_entries
    }
    render_jsonp(response)
  end

  def create
    member = Synchronization::Member.new(
      { name:     params[:table_name],
        user_id:  current_user.id
      }.merge(payload)
    ).store
    collection      = Synchronization::Collection.new.fetch
    collection.add(member)
    collection.store

    options = { 
      user_id:            current_user.id,
      table_name:         params[:table_name].presence,
      data_source:        params[:url],
      synchronization_id: member.id
    }
      
    data_import = DataImport.create(options)
    ::Resque.enqueue(::Resque::ImporterJobs, job_id: data_import.id)

    response = {
      data_import: { 
        endpoint:       "/api/v1/imports",
        item_queue_id:  data_import.id
      }
    }.merge(member.to_hash)
    render_jsonp(response)
  rescue CartoDB::InvalidMember => exception
    render_jsonp({ errors: member.full_errors }, 400)
    puts exception.to_s
    puts exception.backtrace
  end

  def show
    member = Synchronization::Member.new(id: params[:id]).fetch

    return(head 401) unless member.authorize?(current_user)
    render_jsonp(member)
  rescue KeyError => exception
    puts exception.to_s
    puts exception.backtrace
{'fraction_non_alphanumeric': 0.1063888888888889, 'fraction_numerical': 0.004722222222222222, 'mean_word_length': 3.046067415730337, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3310996', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1078, 'n_words': 204}