bubbles-rest-client 0.3.1 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,28 +3,27 @@ require 'bubbles/rest_environment'
3
3
 
4
4
  module Bubbles
5
5
  class RestClientResources
6
- def environment
7
- Bubbles.configuration.environment
6
+ def environment(env_name = nil)
7
+ Bubbles.configuration.environment(env_name)
8
8
  end
9
9
 
10
10
  ##
11
11
  # Create a new instance of +RestClientResources+.
12
12
  #
13
13
  # @param env The +RestEnvironment+ that should be used for this set of resources.
14
- # @param api_key The API key to use to send to the host for unauthenticated requests.
14
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
15
+ # to +nil+.
16
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
17
+ # +"X-API-Key"+.
15
18
  #
16
- def initialize(env, api_key)
17
- unless env
18
- env = :local
19
- end
20
-
19
+ def initialize(env, api_key = nil, api_key_name='X-API-Key')
21
20
  unless api_key
22
21
  api_key = ''
23
22
  end
24
23
 
25
- @environment = get_environment env
26
24
  @api_key = api_key
27
25
  @auth_token = nil
26
+ @api_key_name = api_key_name
28
27
  end
29
28
 
30
29
  ##
@@ -32,17 +31,16 @@ module Bubbles
32
31
  #
33
32
  # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
34
33
  # @param [Endpoint] endpoint The +Endpoint+ which should be requested
34
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
35
+ # call. Defaults to an empty +Hash+.
35
36
  #
36
37
  # @return [RestClient::Response] The +Response+ resulting from the execution of the GET call.
37
38
  #
38
- def self.execute_get_unauthenticated(env, endpoint)
39
- execute_rest_call(env, endpoint, nil, nil, nil) do |env, url, data, headers|
40
- if env.scheme == 'https'
41
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
42
- .get(headers)
43
- else
44
- next RestClient.get(url.to_s, headers)
45
- end
39
+ def self.execute_get_unauthenticated(env, endpoint, uri_params, additional_headers = {}, api_key = nil, api_key_name='X-API-Key')
40
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
41
+
42
+ execute_rest_call(env, endpoint, nil, nil, composite_headers, uri_params) do |env, url, data, headers|
43
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).get(headers)
46
44
  end
47
45
  end
48
46
 
@@ -53,19 +51,30 @@ module Bubbles
53
51
  #
54
52
  # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
55
53
  # @param [Endpoint] endpoint The +Endpoint+ which should be requested
56
- # @param [String] auth_token The authorization token to use for authentication.
54
+ # @param [Symbol] auth_type The authorization type to use (Bearer, or Basic)
55
+ # @param [String] auth_value The authorization token OR encoded value (login/password )to use for authentication.
57
56
  # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
57
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
58
+ # call. Defaults to an empty +Hash+.
59
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
60
+ # to +nil+.
61
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
62
+ # +"X-API-Key"+.
58
63
  #
59
64
  # @return [RestClient::Response] The +Response+ resulting from the execution of the GET call.
60
65
  #
61
- def self.execute_get_authenticated(env, endpoint, auth_token, uri_params)
62
- execute_rest_call(env, endpoint, nil, auth_token, nil, uri_params) do |env, url, data, headers|
63
- if env.scheme == 'https'
64
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
65
- .get(headers)
66
- else
67
- next RestClient.get(url.to_s, headers)
68
- end
66
+ def self.execute_get_authenticated(env, endpoint, auth_type, auth_value, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
67
+ if auth_type == :basic
68
+ composite_headers = RestClientResources.build_composite_headers(endpoint.additional_headers, {
69
+ Authorization: 'Basic ' + Base64.strict_encode64(auth_value)
70
+ })
71
+ auth_value = nil
72
+ else
73
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
74
+ end
75
+
76
+ execute_rest_call(env, endpoint, nil, auth_value, composite_headers, uri_params) do |env, url, data, headers|
77
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).get(headers)
69
78
  end
70
79
  end
71
80
 
@@ -76,17 +85,21 @@ module Bubbles
76
85
  #
77
86
  # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
78
87
  # @param [Endpoint] endpoint The +Endpoint+ which should be requested
88
+ # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
89
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
90
+ # call. Defaults to an empty +Hash+.
91
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
92
+ # to +nil+.
93
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
94
+ # +"X-API-Key"+.
79
95
  #
