Class: Bulkforce::Http::Request

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_method, host, path, body, headers) ⇒ Request

Returns a new instance of Request.



123
124
125
126
127
128
129
# File 'lib/bulkforce/http.rb', line 123

def initialize http_method, host, path, body, headers
  @http_method  = http_method
  @host         = host
  @path         = path
  @body         = body
  @headers      = headers
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



119
120
121
# File 'lib/bulkforce/http.rb', line 119

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



120
121
122
# File 'lib/bulkforce/http.rb', line 120

def headers
  @headers
end

#hostObject (readonly)

Returns the value of attribute host.



118
119
120
# File 'lib/bulkforce/http.rb', line 118

def host
  @host
end

#http_methodObject (readonly)

Returns the value of attribute http_method.



121
122
123
# File 'lib/bulkforce/http.rb', line 121

def http_method
  @http_method
end

#pathObject (readonly)

Returns the value of attribute path.



117
118
119
# File 'lib/bulkforce/http.rb', line 117

def path
  @path
end

Class Method Details

.add_batch(instance, session_id, job_id, data, api_version) ⇒ Object



217
218
219
220
221
222
223
224
225
# File 'lib/bulkforce/http.rb', line 217

def self.add_batch instance, session_id, job_id, data, api_version
  headers = {"Content-Type" => "text/csv; charset=UTF-8", "X-SFDC-Session" => session_id}
  Http::Request.new(
    :post,
    instance_host(instance),
    "/services/async/#{api_version}/job/#{job_id}/batch",
    data,
    headers)
end

.close_job(instance, session_id, job_id, api_version) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/bulkforce/http.rb', line 200

def self.close_job instance, session_id, job_id, api_version
  body = %Q{<?xml version="1.0" encoding="utf-8" ?>
    <jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
      <state>Closed</state>
    </jobInfo>
  }
  headers = {
    "Content-Type" => "application/xml; charset=utf-8",
    "X-SFDC-Session" => session_id}
  Http::Request.new(
    :post,
    instance_host(instance),
    "/services/async/#{api_version}/job/#{job_id}",
    body,
    headers)
end

.create_job(instance, session_id, operation, sobject, content_type, api_version, external_field = nil) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/bulkforce/http.rb', line 178

def self.create_job instance, session_id, operation, sobject, content_type, api_version, external_field = nil
  external_field_line = external_field ?
    "<externalIdFieldName>#{external_field}</externalIdFieldName>" : nil
  body = %Q{<?xml version="1.0" encoding="utf-8" ?>
    <jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">
      <operation>#{operation}</operation>
      <object>#{sobject}</object>
      #{external_field_line}
      <contentType>#{content_type}</contentType>
    </jobInfo>
  }
  headers = {
    "Content-Type" => "application/xml; charset=utf-8",
    "X-SFDC-Session" => session_id}
  Http::Request.new(
    :post,
    instance_host(instance),
    "/services/async/#{api_version}/job",
    body,
    headers)
end

.instance_host(instance) ⇒ Object



267
268
269
# File 'lib/bulkforce/http.rb', line 267

def self.instance_host instance
  "#{instance}.salesforce.com"
end

.login(host, username, password, api_version) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/bulkforce/http.rb', line 131

def self. host, username, password, api_version
  body =  %Q{<?xml version="1.0" encoding="utf-8" ?>
  <env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Body>
      <n1:login xmlns:n1="urn:partner.soap.sforce.com">
        <n1:username>#{username}</n1:username>
        <n1:password>#{password}</n1:password>
      </n1:login>
    </env:Body>
  </env:Envelope>}
  headers = {
    "Content-Type" => "text/xml; charset=utf-8",
    "SOAPAction" => "login"
  }
  Http::Request.new(
    :post,
    host,
    "/services/Soap/u/#{api_version}",
    body,
    headers)
end

.oauth_login(host, client_id, client_secret, refresh_token) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/bulkforce/http.rb', line 155

def self.(host, client_id, client_secret, refresh_token)
  headers = {
    "Content-Type" => "application/x-www-form-urlencoded",
    "Accept" => "application/xml",
  }

  body = {
    grant_type: "refresh_token",
    client_id: client_id,
    client_secret: client_secret,
    refresh_token: refresh_token
  }.inject("") do |string, (k,v)|
    string += "#{k}=#{v}&"
  end

  Http::Request.new(
    :post,
    host,
    "/services/oauth2/token",
    body,
    headers)
end

.query_batch(instance, session_id, job_id, batch_id, api_version) ⇒ Object



227
228
229
230
231
232
233
234
235
# File 'lib/bulkforce/http.rb', line 227

def self.query_batch instance, session_id, job_id, batch_id, api_version
  headers = {"X-SFDC-Session" => session_id}
  Http::Request.new(
    :get,
    instance_host(instance),
    "/services/async/#{api_version}/job/#{job_id}/batch/#{batch_id}",
    nil,
    headers)
end

.query_batch_result_data(instance, session_id, job_id, batch_id, result_id, api_version) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/bulkforce/http.rb', line 249

def self.query_batch_result_data(instance,
  session_id,
  job_id,
  batch_id,
  result_id,
  api_version)
  headers = {
    "Content-Type" => "text/csv; charset=UTF-8",
    "X-SFDC-Session" => session_id}
  Http::Request.new(
    :get,
    instance_host(instance),
    "/services/async/#{api_version}" \
      "/job/#{job_id}/batch/#{batch_id}/result/#{result_id}",
    nil,
    headers)
end

.query_batch_result_id(instance, session_id, job_id, batch_id, api_version) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
# File 'lib/bulkforce/http.rb', line 237

def self.query_batch_result_id instance, session_id, job_id, batch_id, api_version
  headers = {
    "Content-Type" => "application/xml; charset=utf-8",
    "X-SFDC-Session" => session_id}
  Http::Request.new(
    :get,
    instance_host(instance),
    "/services/async/#{api_version}/job/#{job_id}/batch/#{batch_id}/result",
    nil,
    headers)
end