Class: Element

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_elements.rb

Defined Under Namespace

Classes: ElementsConnector

Instance Method Summary collapse

Constructor Details

#initialize(providerName, elementToken, base_url = 'https://console.cloud-elements.com/elements/api-v1/') ⇒ Element

Returns a new instance of Element.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/cloud_elements.rb', line 69

def initialize(providerName, elementToken, base_url='https://console.cloud-elements.com/elements/api-v1/')
    apidocs = HTTMultiParty.get(base_url+providerName)
    if apidocs.has_key? 'errorMsg'
        raise APIException.new "Element Hub for #{providerName} does not exist"
    end

    method_data = []
    method_count = 0
    apidocs['apis'].each do |api|
        method_data[method_count] = Hash.new
        
        method_data[method_count][:elementToken] = elementToken
        method_data[method_count][:providerName] = providerName

        method_data[method_count][:apiMethodName] = api['path'].split('/')[-1]
        method_data[method_count][:httpMethod] = api['operations'][0]["httpMethod"].downcase()
        method_count = method_count + 1    
    end

    method_data.each do |data|
        define_singleton_method data[:apiMethodName] do |params=nil, files=nil|
            client = ElementsConnector.new
            return client.invoke(data[:httpMethod], data[:providerName], data[:elementToken], data[:apiMethodName], params, files)
        end
    end

    if self.respond_to? :ping
        response = self.ping
        if not response['success']
            raise APIException.new response['errorMsg']
        end
    end
end