80
96
  # @return [RestClient::Response] The +Response+ resulting from the execution of the GET call.
81
97
  #
82
- def self.execute_head_unauthenticated(env, endpoint, uri_params, additional_headers)
83
- execute_rest_call(env, endpoint, nil, nil, additional_headers, uri_params) do |env, url, data, headers|
84
- if env.scheme == 'https'
85
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
86
- .head(headers)
87
- else
88
- next RestClient.head(url.to_s, headers)
89
- end
98
+ def self.execute_head_unauthenticated(env, endpoint, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
99
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
100
+
101
+ execute_rest_call(env, endpoint, nil, nil, composite_headers, uri_params) do |env, url, data, headers|
102
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).head(headers)
90
103
  end
91
104
  end
92
105
 
@@ -100,49 +113,20 @@ module Bubbles
100
113
  # @param [Endpoint] endpoint The +Endpoint+ which should be requested
101
114
  # @param [String] auth_token The authorization token to use for authentication.
102
115
  # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
116
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
117
+ # call. Defaults to an empty +Hash+.
118
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
119
+ # to +nil+.
120
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
121
+ # +"X-API-Key"+.
103
122
  #
104
123
  # @return [RestClient::Response] The +Response+ resulting from the execution of the HEAD call.
105
124
  #
106
- def self.execute_head_authenticated(env, endpoint, auth_token, uri_params)
107
- execute_rest_call(env, endpoint, nil, auth_token, nil, uri_params) do |env, url, data, headers|
108
- if env.scheme == 'https'
109
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
110
- .head(headers)
111
- else
112
- next RestClient.head(url.to_s, headers)
113
- end
114
- end
115
- end
116
-
117
- ##
118
- # Execute a HEAD request without authentication but with some parameters encoded in the URI string.
119
- #
120
- # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
121
- # @param [Endpoint] endpoint The +Endpoint+ which should be requested
122
- # @param [String] api_key The API key to use to validate the client.
123
- # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
124
- # @param [Hash] headers An optional +Hash+ of additional headers to pass with the request.
125
- #
126
- # @return [RestClient::Response] The +Response+ resulting from the execution of the HEAD call.
127
- #
128
- def self.execute_head_unauthenticated_with_uri_params(env, endpoint, api_key, uri_params, headers=nil)
129
- additional_headers = {
130
- 'X-Api-Key' => api_key
131
- }
125
+ def self.execute_head_authenticated(env, endpoint, auth_token, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
126
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
132
127
 
133
- unless headers.nil?
134
- headers.each { |nextHeader|
135
- additional_headers[nextHeader[0]] = nextHeader[1]
136
- }
137
- end
138
-
139
- execute_rest_call(env, endpoint, nil, nil, additional_headers, uri_params) do |env, url, data, headers|
140
- if env.scheme == 'https'
141
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
142
- .head(headers)
143
- else
144
- next RestClient.head(url.to_s, headers)
145
- end
128
+ execute_rest_call(env, endpoint, nil, auth_token, composite_headers, uri_params) do |env, url, data, headers|
129
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).head(headers)
146
130
  end
147
131
  end
148
132
 
@@ -153,29 +137,20 @@ module Bubbles
153
137
  # @param [Endpoint] endpoint The +Endpoint+ which should be requested
154
138
  # @param [String] api_key The API key to use to process the request. Will be placed in an 'X-API-KEY' header.
155
139
  # @param [Hash] data A +Hash+ of key-value pairs that will be sent in the body of the http request.
156
- # @param [Hash] headers (Optional) A +Hash+ of key-value pairs that will be sent as HTTP headers as part of the
157
- # request. Defaults to +nil+.
140
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
141
+ # call. Defaults to an empty +Hash+.
142
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
143
+ # to +nil+.
144
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
145
+ # +"X-API-Key"+.
158
146
  #
159
147
  # @return [RestClient::Response] The +Response+ resulting from the execution of the POST call.
160
148
  #
