Class: SalesforceBulk::Job
- Inherits:
-
Object
- Object
- SalesforceBulk::Job
- Defined in:
- lib/salesforce_bulk/job.rb
Instance Method Summary collapse
- #add_batch ⇒ Object
- #add_query ⇒ Object
- #check_batch_status ⇒ Object
- #close_job ⇒ Object
- #create_job ⇒ Object
- #get_batch_result ⇒ Object
-
#initialize(operation, sobject, records, external_field, connection) ⇒ Job
constructor
A new instance of Job.
Constructor Details
#initialize(operation, sobject, records, external_field, connection) ⇒ Job
Returns a new instance of Job.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/salesforce_bulk/job.rb', line 5 def initialize(operation, sobject, records, external_field, connection) @@operation = operation @@sobject = sobject @@external_field = external_field @@records = records @@connection = connection @@XML_HEADER = '<?xml version="1.0" encoding="utf-8" ?>' end |
Instance Method Details
#add_batch ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/salesforce_bulk/job.rb', line 56 def add_batch() keys = @@records.inject({}) {|h,pairs| pairs.each {|k,v| (h[k] ||= []) << v}; h}.keys ## Target here headers = keys @@records_dup=@@records.clone super_records=[] (@@records_dup.size/10000).times do super_records<<@@records_dup.pop(10000) end super_records<<@@records_dup super_records.each do|batch| xml = "#{@@XML_HEADER}<sObjects xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\">" batch.each do |r| xml += "<sObject>" keys.each do |k| xml += "<#{k}>#{r[k]}</#{k}>" end xml += "</sObject>" end xml += "</sObjects>" path = "job/#{@@job_id}/batch/" headers = Hash["Content-Type" => "application/xml; charset=UTF-8"] response = @@connection.post_xml(nil, path, xml, headers) response_parsed = XmlSimple.xml_in(response) @@batch_id = response_parsed['id'][0] puts @@batch_id end end |
#add_query ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/salesforce_bulk/job.rb', line 48 def add_query path = "job/#{@@job_id}/batch/" headers = Hash["Content-Type" => "application/xml; charset=UTF-8"] response = @@connection.post_xml(nil, path, @@records, headers) response_parsed = XmlSimple.xml_in(response) @@batch_id = response_parsed['id'][0] end |
#check_batch_status ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/salesforce_bulk/job.rb', line 87 def check_batch_status() headers = Hash.new response = @@connection.get_request(nil, path, headers) response_parsed = XmlSimple.xml_in(response) puts response_parsed begin #puts "check: #{response_parsed.inspect}\n" response_parsed['state'][0] rescue Exception => e #puts "check: #{response_parsed.inspect}\n" nil end end |
#close_job ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/salesforce_bulk/job.rb', line 34 def close_job() xml = "#{@@XML_HEADER}<jobInfo xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\">" xml += "<state>Closed</state>" xml += "</jobInfo>" path = "job/#{@@job_id}" headers = Hash['Content-Type' => 'application/xml; charset=utf-8'] response = @@connection.post_xml(nil, path, xml, headers) response_parsed = XmlSimple.xml_in(response) #job_id = response_parsed['id'][0] end |
#create_job ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/salesforce_bulk/job.rb', line 16 def create_job() xml = "#{@@XML_HEADER}<jobInfo xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\">" xml += "<operation>#{@@operation}</operation>" xml += "<object>#{@@sobject}</object>" if !@@external_field.nil? # This only happens on upsert xml += "<externalIdFieldName>#{@@external_field}</externalIdFieldName>" end xml += "<contentType>XML</contentType>" xml += "</jobInfo>" path = "job" headers = Hash['Content-Type' => 'application/xml; charset=utf-8'] response = @@connection.post_xml(nil, path, xml, headers) response_parsed = XmlSimple.xml_in(response) @@job_id = response_parsed['id'][0] end |
#get_batch_result ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/salesforce_bulk/job.rb', line 102 def get_batch_result() path = "job/#{@@job_id}/batch/#{@@batch_id}/result" headers = Hash["Content-Type" => "application/xml; charset=UTF-8"] response = @@connection.get_request(nil, path, headers) if(@@operation == "query") # The query op requires us to do another request to get the results response_parsed = XmlSimple.xml_in(response) result_id = response_parsed["result"][0] path = "job/#{@@job_id}/batch/#{@@batch_id}/result/#{result_id}" headers = Hash.new headers = Hash["Content-Type" => "application/xml; charset=UTF-8"] response = @@connection.get_request(nil, path, headers) end titles = [] doc = REXML::Document.new(response) doc.elements.each('results/result/success') do |ele| titles << ele.text end return titles end |