Class: SalesforceAPI::Caller
- Inherits:
-
Object
- Object
- SalesforceAPI::Caller
- Defined in:
- lib/salesforce-api/caller.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#instance_url ⇒ Object
readonly
Returns the value of attribute instance_url.
-
#issued_at ⇒ Object
readonly
Returns the value of attribute issued_at.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#signature ⇒ Object
readonly
Returns the value of attribute signature.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #attachment(name, id) ⇒ Object
- #authorize ⇒ Object
- #http_get(uri, auto_authorize = true) ⇒ Object
- #http_patch(uri, body, auto_authorize = true) ⇒ Object
-
#initialize(options = {}) ⇒ Caller
constructor
A new instance of Caller.
- #query(query_string) ⇒ Object
- #sobject(name, id, fields = "") ⇒ Object
- #token_request_parameters ⇒ Object
- #update(name, id, body = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Caller
Returns a new instance of Caller.
11 12 13 14 15 16 17 18 19 |
# File 'lib/salesforce-api/caller.rb', line 11 def initialize = {} @api_version = [:api_version] ||= ENV["SALESFORCE_API_VERSION"] @username = [:username] ||= ENV["SALESFORCE_USERNAME"] @password = [:password] ||= ENV["SALESFORCE_PASSWORD"] @client_id = [:client_id] ||= ENV["SALESFORCE_CLIENT_ID"] @client_secret = [:client_secret] ||= ENV["SALESFORCE_CLIENT_SECRET"] @host = [:host] ||= ENV["SALESFORCE_HOST"] end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
9 10 11 |
# File 'lib/salesforce-api/caller.rb', line 9 def access_token @access_token end |
#api_version ⇒ Object (readonly)
Returns the value of attribute api_version.
9 10 11 |
# File 'lib/salesforce-api/caller.rb', line 9 def api_version @api_version end |
#client_id ⇒ Object (readonly)
Returns the value of attribute client_id.
9 10 11 |
# File 'lib/salesforce-api/caller.rb', line 9 def client_id @client_id end |
#client_secret ⇒ Object (readonly)
Returns the value of attribute client_secret.
9 10 11 |
# File 'lib/salesforce-api/caller.rb', line 9 def client_secret @client_secret end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
9 10 11 |
# File 'lib/salesforce-api/caller.rb', line 9 def host @host end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
9 10 11 |
# File 'lib/salesforce-api/caller.rb', line 9 def id @id end |
#instance_url ⇒ Object (readonly)
Returns the value of attribute instance_url.
9 10 11 |
# File 'lib/salesforce-api/caller.rb', line 9 def instance_url @instance_url end |
#issued_at ⇒ Object (readonly)
Returns the value of attribute issued_at.
9 10 11 |
# File 'lib/salesforce-api/caller.rb', line 9 def issued_at @issued_at end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
9 10 11 |
# File 'lib/salesforce-api/caller.rb', line 9 def password @password end |
#signature ⇒ Object (readonly)
Returns the value of attribute signature.
9 10 11 |
# File 'lib/salesforce-api/caller.rb', line 9 def signature @signature end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
9 10 11 |
# File 'lib/salesforce-api/caller.rb', line 9 def username @username end |
Instance Method Details
#attachment(name, id) ⇒ Object
21 22 23 |
# File 'lib/salesforce-api/caller.rb', line 21 def name, id http_get( URI("#{instance_url}/services/data/v#{api_version}/sobjects/#{name}/#{id}/body")).body end |
#authorize ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/salesforce-api/caller.rb', line 25 def begin ( Net::HTTP.new(@host, 443).tap { |http| http.use_ssl = true }.post("/services/oauth2/token", token_request_parameters.to_query).body) rescue Errno::ECONNREFUSED raise ConnectionException, "Connection problem, did you supply a host?" end self end |
#http_get(uri, auto_authorize = true) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/salesforce-api/caller.rb', line 36 def http_get uri, = true req = Net::HTTP::Get.new(uri.request_uri) req["Authorization"] = "Bearer #{access_token}" response = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true) do |http| http.request(req) end if response.instance_of?(Net::HTTPUnauthorized) && http_get uri, false else response end end |
#http_patch(uri, body, auto_authorize = true) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/salesforce-api/caller.rb', line 51 def http_patch uri, body, = true req = Net::HTTPGenericRequest.new("PATCH", nil, nil, uri.request_uri) req["Authorization"] = "Bearer #{access_token}" req["Content-Type"] = "application/json" req.body = body.to_json response = Net::HTTP.start( uri.hostname, uri.port, :use_ssl => true ) do |http| http.request( req ) end if response.instance_of?(Net::HTTPUnauthorized) && http_patch uri, body, false else response end end |
#query(query_string) ⇒ Object
74 75 76 77 78 |
# File 'lib/salesforce-api/caller.rb', line 74 def query query_string uri = URI("#{instance_url}/services/data/v#{api_version}/query") uri.query = "q=#{URI::escape(query_string)}" http_get(uri).body end |
#sobject(name, id, fields = "") ⇒ Object
68 69 70 71 72 |
# File 'lib/salesforce-api/caller.rb', line 68 def sobject name, id, fields = "" uri = URI("#{instance_url}/services/data/v#{api_version}/sobjects/#{name}/#{id}") uri.query = {:fields => fields}.to_query unless fields.empty? http_get(uri).body end |
#token_request_parameters ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/salesforce-api/caller.rb', line 80 def token_request_parameters @token_request_parameters.nil? ? @token_request_parameters = { :grant_type => "password", :username => @username, :password => @password, :client_id => @client_id, :client_secret => @client_secret }.delete_if{|k,v| v.to_s.empty?} : @token_request_parameters end |
#update(name, id, body = {}) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/salesforce-api/caller.rb', line 90 def update name, id, body = {} uri = URI("#{instance_url}/services/data/v#{api_version}/sobjects/#{name}/#{id}") response = http_patch( uri, body ) if response.instance_of? Net::HTTPBadRequest raise Error400, "The request could not be understood, usually because the JSON or XML body contains an error. Code[#{response.code}]." end end |