Class: SalesforceBulk::Job
- Inherits:
-
Object
- Object
- SalesforceBulk::Job
- Defined in:
- lib/salesforce_bulk/job.rb
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
Returns the value of attribute result.
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.
- #parse_results(response) ⇒ Object
Constructor Details
#initialize(operation, sobject, records, external_field, connection) ⇒ Job
Returns a new instance of Job.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/salesforce_bulk/job.rb', line 7 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" ?>' # @result = {"errors" => [], "success" => nil, "records" => [], "raw" => nil, "message" => 'The job has been queued.'} @result = JobResult.new end |
Instance Attribute Details
#result ⇒ Object (readonly)
Returns the value of attribute result.
5 6 7 |
# File 'lib/salesforce_bulk/job.rb', line 5 def result @result end |
Instance Method Details
#add_batch ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/salesforce_bulk/job.rb', line 64 def add_batch() keys = @@records.first.keys output_csv = keys.to_csv @@records.each do |r| fields = Array.new keys.each do |k| fields.push(r[k]) end row_csv = fields.to_csv output_csv += row_csv end path = "job/#{@@job_id}/batch/" headers = Hash["Content-Type" => "text/csv; charset=UTF-8"] response = @@connection.post_xml(nil, path, output_csv, headers) response_parsed = XmlSimple.xml_in(response) @@batch_id = response_parsed['id'][0] end |
#add_query ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/salesforce_bulk/job.rb', line 54 def add_query path = "job/#{@@job_id}/batch/" headers = Hash["Content-Type" => "text/csv; 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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/salesforce_bulk/job.rb', line 88 def check_batch_status() path = "job/#{@@job_id}/batch/#{@@batch_id}" headers = Hash.new response = @@connection.get_request(nil, path, headers) response_parsed = XmlSimple.xml_in(response) 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
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/salesforce_bulk/job.rb', line 40 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
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/salesforce_bulk/job.rb', line 21 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>CSV</contentType>" xml += "</jobInfo>" path = "job" headers = Hash['Content-Type' => 'application/xml; charset=utf-8'] response = @@connection.post_xml(nil, path, xml, headers) response_parsed = @@connection.parse_response response @@job_id = response_parsed['id'][0] end |
#get_batch_result ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/salesforce_bulk/job.rb', line 105 def get_batch_result() path = "job/#{@@job_id}/batch/#{@@batch_id}/result" headers = Hash["Content-Type" => "text/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" => "text/xml; charset=UTF-8"] response = @@connection.get_request(nil, path, headers) end parse_results response response = response.lines.to_a[1..-1].join # csvRows = CSV.parse(response, :headers => true) end |
#parse_results(response) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/salesforce_bulk/job.rb', line 129 def parse_results response @result.success = true @result.raw = response.lines.to_a[1..-1].join csvRows = CSV.parse(response, :headers => true) csvRows.each_with_index do |row, index| if @@operation != "query" row["Created"] = row["Created"] == "true" ? true : false row["Success"] = row["Success"] == "true" ? true : false end @result.records.push row if row["Success"] == false @result.success = false @result.errors.push({"#{index}" => row["Error"]}) if row["Error"] end end @result. = "The job has been closed." end |