Class: SalesforceBulk::Api

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

Overview

Your code goes hereā€¦

Constant Summary collapse

@@SALESFORCE_API_VERSION =
'23.0'

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Api

Returns a new instance of Api.



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

def initialize(username, password)
  @connection = SalesforceBulk::Connection.new(username, password, @@SALESFORCE_API_VERSION)
end

Instance Method Details

#create(sobject, records) ⇒ Object



26
27
28
# File 'lib/salesforce_bulk.rb', line 26

def create(sobject, records)
  self.do_operation('insert', sobject, records, nil)
end

#delete(sobject, records) ⇒ Object



30
31
32
# File 'lib/salesforce_bulk.rb', line 30

def delete(sobject, records)
  self.do_operation('delete', sobject, records, nil)
end

#do_operation(operation, sobject, records, external_field) ⇒ Object

private



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/salesforce_bulk.rb', line 40

def do_operation(operation, sobject, records, external_field)
  job = SalesforceBulk::Job.new(operation, sobject, records, external_field, @connection)

  # TODO: put this in one function
  job_id = job.create_job()
  if(operation == "query")
    batch_id = job.add_query()
  else
    batch_id = job.add_batch()
  end
  job.close_job()

  while true
    state = job.check_batch_status()
    #puts "\nstate is #{state}\n"
    if state != "Queued" && state != "InProgress"
      break
    end
    sleep(2) # wait x seconds and check again
  end

  if state == 'Completed'
    job.get_batch_result()
  else
    return "error"
  end

end

#query(sobject, query) ⇒ Object



34
35
36
# File 'lib/salesforce_bulk.rb', line 34

def query(sobject, query)
  self.do_operation('query', sobject, query, nil)
end

#update(sobject, records) ⇒ Object



22
23
24
# File 'lib/salesforce_bulk.rb', line 22

def update(sobject, records)
  self.do_operation('update', sobject, records, nil)
end

#upsert(sobject, records, external_field) ⇒ Object



18
19
20
# File 'lib/salesforce_bulk.rb', line 18

def upsert(sobject, records, external_field)
  self.do_operation('upsert', sobject, records, external_field)
end