Class: SalesforceBulk::Job

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

Instance Method Summary collapse

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_batchObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/salesforce_bulk/job.rb', line 59

def add_batch()
  keys = @@records.reduce({}) {|h,pairs| pairs.each {|k,v| (h[k] ||= []) << v}; h}.keys
  headers = keys.to_csv
  
  output_csv = headers

  @@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_queryObject



49
50
51
52
53
54
55
56
57
# File 'lib/salesforce_bulk/job.rb', line 49

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_statusObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/salesforce_bulk/job.rb', line 84

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_jobObject



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/salesforce_bulk/job.rb', line 35

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_jobObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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>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 = XmlSimple.xml_in(response)    

  @@job_id = response_parsed['id'][0]
end

#get_batch_resultObject



101
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 101

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"]
    #puts "path is: #{path}\n"
    
    response = @@connection.get_request(nil, path, headers)
    #puts "\n\nres2: #{response.inspect}\n\n"

  end

  response = response.lines.to_a[1..-1].join
  csvRows = CSV.parse(response)
end