cloud_elements 0.2.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/cloud_elements.rb +86 -44
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14f3ea7f3a7175a74406d009b581408b89610dae
|
4
|
+
data.tar.gz: d1ed6d221de23bc516e4a4e73918caa8cf136791
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2a7cf628cb9277b3a825ae2862a24c7388f93f6d1f92308555841f019342e45a1136f78866e514d1dc713bd72ce99eb5ce7da1f6ec4f65321709812d4de8b078
|
7
|
+
data.tar.gz: d883b0b7719a0030c0a1f602dfc025edb55fabb8332b9294fc4c0821b71caef6d12f8f184bb93015bde8a56dad16dd83da65a3e3f86c56699b5613fe03658635
|
data/lib/cloud_elements.rb
CHANGED
@@ -1,59 +1,101 @@
|
|
1
1
|
require 'httmultiparty'
|
2
2
|
|
3
|
+
class APIException < Exception
|
4
|
+
end
|
5
|
+
|
6
|
+
class Element
|
7
|
+
|
3
8
|
=begin
|
4
|
-
Class that allows connection to the Cloud Elements REST API.
|
5
|
-
All connections are made through the invoke method.
|
6
|
-
It utilized HTTMultiParty to make http requests.
|
9
|
+
Class that allows connection to the Cloud Elements REST API.
|
10
|
+
All connections are made through the invoke method.
|
11
|
+
It utilized HTTMultiParty to make http requests.
|
7
12
|
=end
|
8
|
-
class ElementsConnector
|
13
|
+
class ElementsConnector
|
9
14
|
|
10
|
-
|
15
|
+
attr_accessor :api_endpoint
|
11
16
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
def initialize(api_endpoint='https://console.cloud-elements.com/elements/api-v1')
|
18
|
+
@api_endpoint = api_endpoint # Endpoint to hit the api. Defaults to production
|
19
|
+
@headers = Hash.new # Headers for some requests
|
20
|
+
@headers['Content-Type'] = 'application-json'
|
21
|
+
end
|
17
22
|
|
18
|
-
|
23
|
+
private
|
19
24
|
|
20
|
-
|
21
|
-
|
22
|
-
|
25
|
+
def process_request
|
26
|
+
# This will do error handling later
|
27
|
+
end
|
23
28
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
29
|
+
public
|
30
|
+
|
31
|
+
# This method conducts the http request and send back the response.
|
32
|
+
# It received a string containing the http method, a hash of query/body parameters
|
33
|
+
# and and optional list of file names for requests that deal with posting files.
|
34
|
+
def request(httpMethod, params, files)
|
35
|
+
case httpMethod
|
36
|
+
when 'get' then response = HTTMultiParty.get(@url, :headers => @headers, :query => params)
|
37
|
+
when 'post'
|
38
|
+
if files
|
39
|
+
files.each { |f| params[f] = File.new(f) }
|
40
|
+
@headers.delete(['Content-Type'])
|
41
|
+
response = HTTMultiParty.post(@url, :headers => @headers, :query => params)
|
42
|
+
@headers['Content-Type'] = 'application-json'
|
43
|
+
else
|
44
|
+
response = HTTMultiParty.post(@url, :headers => @headers, :body => params.to_json)
|
45
|
+
end
|
46
|
+
when 'put' then response = HTTMultiParty.put(@url, :headers => @headers, :body => params.to_json)
|
47
|
+
when 'delete' then response = HTTMultiParty.delete(@url, :headers => @headers, :body => params.to_json)
|
48
|
+
end
|
49
|
+
|
50
|
+
return response
|
51
|
+
end
|
52
|
+
|
53
|
+
# This method allows users to interact with the Elements API.
|
54
|
+
# It is essentially a copy of the Java ElementsConnector class.
|
55
|
+
def invoke(httpMethod, providerName, headers, apiMethodName, params=nil, files=nil, providerVersion='1')
|
56
|
+
@url = "#{@api_endpoint}/#{providerName}/#{providerVersion}/#{apiMethodName}"
|
57
|
+
if headers.class() == Hash
|
58
|
+
auth_string = "User #{headers[:user_secret]}, Organization #{headers[:organization_secret]}"
|
38
59
|
else
|
39
|
-
|
60
|
+
auth_string = "Element #{headers}"
|
40
61
|
end
|
41
|
-
|
42
|
-
|
62
|
+
@headers['Authorization'] = auth_string
|
63
|
+
|
64
|
+
return request(httpMethod, params, files)
|
43
65
|
end
|
44
|
-
return response
|
45
66
|
end
|
46
67
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
68
|
+
|
69
|
+
def initialize(providerName, elementToken, base_url='https://console.cloud-elements.com/elements/api-v1/')
|
70
|
+
apidocs = HTTMultiParty.get(base_url+providerName)
|
71
|
+
if apidocs.has_key? 'errorMsg'
|
72
|
+
raise APIException.new "Element Hub for #{providerName} does not exist"
|
73
|
+
end
|
74
|
+
|
75
|
+
method_data = []
|
76
|
+
method_count = 0
|
77
|
+
apidocs['apis'].each do |api|
|
78
|
+
method_data[method_count] = Hash.new
|
79
|
+
|
80
|
+
method_data[method_count][:elementToken] = elementToken
|
81
|
+
method_data[method_count][:providerName] = providerName
|
82
|
+
|
83
|
+
method_data[method_count][:apiMethodName] = api['path'].split('/')[-1]
|
84
|
+
method_data[method_count][:httpMethod] = api['operations'][0]["httpMethod"].downcase()
|
85
|
+
method_count = method_count + 1
|
86
|
+
end
|
87
|
+
|
88
|
+
method_data.each do |data|
|
89
|
+
define_singleton_method data[:apiMethodName] do |params=nil, files=nil|
|
90
|
+
client = ElementsConnector.new
|
91
|
+
return client.invoke(data[:httpMethod], data[:providerName], data[:elementToken], data[:apiMethodName], params, files)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
response = self.ping
|
96
|
+
if not response['success']
|
97
|
+
raise APIException.new response['errorMsg']
|
98
|
+
end
|
99
|
+
|
58
100
|
end
|
59
|
-
end
|
101
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cloud_elements
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Christian Rodriguez
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httmultiparty
|