Class: Salesforceapi::Rest::Client
- Inherits:
-
Object
- Object
- Salesforceapi::Rest::Client
- Extended by:
- SalesforceApi::Request
- Includes:
- HTTParty
- Defined in:
- lib/salesforceapi-rest.rb
Constant Summary collapse
- Envelope =
<<-HERE <?xml version="1.0" encoding="utf-8" ?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Header> <ns1:SessionHeader soapenv:mustUnderstand="0" xsi:type="ns1:SessionHeader" xmlns:ns1="http://soap.sforce.com/2006/04/metadata"> <ns1:sessionId>%s</ns1:sessionId> </ns1:SessionHeader> <ns2:CallOptions soapenv:mustUnderstand="0" xsi:type="ns2:SessionHeader" xmlns:ns2="http://soap.sforce.com/2006/04/metadata"> <ns2:client>apex_eclipse/16.0.200906151227</ns2:client> </ns2:CallOptions> <ns3:DebuggingHeader soapenv:mustUnderstand="0" xsi:type="ns3:DebuggingHeader" xmlns:ns3="http://soap.sforce.com/2006/04/metadata"> <ns3:debugLevel xsi:nil="true" /> </ns3:DebuggingHeader> </soapenv:Header> <soapenv:Body> %s </soapenv:Body> </soapenv:Envelope> HERE
- @@ssl_port =
443
Class Method Summary collapse
-
.set_headers(auth_setting) ⇒ Object
set header for httparty.
Instance Method Summary collapse
- #add_custom_field(attributes) ⇒ Object
- #config_authorization! ⇒ Object
- #create(object, attributes) ⇒ Object
- #custom_fields_xml(opts) ⇒ Object
- #describe(object) ⇒ Object
-
#initialize(refresh_token, metadata_uri, client_id, client_secret) ⇒ Client
constructor
A new instance of Client.
- #resources ⇒ Object
Methods included from SalesforceApi::Request
Constructor Details
#initialize(refresh_token, metadata_uri, client_id, client_secret) ⇒ Client
Returns a new instance of Client.
30 31 32 33 34 35 36 37 38 |
# File 'lib/salesforceapi-rest.rb', line 30 def initialize(refresh_token, , client_id, client_secret) @refresh_token = refresh_token @client_id = client_id @client_secret = client_secret @metadata_uri = @api_version = "v21.0" @ssl_port = 443 # TODO, right SF use port 443 for all HTTPS traffic. end |
Class Method Details
.set_headers(auth_setting) ⇒ Object
set header for httparty
26 27 28 |
# File 'lib/salesforceapi-rest.rb', line 26 def self.set_headers (auth_setting) headers (auth_setting) end |
Instance Method Details
#add_custom_field(attributes) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/salesforceapi-rest.rb', line 112 def add_custom_field(attributes) auth_header = { "Authorization" => "OAuth " + @access_token, 'Connection' => 'Keep-Alive', 'Content-Type' => 'text/xml', 'SOAPAction' => '""' } self.class.base_uri @metadata_uri data = (Envelope % [@access_token, custom_fields_xml(attributes)]) resp = SalesforceApi::Request.do_request("POST", @metadata_uri, auth_header, data.lstrip) xml_response = Crack::XML.parse(resp.body) if resp.code != 200 SalesforceApi::Errors::ErrorManager.raise_error("HTTP code " + resp.code.to_s + " xml response: " + resp.body, resp.code) else return xml_response end end |
#config_authorization! ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/salesforceapi-rest.rb', line 94 def target = CGI::unescape("https://login.salesforce.com/services/oauth2/token?grant_type=refresh_token&client_id=#{@client_id}&client_secret=#{@client_secret}&refresh_token=#{@refresh_token}") resp = SalesforceApi::Request.do_request("POST", target, {"content-Type" => 'application/json'}, nil) if (resp.code != 200) || !resp.success? = ActiveSupport::JSON.decode(resp.body)["error_description"] SalesforceApi::Errors::ErrorManager.raise_error(, 401) else response = ActiveSupport::JSON.decode(resp.body) end @instance_uri = response['instance_url'] @access_token = response['access_token'] @auth_header = { "Authorization" => "OAuth " + @access_token, "content-Type" => 'application/json' } end |
#create(object, attributes) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/salesforceapi-rest.rb', line 41 def create(object, attributes) path = "/services/data/#{@api_version}/sobjects/#{object}/" target = @instance_uri + path self.class.base_uri @instance_uri data = ActiveSupport::JSON::encode(attributes) resp = SalesforceApi::Request.do_request("POST", target, @auth_header, data) # HTTP code 201 means it was successfully saved. if resp.code != 201 = ActiveSupport::JSON.decode(resp.body)[0]["message"] SalesforceApi::Errors::ErrorManager.raise_error("HTTP code " + resp.code.to_s + ": " + , resp.code) else return ActiveSupport::JSON.decode(resp.body) end end |
#custom_fields_xml(opts) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/salesforceapi-rest.rb', line 135 def custom_fields_xml(opts) # Create XML text from the arguments. = '' @builder = Builder::XmlMarkup.new(:target => ) @builder.tag! :create, :xmlns => "http://soap.sforce.com/2006/04/metadata" do |b| b.tag! :metadata, "xsi:type" => "ns2:CustomField", "xmlns:ns2" => "http://soap.sforce.com/2006/04/metadata" do |c| opts.each { |k,v| c.tag! k, v } end end end |
#describe(object) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/salesforceapi-rest.rb', line 62 def describe(object) path = "/services/data/#{@api_version}/sobjects/#{object}/describe" target = @instance_uri + path self.class.base_uri @instance_uri resp = SalesforceApi::Request.do_request("GET", target, @auth_header, nil) if (resp.code != 200) || !resp.success? = ActiveSupport::JSON.decode(resp.body)[0]["message"] SalesforceApi::Errors::ErrorManager.raise_error("HTTP code " + resp.code.to_s + ": " + , resp.code) else return ActiveSupport::JSON.decode(resp.body) end end |
#resources ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/salesforceapi-rest.rb', line 78 def resources path = "/services/data/#{@api_version}" target = @instance_uri + path self.class.base_uri @instance_uri resp = SalesforceApi::Request.do_request("GET", target, @auth_header, nil) if (resp.code != 200) || !resp.success? = ActiveSupport::JSON.decode(resp.body)[0]["message"] SalesforceApi::Errors::ErrorManager.raise_error("HTTP code " + resp.code.to_s + ": " + , resp.code) else return ActiveSupport::JSON.decode(resp.body) end end |