Class: SalesforceBulk::Connection
- Inherits:
-
Object
- Object
- SalesforceBulk::Connection
- Defined in:
- lib/salesforce_bulk/connection.rb
Constant Summary collapse
- @@XML_HEADER =
'<?xml version="1.0" encoding="utf-8" ?>'
- @@API_VERSION =
nil
- @@LOGIN_HOST =
'login.salesforce.com'
- @@INSTANCE_HOST =
Gets set in login()
nil
Instance Method Summary collapse
- #get_request(host, path, headers) ⇒ Object
- #https(host) ⇒ Object
-
#initialize(username, password, api_version, in_sandbox) ⇒ Connection
constructor
A new instance of Connection.
-
#login ⇒ Object
private.
- #parse_instance ⇒ Object
- #parse_response(response) ⇒ Object
- #post_xml(host, path, xml, headers) ⇒ Object
Constructor Details
#initialize(username, password, api_version, in_sandbox) ⇒ Connection
Returns a new instance of Connection.
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/salesforce_bulk/connection.rb', line 10 def initialize(username, password, api_version, in_sandbox) @username = username @password = password @session_id = nil @server_url = nil @instance = nil @@API_VERSION = api_version @@LOGIN_PATH = "/services/Soap/u/#{@@API_VERSION}" @@PATH_PREFIX = "/services/async/#{@@API_VERSION}/" @@LOGIN_HOST = 'test.salesforce.com' if in_sandbox login() end |
Instance Method Details
#get_request(host, path, headers) ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/salesforce_bulk/connection.rb', line 65 def get_request(host, path, headers) host = host || @@INSTANCE_HOST path = "#{@@PATH_PREFIX}#{path}" if host != @@LOGIN_HOST # Not login, need to add session id to header headers['X-SFDC-Session'] = @session_id; end https(host).get(path, headers).body end |
#https(host) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/salesforce_bulk/connection.rb', line 76 def https(host) req = Net::HTTP.new(host, 443) req.use_ssl = true req.verify_mode = OpenSSL::SSL::VERIFY_NONE req end |
#login ⇒ Object
private
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/salesforce_bulk/connection.rb', line 26 def login() xml = '<?xml version="1.0" encoding="utf-8" ?>' xml += "<env:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" xml += " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" xml += " xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">" xml += " <env:Body>" xml += " <n1:login xmlns:n1=\"urn:partner.soap.sforce.com\">" xml += " <n1:username>#{@username}</n1:username>" xml += " <n1:password>#{@password}</n1:password>" xml += " </n1:login>" xml += " </env:Body>" xml += "</env:Envelope>" headers = Hash['Content-Type' => 'text/xml; charset=utf-8', 'SOAPAction' => 'login'] response = post_xml(@@LOGIN_HOST, @@LOGIN_PATH, xml, headers) # response_parsed = XmlSimple.xml_in(response) response_parsed = parse_response response @session_id = response_parsed['Body'][0]['loginResponse'][0]['result'][0]['sessionId'][0] @server_url = response_parsed['Body'][0]['loginResponse'][0]['result'][0]['serverUrl'][0] @instance = parse_instance() @@INSTANCE_HOST = "#{@instance}.salesforce.com" end |
#parse_instance ⇒ Object
83 84 85 86 |
# File 'lib/salesforce_bulk/connection.rb', line 83 def parse_instance() @server_url =~ /https:\/\/([a-z]{2,2}[0-9]{1,2})-api/ @instance = $~.captures[0] end |
#parse_response(response) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/salesforce_bulk/connection.rb', line 88 def parse_response response response_parsed = XmlSimple.xml_in(response) if response.downcase.include?("faultstring") || response.downcase.include?("exceptionmessage") begin if response.downcase.include?("faultstring") = response_parsed["Body"][0]["Fault"][0]["faultstring"][0] elsif response.downcase.include?("exceptionmessage") = response_parsed["exceptionMessage"][0] end rescue raise "An unknown error has occured within the salesforce_bulk gem. This is most likely caused by bad request, but I am unable to parse the correct error message. Here is a dump of the response for your convenience. #{response}" end raise end response_parsed end |
#post_xml(host, path, xml, headers) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/salesforce_bulk/connection.rb', line 53 def post_xml(host, path, xml, headers) host = host || @@INSTANCE_HOST if host != @@LOGIN_HOST # Not login, need to add session id to header headers['X-SFDC-Session'] = @session_id; path = "#{@@PATH_PREFIX}#{path}" end https(host).post(path, xml, headers).body end |