Class: SalesforceChunker::Connection

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

Instance Method Summary collapse

Constructor Details

#initialize(username: "", password: "", security_token: "", domain: "login", salesforce_version: "42.0", **options) ⇒ Connection

Returns a new instance of Connection.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/salesforce_chunker/connection.rb', line 6

def initialize(username: "", password: "", security_token: "", domain: "login", salesforce_version: "42.0", **options)
  @log = options[:logger] || Logger.new(options[:log_output])
  @log.progname = "salesforce_chunker"

  response = HTTParty.post(
    "https://#{domain}.salesforce.com/services/Soap/u/#{salesforce_version}",
    headers: { "SOAPAction": "login", "Content-Type": "text/xml; charset=UTF-8" },
    body: self.class.(username, password, security_token)
  ).parsed_response

  result = response["Envelope"]["Body"]["loginResponse"]["result"]

  instance = self.class.get_instance(result["serverUrl"])

  @base_url = "https://#{instance}.salesforce.com/services/async/#{salesforce_version}/"
  @default_headers = {
    "Content-Type": "application/json",
    "X-SFDC-Session": result["sessionId"],
  }
rescue NoMethodError
  raise ConnectionError, response["Envelope"]["Body"]["Fault"]["faultstring"]
end

Instance Method Details

#get(url, headers = {}) ⇒ Object



49
50
51
52
53
54
# File 'lib/salesforce_chunker/connection.rb', line 49

def get(url, headers={})
  @log.info "GET: #{url}"
  self.class.retry_block(log: @log) do
    HTTParty.get(@base_url + url, headers: @default_headers.merge(headers)).body
  end
end

#get_json(url, headers = {}) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/salesforce_chunker/connection.rb', line 41

def get_json(url, headers={})
  @log.info "GET: #{url}"
  response = self.class.retry_block(log: @log) do
    HTTParty.get(@base_url + url, headers: @default_headers.merge(headers))
  end
  self.class.check_response_error(response.parsed_response)
end

#post(url, body, headers = {}) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/salesforce_chunker/connection.rb', line 33

def post(url, body, headers={})
  @log.info "POST: #{url}"
  response = self.class.retry_block(log: @log) do
    HTTParty.post(@base_url + url, headers: @default_headers.merge(headers), body: body)
  end
  self.class.check_response_error(response.parsed_response)
end

#post_json(url, body, headers = {}) ⇒ Object



29
30
31
# File 'lib/salesforce_chunker/connection.rb', line 29

def post_json(url, body, headers={})
  post(url, body.to_json, headers)
end