require "httparty" module SalesforceChunker class Connection 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.login_soap_request_body(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"], "Accept-Encoding": "gzip", } rescue NoMethodError raise ConnectionError, response["Envelope"]["Body"]["Fault"]["faultstring"] end def post_json(url, body, headers={}) post(url, body.to_json, headers) end def post(url, body, headers={}) @log.info "POST: #{url}" response = HTTParty.post(@base_url + url, headers: @default_headers.merge(headers), body: body) self.class.check_response_error(response.parsed_response) end def get_json(url, headers={}) @log.info "GET: #{url}" response = HTTParty.get(@base_url + url, headers: @default_headers.merge(headers)) self.class.check_response_error(response.parsed_response) end def get(url, headers={}) @log.info "GET: #{url}" HTTParty.get(@base_url + url, headers: @default_headers.merge(headers)).body end private def self.login_soap_request_body(username, password, security_token) " #{username.encode(xml: :text)} #{password.encode(xml: :text)}#{security_token.encode(xml: :text)} " end def self.get_instance(server_url) /https:\/\/(.*).salesforce.com/.match(server_url)[1] end def self.check_response_error(response) if response.is_a?(Hash) && response.key?("exceptionCode") raise ResponseError, "#{response["exceptionCode"]}: #{response["exceptionMessage"]}" else response end end end end