161
- def self.execute_post_with_api_key(env, endpoint, api_key, data, headers=nil)
162
- additional_headers = {
163
- 'X-Api-Key' => api_key
164
- }
149
+ def self.execute_post_unauthenticated(env, endpoint, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
150
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
165
151
 
166
- unless headers.nil?
167
- headers.each { |nextHeader|
168
- additional_headers[nextHeader[0]] = nextHeader[1]
169
- }
170
- end
171
-
172
- execute_rest_call(env, endpoint, data, nil, additional_headers) do |env, url, data, headers|
173
- if env.scheme == 'https'
174
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
175
- .post(data.to_json, additional_headers)
176
- else
177
- next RestClient.post url.to_s, data.to_json, additional_headers
178
- end
152
+ execute_rest_call(env, endpoint, data, nil, composite_headers) do |env, url, data, headers|
153
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).post(data.to_json, headers)
179
154
  end
180
155
  end
181
156
 
@@ -187,18 +162,27 @@ module Bubbles
187
162
  # @param [String] auth_token The authorization token retrieved during some former authentication call. Will be
188
163
  # placed into a Authorization header.
189
164
  # @param [Hash] data A +Hash+ of key-value pairs that will be sent in the body of the http request.
165
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
166
+ # call. Defaults to an empty +Hash+.
167
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
168
+ # to +nil+.
169
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
170
+ # +"X-API-Key"+.
190
171
  #
191
172
  # @return [RestClient::Response] The +Response+ resulting from the execution of the POST call.
192
173
  #
193
- def self.execute_post_authenticated(env, endpoint, auth_token, data)
194
- return execute_rest_call(env, endpoint, data, auth_token, nil) do |env, url, data, headers|
195
- if env.scheme == 'https'
196
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
197
- .post(data.to_json, headers)
174
+ def self.execute_post_authenticated(env, endpoint, auth_type, auth_value, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
175
+ if auth_type == :basic
176
+ composite_headers = RestClientResources.build_composite_headers(endpoint.additional_headers, {
177
+ Authorization: 'Basic ' + Base64.strict_encode64(auth_value)
178
+ })
179
+ auth_value = nil
180
+ else
181
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
182
+ end
198
183
 
199
- else
200
- next RestClient.post(url.to_s, data.to_json, headers)
201
- end
184
+ execute_rest_call(env, endpoint, data, auth_value, composite_headers) do |env, url, data, headers|
185
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).post(data.to_json, headers)
202
186
  end
203
187
  end
204
188
 
@@ -211,18 +195,20 @@ module Bubbles
211
195
  # placed into a Authorization header.
212
196
  # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
213
197
  # @param [Hash] data A +Hash+ of key-value pairs that will be sent in the body of the http request.
198
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
199
+ # call. Defaults to an empty +Hash+.
200
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
201
+ # to +nil+.
202
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
203
+ # +"X-API-Key"+.
214
204
  #
215
205
  # @return [RestClient::Response] The +Response+ resulting from the execution of the PATCH call.
216
206
  #
217
- def self.execute_patch_authenticated(env, endpoint, auth_token, uri_params, data)
218
- return execute_rest_call(env, endpoint, data, auth_token, nil, uri_params) do |env, url, data, headers|
219
- if env.scheme == 'https'
220
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
221
- .patch(data.to_json, headers)
207
+ def self.execute_patch_authenticated(env, endpoint, auth_token, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
208
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
222
209
 
223
- else
224
- next RestClient.patch(url.to_s, data.to_json, headers)
225
- end
210
+ return execute_rest_call(env, endpoint, data, auth_token, composite_headers, uri_params) do |env, url, data, headers|
211
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).patch(data.to_json, headers)
226
212
  end
227
213
  end
228
214
 
@@ -233,17 +219,20 @@ module Bubbles
233
219
  # @param [Endpoint] endpoint The +Endpoint+ which should be requested
234
220
  # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
235
221
  # @param [Hash] data A +Hash+ of key-value pairs that will be sent in the body of the http request.
222
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
223
+ # call. Defaults to an empty +Hash+.
224
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
225
+ # to +nil+.
226
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
227
+ # +"X-API-Key"+.
236
228
  #
237
229
  # @return [RestClient::Response] The +Response+ resulting from the execution of the PATCH call.
238
230
  #
239
- def self.execute_patch_unauthenticated(env, endpoint, uri_params, data)
240
- return execute_rest_call(env, endpoint, data, nil, nil, uri_params) do |env, url, data, headers|
241
- if env.scheme == 'https'
242
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
243
- .patch(data.to_json, headers)
244
- else
245
- next RestClient.patch(url.to_s, data.to_json, headers)
246
- end
231
+ def self.execute_patch_unauthenticated(env, endpoint, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
232
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
233
+
234
+ return execute_rest_call(env, endpoint, data, nil, composite_headers, uri_params) do |env, url, data, headers|
235
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).patch(data.to_json, headers)
247
236
  end
