Class: SalesforceBulkApi::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/salesforce_bulk_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, salesforce_api_version = "46.0") ⇒ Api

Returns a new instance of Api.



16
17
18
19
# File 'lib/salesforce_bulk_api.rb', line 16

def initialize(client, salesforce_api_version = "46.0")
  @connection = SalesforceBulkApi::Connection.new(salesforce_api_version, client)
  @listeners = { job_created: [] }
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



14
15
16
# File 'lib/salesforce_bulk_api.rb', line 14

def connection
  @connection
end

Instance Method Details

#countersObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/salesforce_bulk_api.rb', line 41

def counters
  {
    http_get: @connection.counters[:get],
    http_post: @connection.counters[:post],
    upsert: get_counters[:upsert],
    update: get_counters[:update],
    create: get_counters[:create],
    delete: get_counters[:delete],
    query: get_counters[:query]
  }
end

#create(sobject, records, get_response = false, send_nulls = false, batch_size = 10000, timeout = 1500) ⇒ Object



29
30
31
# File 'lib/salesforce_bulk_api.rb', line 29

def create(sobject, records, get_response = false, send_nulls = false, batch_size = 10000, timeout = 1500)
  do_operation('insert', sobject, records, nil, get_response, timeout, batch_size, send_nulls)
end

#delete(sobject, records, get_response = false, batch_size = 10000, timeout = 1500) ⇒ Object



33
34
35
# File 'lib/salesforce_bulk_api.rb', line 33

def delete(sobject, records, get_response = false, batch_size = 10000, timeout = 1500)
  do_operation('delete', sobject, records, nil, get_response, timeout, batch_size)
end

#do_operation(operation, sobject, records, external_field, get_response, timeout, batch_size, send_nulls = false, no_null_list = []) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/salesforce_bulk_api.rb', line 66

def do_operation(operation, sobject, records, external_field, get_response, timeout, batch_size, send_nulls = false, no_null_list = [])
  count operation.to_sym

  job = SalesforceBulkApi::Job.new(
    operation: operation,
    sobject: sobject,
    records: records,
    external_field: external_field,
    connection: @connection
  )

  job.create_job(batch_size, send_nulls, no_null_list)
  @listeners[:job_created].each {|callback| callback.call(job)}
  operation == "query" ? job.add_query() : job.add_batches()
  response = job.close_job
  response.merge!({'batches' => job.get_job_result(get_response, timeout)}) if get_response == true
  response
end

#job_from_id(job_id) ⇒ Object



62
63
64
# File 'lib/salesforce_bulk_api.rb', line 62

def job_from_id(job_id)
  SalesforceBulkApi::Job.new(job_id: job_id, connection: @connection)
end

#on_job_created(&block) ⇒ Object

Allows you to attach a listener that accepts the created job (which has a useful #job_id field). This is useful for recording a job ID persistently before you begin batch work (i.e. start modifying the salesforce database), so if the load process you are writing needs to recover, it can be aware of previous jobs it started and wait for them to finish.



58
59
60
# File 'lib/salesforce_bulk_api.rb', line 58

def on_job_created(&block)
  @listeners[:job_created] << block
end

#query(sobject, query, batch_size = 10000, timeout = 1500) ⇒ Object



37
38
39
# File 'lib/salesforce_bulk_api.rb', line 37

def query(sobject, query, batch_size = 10000, timeout = 1500)
  do_operation('query', sobject, query, nil, true, timeout, batch_size)
end

#update(sobject, records, get_response = false, send_nulls = false, no_null_list = [], batch_size = 10000, timeout = 1500) ⇒ Object



25
26
27
# File 'lib/salesforce_bulk_api.rb', line 25

def update(sobject, records, get_response = false, send_nulls = false, no_null_list = [], batch_size = 10000, timeout = 1500)
  do_operation('update', sobject, records, nil, get_response, timeout, batch_size, send_nulls, no_null_list)
end

#upsert(sobject, records, external_field, get_response = false, send_nulls = false, no_null_list = [], batch_size = 10000, timeout = 1500) ⇒ Object



21
22
23
# File 'lib/salesforce_bulk_api.rb', line 21

def upsert(sobject, records, external_field, get_response = false, send_nulls = false, no_null_list = [], batch_size = 10000, timeout = 1500)
  do_operation('upsert', sobject, records, external_field, get_response, timeout, batch_size, send_nulls, no_null_list)
end