Class: ElementsConnector
- Inherits:
-
Object
- Object
- ElementsConnector
- Defined in:
- lib/cloud_elements.rb
Overview
Class that allows connection to the Cloud Elements REST API. All connections are made through the invoke method. It utilized HTTMultiParty to make http requests.
Instance Method Summary collapse
-
#initialize(api_endpoint = 'https://console.cloud-elements.com/elements/api-v1') ⇒ ElementsConnector
constructor
A new instance of ElementsConnector.
-
#invoke(httpMethod, providerName, elementToken, apiMethodName, params = nil, files = nil, providerVersion = '1') ⇒ Object
This method allows users to interact with the Elements API.
-
#request(httpMethod, params, files) ⇒ Object
This method conducts the http request and send back the response.
Constructor Details
#initialize(api_endpoint = 'https://console.cloud-elements.com/elements/api-v1') ⇒ ElementsConnector
Returns a new instance of ElementsConnector.
9 10 11 12 |
# File 'lib/cloud_elements.rb', line 9 def initialize(api_endpoint='https://console.cloud-elements.com/elements/api-v1') @api_endpoint = api_endpoint # Endpoint to hit the api. Defaults to production @headers = Hash.new # Headers for some requests end |
Instance Method Details
#invoke(httpMethod, providerName, elementToken, apiMethodName, params = nil, files = nil, providerVersion = '1') ⇒ Object
This method allows users to interact with the Elements API. It is essentially a copy of the Java ElementsConnector class.
43 44 45 46 47 |
# File 'lib/cloud_elements.rb', line 43 def invoke(httpMethod, providerName, elementToken, apiMethodName, params=nil, files=nil, providerVersion='1') @url = "#{@api_endpoint}/#{providerName}/#{providerVersion}/#{apiMethodName}" @headers['Authorization'] = "Element #{elementToken}" return request(httpMethod, params, files) end |
#request(httpMethod, params, files) ⇒ Object
This method conducts the http request and send back the response. It received a string containing the http method, a hash of query/body parameters and and optional list of file names for requests that deal with posting files.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cloud_elements.rb', line 25 def request(httpMethod, params, files) case httpMethod when 'get' then response = HTTMultiParty.get(@url, :headers => @headers, :query => params) when 'post' if files files.each { |f| params[f] = File.new(f) } response = HTTMultiParty.post(@url, :headers => @headers, :query => params) else response = HTTMultiParty.post(@url, :headers => @headers, :body => params.to_json) end when 'put' then response = HTTMultiParty.put(@url, :headers => @headers, :body => params.to_json) when 'delete' then response = HTTMultiParty.delete(@url, :headers => @headers, :body => params.to_json) end return response end |