Class: Element::ElementsConnector

Inherits:
Object
  • Object
show all
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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_endpoint = 'https://console.cloud-elements.com/elements/api-v1') ⇒ ElementsConnector

Returns a new instance of ElementsConnector.



17
18
19
20
21
# File 'lib/cloud_elements.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'
end

Instance Attribute Details

#api_endpointObject

Returns the value of attribute api_endpoint.



15
16
17
# File 'lib/cloud_elements.rb', line 15

def api_endpoint
  @api_endpoint
end

Instance Method Details

#invoke(httpMethod, providerName, headers, 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.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cloud_elements.rb', line 55

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) ⇒ 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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cloud_elements.rb', line 34

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