<commit_before>$:.unshift File.join('..', File.dirname(__FILE__), 'lib')

require 'httmultiparty'
require 'rexml/document'
require 'net/http/post/multipart'
require 'net/https'
require 'mime/types'
require 'ruby_zoho'
require 'yaml'
require 'api_utils'
require 'zoho_api_field_utils'
require 'zoho_api_finders'

module ZohoApi

  include ApiUtils

  class Crm

    include HTTMultiParty
    include ZohoApiFieldUtils
    include ZohoApiFinders

    debug_output $stderr

    attr_reader :auth_token, :module_fields

    def initialize(auth_token, modules, ignore_fields, fields = nil)
      @auth_token = auth_token
      @modules = %w(Accounts Contacts Events Potentials Tasks Users).concat(modules).uniq
      @module_fields = fields.nil? ? reflect_module_fields : fields
      @ignore_fields = ignore_fields
    end

    def add_record(module_name, fields_values_hash)
      x = REXML::Document.new
      element = x.add_element module_name
      row = element.add_element 'row', {'no' => '1'}
      fields_values_hash.each_pair { |k, v| add_field(row, k, v, module_name) }
      r = self.class.post(create_url(module_name, 'insertRecords'),
                          :query => {:newFormat => 1, :authtoken => @auth_token,
                                     :scope => 'crmapi', :xmlData => x, :wfTrigger => 'true'},
                          :headers => {'Content-length' => '0'})
      check_for_errors(r)
      x_r = REXML::Document.new(r.body).elements.to_a('//recorddetail')
      to_hash(x_r, module_name)[0]
    end

    def bulk_insert(module_name, records)
      x = REXML::Document.new
      element = x.add_element module_name

      records.each_with_index do |fields_values_hash, index|
        row = element.add_element 'row', {'no' => "#{index + 1}"}
        fields_values_hash.each_pair { |k, v| add_field(row, k, v, module_name) }
      end

      r = self.class.post(create_url(module_name, 'insertRecords'),
                          :query => {:newFormat => 1, :authtoken => @auth_token,
                                     :scope => 'crmapi', :xmlData => x, :wfTrigger => 'true'},
                          :headers => {'Content-length' => '0'})
      check_for_errors(r)
      x_r = REXML::Document.new(r.body).elements.to_a('//recorddetail')
      to_hash(x_r, module_name)[0]
    end

    def attach_file(module_name, record_id, file_path, file_name)
      mime_type = (MIME::Types.type_for(file_path)[0] || MIME::Types['application/octet-stream'][0])
      url_path = create_url(module_name, "uploadFile?authtoken=#{@auth_token}&scope=crmapi&id=#{record_id}")
      url = URI.parse(create_url(module_name, url_path))
      io = UploadIO.new(file_path, mime_type, file_name)<commit_msg>Update zoho_api.rb<commit_after>$:.unshift File.join('..', File.dirname(__FILE__), 'lib')

require 'httmultiparty'
require 'rexml/document'
require 'net/http/post/multipart'
require 'net/https'
require 'mime/types'
require 'ruby_zoho'
require 'yaml'
require 'api_utils'
require 'zoho_api_field_utils'
require 'zoho_api_finders'

module ZohoApi

  include ApiUtils

  class Crm

    include HTTMultiParty
    include ZohoApiFieldUtils
    include ZohoApiFinders

    debug_output $stderr

    attr_reader :auth_token, :module_fields

    def initialize(auth_token, modules, ignore_fields, fields = nil)
      @auth_token = auth_token
      @modules = %w(Accounts Contacts Events Potentials Tasks Users).concat(modules).uniq
      @module_fields = fields.nil? ? reflect_module_fields : fields
      @ignore_fields = ignore_fields
    end

    def add_record(module_name, fields_values_hash)
      x = REXML::Document.new
      element = x.add_element module_name
      row = element.add_element 'row', {'no' => '1'}
      fields_values_hash.each_pair { |k, v| add_field(row, k, v, module_name) }
      r = self.class.post(create_url(module_name, 'insertRecords'),
                          :query => {:newFormat => 1, :authtoken => @auth_token, :version => 2,
                                     :scope => 'crmapi', :xmlData => x, :wfTrigger => 'true'},
                          :headers => {'Content-length' => '0'})
      check_for_errors(r)
      x_r = REXML::Document.new(r.body).elements.to_a('//recorddetail')
      to_hash(x_r, module_name)[0]
    end

    def bulk_insert(module_name, records)
      x = REXML::Document.new
      element = x.add_element module_name

      records.each_with_index do |fields_values_hash, index|
        row = element.add_element 'row', {'no' => "#{index + 1}"}
        fields_values_hash.each_pair { |k, v| add_field(row, k, v, module_name) }
      end

      r = self.class.post(create_url(module_name, 'insertRecords'),
                          :query => {:newFormat => 1, :authtoken => @auth_token,
                                     :scope => 'crmapi', :xmlData => x, :wfTrigger => 'true'},
                          :headers => {'Content-length' => '0'})
      check_for_errors(r)
      x_r = REXML::Document.new(r.body).elements.to_a('//recorddetail')
      to_hash(x_r, module_name)[0]
    end

    def attach_file(module_name, record_id, file_path, file_name)
      mime_type = (MIME::Types.type_for(file_path)[0] || MIME::Types['application/octet-stream'][0])
      url_path = create_url(module_name, "uploadFile?authtoken=#{@auth_token}&scope=crmapi&id=#{record_id}")
      url = URI.parse(create_url(module_name, url_path))
      io = UploadIO.new(file_path, mime_type, file_name)
{'fraction_non_alphanumeric': 0.13229357798165137, 'fraction_numerical': 0.003853211009174312, 'mean_word_length': 3.0739910313901344, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3986815', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1796, 'n_words': 348}