class CloudElementsConnector::ElementsConnector
Class that allows connection to the Cloud Elements API. All connections are made through the invoke method. It utilized HTTMultiParty to make http requests.
Attributes
api_endpoint[RW]
Public Class Methods
new(api_endpoint='https://console.cloud-elements.com/elements/api-v1')
click to toggle source
# File lib/cloud_elements_connector.rb, line 17 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 @headers['Content-Type'] = 'application-json' @headers['User-Agent'] = 'elements-connector-ruby' end
Public Instance Methods
invoke(httpMethod, providerName, headers, apiMethodName, params=nil, files=nil, providerVersion='1')
click to toggle source
This method allows users to interact with the Elements API. It is essentially a copy of the Java ElementsConnector
class.
# File lib/cloud_elements_connector.rb, line 56 def invoke(httpMethod, providerName, headers, apiMethodName, params=nil, files=nil, providerVersion='1') @url = "#{@api_endpoint}/#{providerName}/#{providerVersion}/#{apiMethodName}" if headers.class() == Hash auth_string = "User #{headers[:user_secret]}, Organization #{headers[:organization_secret]}" else auth_string = "Element #{headers}" end @headers['Authorization'] = auth_string return request(httpMethod, params, files) end
request(httpMethod, params, files)
click to toggle source
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.
# File lib/cloud_elements_connector.rb, line 35 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) } @headers.delete(['Content-Type']) response = HTTMultiParty.post(@url, :headers => @headers, :query => params) @headers['Content-Type'] = 'application-json' 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
Private Instance Methods
process_request()
click to toggle source
# File lib/cloud_elements_connector.rb, line 26 def process_request # This will do error handling later end