class Executrix::Api

Constants

SALESFORCE_API_VERSION

Public Class Methods

new(username, password, sandbox = false, api_version = SALESFORCE_API_VERSION) click to toggle source
# File lib/executrix.rb, line 12
def initialize(username, password, sandbox = false, api_version = SALESFORCE_API_VERSION)
  @connection = Executrix::Connection.connect(
    username,
    password,
    api_version,
    sandbox)
end

Public Instance Methods

delete(sobject, records) click to toggle source
# File lib/executrix.rb, line 36
def delete(sobject, records)
  start_job('delete', sobject, records)
end
insert(sobject, records) click to toggle source
# File lib/executrix.rb, line 32
def insert(sobject, records)
  start_job('insert', sobject, records)
end
org_id() click to toggle source
# File lib/executrix.rb, line 20
def org_id
  @connection.org_id
end
query(sobject, query) click to toggle source
# File lib/executrix.rb, line 40
def query(sobject, query)
  job_id = @connection.create_job(
    'query',
    sobject,
    'CSV',
    nil)
  batch_id = @connection.add_query(job_id, query)
  @connection.close_job job_id
  batch_reference = Executrix::Batch.new @connection, job_id, batch_id
  batch_reference.final_status
end
update(sobject, records) click to toggle source
# File lib/executrix.rb, line 28
def update(sobject, records)
  start_job('update', sobject, records)
end
upsert(sobject, records, external_field) click to toggle source
# File lib/executrix.rb, line 24
def upsert(sobject, records, external_field)
  start_job('upsert', sobject, records, external_field)
end

Private Instance Methods

start_job(operation, sobject, records, external_field=nil) click to toggle source
# File lib/executrix.rb, line 53
def start_job(operation, sobject, records, external_field=nil)
  attachment_keys = Executrix::Helper.attachment_keys(records)

  content_type = 'CSV'
  zip_filename = nil
  request_filename = nil
  batch_id = -1
  if not attachment_keys.empty?
    tmp_filename = Dir::Tmpname.make_tmpname('bulk_upload', '.zip')
    zip_filename = "#{Dir.tmpdir}/#{tmp_filename}"
    Zip::File.open(zip_filename, Zip::File::CREATE) do |zipfile|
      Executrix::Helper.transform_values!(records, attachment_keys) do |path|
        relative_path = Executrix::Helper.absolute_to_relative_path(path, '')
        zipfile.add(relative_path, path) rescue Zip::ZipEntryExistsError
      end
      tmp_filename = Dir::Tmpname.make_tmpname('request', '.txt')
      request_filename = "#{Dir.tmpdir}/#{tmp_filename}"
      File.open(request_filename, 'w') do |file|
        file.write(Executrix::Helper.records_to_csv(records))
      end
      zipfile.add('request.txt', request_filename)
    end

    content_type = 'ZIP_CSV'
  end

  job_id = @connection.create_job(
    operation,
    sobject,
    content_type,
    external_field)
  if zip_filename
    batch_id = @connection.add_file_upload_batch job_id, zip_filename
    [zip_filename, request_filename].each do |file|
      File.delete(file) if file
    end
  else
    batch_id = @connection.add_batch job_id, records
  end

  @connection.close_job job_id
  Executrix::Batch.new @connection, job_id, batch_id
end