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 =
'24.0'

Instance Method Summary collapse

Constructor Details

#initialize(username, password, in_sandbox = false) ⇒ Api

Returns a new instance of Api.



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

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

Instance Method Details

#create(sobject, records, wait = false) ⇒ Object



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

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

#delete(sobject, records, wait = false) ⇒ Object



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

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

#do_operation(operation, sobject, records, external_field, wait = false) ⇒ Object



38
39
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
68
69
70
71
72
# File 'lib/salesforce_bulk.rb', line 38

def do_operation(operation, sobject, records, external_field, wait=false)
  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()

  if wait or operation == 'query'
    while true
      state = job.check_batch_status()
      if state != "Queued" && state != "InProgress"
        break
      end
      sleep(2) # wait x seconds and check again
    end
    
    if state == 'Completed'
      job.get_batch_result()
      job
    else
      job.result.message = "There is an error in your job. The response returned a state of #{state}. Please check your query/parameters and try again."
      job.result.success = false
      return job

    end
  else
    return job
  end

end

#parse_batch_result(result) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/salesforce_bulk.rb', line 74

def parse_batch_result result
  begin
    CSV.parse(result, :headers => true)
  rescue
    result
  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, wait = false) ⇒ Object



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

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

#upsert(sobject, records, external_field, wait = false) ⇒ Object



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

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