248
237
  end
249
238
 
@@ -256,18 +245,20 @@ module Bubbles
256
245
  # placed into a Authorization header.
257
246
  # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
258
247
  # @param [Hash] data A +Hash+ of key-value pairs that will be sent in the body of the http request.
248
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
249
+ # call. Defaults to an empty +Hash+.
250
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
251
+ # to +nil+.
252
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
253
+ # +"X-API-Key"+.
259
254
  #
260
255
  # @return [RestClient::Response] The +Response+ resulting from the execution of the PUT call.
261
256
  #
262
- def self.execute_put_authenticated(env, endpoint, auth_token, uri_params, data)
263
- return execute_rest_call(env, endpoint, data, auth_token, nil, uri_params) do |env, url, data, headers|
264
- if env.scheme == 'https'
265
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
266
- .put(data.to_json, headers)
257
+ def self.execute_put_authenticated(env, endpoint, auth_token, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
258
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
267
259
 
268
- else
269
- next RestClient.put(url.to_s, data.to_json, headers)
270
- end
260
+ return execute_rest_call(env, endpoint, data, auth_token, composite_headers, uri_params) do |env, url, data, headers|
261
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).put(data.to_json, headers)
271
262
  end
272
263
  end
273
264
 
@@ -278,17 +269,20 @@ module Bubbles
278
269
  # @param [Endpoint] endpoint The +Endpoint+ which should be requested
279
270
  # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
280
271
  # @param [Hash] data A +Hash+ of key-value pairs that will be sent in the body of the http request.
272
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
273
+ # call. Defaults to an empty +Hash+.
274
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
275
+ # to +nil+.
276
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
277
+ # +"X-API-Key"+.
281
278
  #
282
279
  # @return [RestClient::Response] The +Response+ resulting from the execution of the PUT call.
283
280
  #
284
- def self.execute_put_unauthenticated(env, endpoint, uri_params, data)
285
- return execute_rest_call(env, endpoint, data, nil, nil, uri_params) do |env, url, data, headers|
286
- if env.scheme == 'https'
287
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
288
- .put(data.to_json, headers)
289
- else
290
- next RestClient.put(url.to_s, data.to_json, headers)
291
- end
281
+ def self.execute_put_unauthenticated(env, endpoint, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
282
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
283
+
284
+ return execute_rest_call(env, endpoint, data, nil, composite_headers, uri_params) do |env, url, data, headers|
285
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).put(data.to_json, headers)
292
286
  end
293
287
  end
294
288
 
@@ -300,17 +294,43 @@ module Bubbles
300
294
  # @param [String] auth_token The authorization token retrieved during some former authentication call. Will be
301
295
  # placed into a Authorization header.
302
296
  # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
297
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
298
+ # call. Defaults to an empty +Hash+.
299
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
300
+ # to +nil+.
301
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
302
+ # +"X-API-Key"+.
303
303
  #
304
304
  # @return [RestClient::Response] The +Response+ resulting from the execution of the DELETE call.
305
305
  #
306
- def self.execute_delete_authenticated(env, endpoint, auth_token, uri_params)
307
- execute_rest_call(env, endpoint, nil, auth_token, nil, uri_params) do |env, url, data, headers|
308
- if env.scheme == 'https'
309
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
310
- .delete(headers)
311
- else
312
- next RestClient.delete(url.to_s, headers)
313
- end
306
+ def self.execute_delete_authenticated(env, endpoint, auth_token, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
307
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
308
+
309
+ execute_rest_call(env, endpoint, nil, auth_token, composite_headers, uri_params) do |env, url, data, headers|
310
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).delete(headers)
311
+ end
312
+ end
313
+
314
+ ##
315
+ # Execute a DELETE request without authentication.
316
+ #
317
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
318
+ # @param [Endpoint] endpoint The +Endpoint+ which should be requested
319
+ # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
320
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
321
+ # call. Defaults to an empty +Hash+.
322
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
323
+ # to +nil+.
324
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
325
+ # +"X-API-Key"+.
326
+ #
327
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the DELETE call.
328
+ #
329
+ def self.execute_delete_unauthenticated(env, endpoint, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-Api-Key')
330
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
331
+
332
+ execute_rest_call(env, endpoint, nil, nil, composite_headers, uri_params) do |env, url, data, headers|
333
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).delete(headers)
314
334
  end
