Class: SalesforceBulk::Api

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

Constant Summary collapse

@@SALESFORCE_API_VERSION =
'23.0'

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Api

Returns a new instance of Api.



15
16
17
18
# File 'lib/salesforce_bulk_oauth2.rb', line 15

def initialize(options)
  client = databasedotcom_client(options[:token], options[:instance_url])
  @connection = SalesforceBulk::Connection.new(@@SALESFORCE_API_VERSION,client)
end

Instance Method Details

#create(sobject, records) ⇒ Object



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

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

#databasedotcom_client(token, instance_url) ⇒ Object



20
21
22
23
24
# File 'lib/salesforce_bulk_oauth2.rb', line 20

def databasedotcom_client(token, instance_url)
  client = Databasedotcom::Client.new("#{Rails.root}/config/databasedotcom.yml")
  client.authenticate :token => token, :instance_url => instance_url
  client
end

#delete(sobject, records) ⇒ Object



38
39
40
# File 'lib/salesforce_bulk_oauth2.rb', line 38

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

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

private



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
73
74
75
# File 'lib/salesforce_bulk_oauth2.rb', line 48

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'
    return job.get_batch_result()
  else
    return ["error"]
  end

end

#query(sobject, query) ⇒ Object



42
43
44
# File 'lib/salesforce_bulk_oauth2.rb', line 42

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

#update(sobject, records) ⇒ Object



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

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

#upsert(sobject, records, external_field) ⇒ Object



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

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