cloud_elements 0.2.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/cloud_elements.rb +86 -44
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3c49ee5f7e77a2cb6e3069a88823d5eaae944177
4
- data.tar.gz: 6fd5050c187d4f9058cb4ce9a4cc309af7e4a0ef
3
+ metadata.gz: 14f3ea7f3a7175a74406d009b581408b89610dae
4
+ data.tar.gz: d1ed6d221de23bc516e4a4e73918caa8cf136791
5
5
  SHA512:
6
- metadata.gz: 109ad9e4b83429e23d4e18e182fc644bf4eae60cd4a75a8418d3e28ca4177c51035a86751e6aa1456b6f4624a76fc2d915d56351f9abe3403b2866d8643b2418
7
- data.tar.gz: e1ad9afd753fbfb205413cfee69912bc777b786b5ccedf56bf1587f5ec5874374b9c184d83bf875d180098daa1d9dd4f532cf6945798b9d1cb1c7713cf7351bc
6
+ metadata.gz: 2a7cf628cb9277b3a825ae2862a24c7388f93f6d1f92308555841f019342e45a1136f78866e514d1dc713bd72ce99eb5ce7da1f6ec4f65321709812d4de8b078
7
+ data.tar.gz: d883b0b7719a0030c0a1f602dfc025edb55fabb8332b9294fc4c0821b71caef6d12f8f184bb93015bde8a56dad16dd83da65a3e3f86c56699b5613fe03658635
@@ -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
- attr_accessor :api_endpoint
15
+ attr_accessor :api_endpoint
11
16
 
12
- def initialize(api_endpoint='https://console.cloud-elements.com/elements/api-v1')
13
- @api_endpoint = api_endpoint # Endpoint to hit the api. Defaults to production
14
- @headers = Hash.new # Headers for some requests
15
- @headers['Content-Type'] = 'application-json'
16
- end
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
- private
23
+ private
19
24
 
20
- def process_request
21
- # This will do error handling later
22
- end
25
+ def process_request
26
+ # This will do error handling later
27
+ end
23
28
 
24
- public
25
-
26
- # This method conducts the http request and send back the response.
27
- # It received a string containing the http method, a hash of query/body parameters
28
- # and and optional list of file names for requests that deal with posting files.
29
- def request(httpMethod, params, files)
30
- case httpMethod
31
- when 'get' then response = HTTMultiParty.get(@url, :headers => @headers, :query => params)
32
- when 'post'
33
- if files
34
- files.each { |f| params[f] = File.new(f) }
35
- @headers.delete(['Content-Type'])
36
- response = HTTMultiParty.post(@url, :headers => @headers, :query => params)
37
- @headers['Content-Type'] = 'application-json'
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
- response = HTTMultiParty.post(@url, :headers => @headers, :body => params.to_json)
60
+ auth_string = "Element #{headers}"
40
61
  end
41
- when 'put' then response = HTTMultiParty.put(@url, :headers => @headers, :body => params.to_json)
42
- when 'delete' then response = HTTMultiParty.delete(@url, :headers => @headers, :body => params.to_json)
62
+ @headers['Authorization'] = auth_string
63
+
64
+ return request(httpMethod, params, files)
43
65
  end
44
- return response
45
66
  end
46
67
 
47
- # This method allows users to interact with the Elements API.
48
- # It is essentially a copy of the Java ElementsConnector class.
49
- def invoke(httpMethod, providerName, headers, apiMethodName, params=nil, files=nil, providerVersion='1')
50
- @url = "#{@api_endpoint}/#{providerName}/#{providerVersion}/#{apiMethodName}"
51
- if headers.class() == Hash
52
- auth_string = "User #{headers[:user_secret]}, Organization #{headers[:organization_secret]}"
53
- else
54
- auth_string = "Element #{headers}"
55
- end
56
- @headers['Authorization'] = auth_string
57
- return request(httpMethod, params, files)
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.2.2
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-10 00:00:00.000000000 Z
11
+ date: 2014-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httmultiparty