315
335
  end
316
336
 
@@ -333,8 +353,81 @@ module Bubbles
333
353
  # self.local_environment
334
354
  # end
335
355
 
356
+ ##
357
+ # Build a set of headers from two existing sets.
358
+ #
359
+ # This takes two sets of headers, as +Hash+es and merges them to create a single +Hash+ with all of the members of
360
+ # the previous two.
361
+ #
362
+ # @param [Hash] headers A set of existing headers.
363
+ # @param [Hash] additional_headers Another set of headers
364
+ #
365
+ # @return A +Hash+ containing all of the members of the input parameters. Key conflicts will be resolved to the
366
+ # benefit of +additional_headers+, meaning that whatever is in the value of the key within the
367
+ # +additional_headers+ +Hash+ will be used.
368
+ #
369
+ def self.build_composite_headers(headers, additional_headers)
370
+ composite_headers = headers
371
+
372
+ unless additional_headers.empty?
373
+ additional_headers.each { |nextHeader|
374
+ composite_headers[nextHeader[0]] = nextHeader[1]
375
+ }
376
+ end
377
+
378
+ composite_headers
379
+ end
380
+
381
+ ##
382
+ # Retrieve an encoded authorization (username and password) for a given
383
+ # +Endpoint+ and data set.
384
+ #
385
+ # @param endpoint The +Endpoint+ that this authorization will be used for.
386
+ # @param data A set of elements (typically username and password) that
387
+ # should be encoded.
388
+ #
389
+ # @return A +String+ containing an encoding of the values passed in as
390
+ # +data+, concatenated with a colon.
391
+ #
392
+ def self.get_encoded_authorization(endpoint, data)
393
+ count = 0
394
+ auth_value = ''
395
+ endpoint.encode_authorization.each { |auth_key|
396
+ if data[auth_key]
397
+ if count > 0
398
+ auth_value = auth_value + ':' + data[auth_key]
399
+ else
400
+ auth_value = data[auth_key]
401
+ end
402
+
403
+ count = count + 1
404
+
405
+ data.delete(auth_key)
406
+ end
407
+ }
408
+
409
+ auth_value
410
+ end
411
+
336
412
  private
337
413
 
414
+ def self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers = {})
415
+ if api_key and endpoint.api_key_required?
416
+ composite_headers = RestClientResources.build_composite_headers(additional_headers, {
417
+ api_key_name.to_s => api_key,
418
+ :content_type => :json,
419
+ :accept => :json
420
+ })
421
+ else
422
+ composite_headers = RestClientResources.build_composite_headers(additional_headers, {
423
+ :content_type => :json,
424
+ :accept => :json
425
+ })
426
+ end
427
+
428
+ composite_headers
429
+ end
430
+
338
431
  ##
339
432
  # Execute a REST call to the API.
340
433
  #
@@ -354,8 +447,12 @@ module Bubbles
354
447
  # will return an +OpenStruct+; otherwise, the +Response+ will be returned.
355
448
  #
356
449
  def self.execute_rest_call(env, endpoint, data, auth_token, headers, uri_params = {}, &block)
450
+ unless headers
451
+ raise ArgumentError.new('Expected headers to be non-nil')
452
+ end
453
+
357
454
  unless block
358
- raise ArgumentError('This method requires that a block is given.')
455
+ raise ArgumentError.new('This method requires that a block is given')
359
456
  end
360
457
 
361
458
  url = endpoint.get_expanded_url env, uri_params
@@ -365,23 +462,19 @@ module Bubbles
365
462
  data = {}
366
463
  end
367
464
 
368
- if headers == nil
369
- headers = {
370
- :content_type => :json
371
- }
372
- else
373
- headers[:content_type] = :json
374
- end
375
-
376
465
  unless auth_token == nil
377
466
  headers[:authorization] = 'Bearer ' + auth_token
378
467
  end
379
468
 
380
- headers[:accept] = :json
381
-
382
469
  response = block.call(env, url, data, headers)
383
- rescue Errno::ECONNREFUSED
470
+
471
+ rescue *[SocketError, Errno::ECONNREFUSED]
384
472
  response = { :error => 'Unable to connect to host ' + env.host.to_s + ':' + env.port.to_s }.to_json
473
+ if endpoint.return_type == :body_as_object
474
+ response = JSON.parse(response, object_class: OpenStruct)
475
+ end
476
+
477
+ return response
385
478
  end
386
479
 
387
480
  if endpoint.return_type == :body_as_object and endpoint.method != :head
@@ -393,4 +486,4 @@ module Bubbles
393
486
  response
394
487
  end
395
488
  end
396
- end
489
+ end
@@ -1,6 +1,6 @@
1
1
  module Bubbles
2
2
  class RestEnvironment
3
- attr_accessor :scheme, :host, :port, :api_key
3
+ attr_accessor :host, :port, :api_key, :api_key_name
4
4
 
5
5
  ##
6
6
  # Construct a new instance of +RestEnvironment+.
@@ -9,17 +9,59 @@ module Bubbles
9
9
  # @param [String] host The host to communicate with.
10
10
  # @param [Integer] port The port on which the communication channel should operate.
11
11
  # @param [String] api_key (Optional) The API key to use to identify your client with the API. Defaults to +nil+.
12
+ # @param [String] api_key_name (Optional) The name of the header that will specify the API key. Defaults to +"X-API-Key"+.
12
13
  #
13
- def initialize(scheme='https', host='api.foamfactory.com', port=443, api_key=nil)
14
+ def initialize(scheme='https', host='api.foamfactory.com', port=443, api_key=nil, api_key_name='X-API-Key')
14
15
  @scheme = scheme
15
16
  @port = port
16
17
 
17
- if @scheme == 'http' && @port == 443
18
+ if @scheme == 'http' && @port == nil
18
19
  @port = 80
20
+ elsif @scheme == 'https' && @port == nil
21
+ @port = 443
19
22
  end
20
23
 
21
24
  @host = host
22
25
  @api_key = api_key
26
+ @api_key_name = api_key_name
27
+ end
28
+
29
+ ##
30
+ # Retrieve the name of the API key to be used.
31
+ #
32
+ # This will be the "key" portion of the key-value of the API key header.
33
+ #
34
+ # @return [String] The API key name, if set; "X-API-Key", otherwise.
35
+ #
36
+ def api_key_name
37
+ @api_key_name
38
+ end
39
+
40
+ ##
41
+ # Retrieve an API key from this +RestEnvironment+, but only if a specific +Endpoint+ requires it.
42
+ #
43
+ # If an +Endpoint+ has +api_key_required+ set to +true+, this method will return the API for the current
44
+ # +RestEnvironment+. If not, then it will return +nil+, rather than just blindly returning the API key for every
45
+ # possible retrieval, even if the +Endpoint+ doesn't require it.
46
+ #
47
+ # @return [String] The API key for this +RestEnvironment+, if the specified +Endpoint+ requires it; +nil+,
48
+ # otherwise.
49
+ #
50
+ def get_api_key_if_needed(endpoint)
51
+ if endpoint.api_key_required?
52
+ @api_key
53
+ else
54
+ nil
55
+ end
56
+ end
57
+
58
+ ##
59
+ # Retrieve the scheme of the current +RestEnvironment+, as a +Symbol+.
60
+ #
61
+ # @return [Symbol] The scheme of the current +RestEnvironment+, as a +Symbol+.
62
+ #
63
+ def scheme
64
+ @scheme.to_s
23
65
  end
24
66
  end
25
67
  end
@@ -7,7 +7,7 @@ module Bubbles
7
7
  end
8
8
 
9
9
  def self.version_name
10
- '0.3.1'
10
+ '0.7.0'
11
11
  end
12
12
 
13
13
  def self.version_code
data/lib/bubbles.rb CHANGED
@@ -10,11 +10,9 @@ require 'json'
10
10
 
11
11
  module Bubbles
12
12
  class Resources < RestClientResources
13
- def initialize
14
- # def initialize(env, api_key)
15
- # @environment = get_environment env
16
- # @api_key = api_key
17
- # @auth_token = nil
13
+ def initialize(api_key='')
14
+ super
15
+
18
16
  @packageName = Bubbles::VersionInformation.package_name
19
17
  @versionName = Bubbles::VersionInformation.version_name
20
18
  @versionCode = Bubbles::VersionInformation.version_code
@@ -29,4 +27,3 @@ module Bubbles
29
27
  end
30
28
  end
31
29
  end
32
-