bubblez 1.0.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.
@@ -0,0 +1,489 @@
1
+ require 'bubblez/config'
2
+ require 'bubblez/rest_environment'
3
+
4
+ module Bubblez
5
+ class RestClientResources
6
+ def environment(env_name = nil)
7
+ Bubblez.configuration.environment(env_name)
8
+ end
9
+
10
+ ##
11
+ # Create a new instance of +RestClientResources+.
12
+ #
13
+ # @param env The +RestEnvironment+ that should be used for this set of resources.
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"+.
18
+ #
19
+ def initialize(env, api_key = nil, api_key_name='X-API-Key')
20
+ unless api_key
21
+ api_key = ''
22
+ end
23
+
24
+ @api_key = api_key
25
+ @auth_token = nil
26
+ @api_key_name = api_key_name
27
+ end
28
+
29
+ ##
30
+ # Execute a GET request without authentication.
31
+ #
32
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
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+.
36
+ #
37
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the GET call.
38
+ #
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)
44
+ end
45
+ end
46
+
47
+ ##
48
+ # Execute a GET request with authentication.
49
+ #
50
+ # Currently, only Authorization: Bearer is supported.
51
+ #
52
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
53
+ # @param [Endpoint] endpoint The +Endpoint+ which should be requested
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.
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"+.
63
+ #
64
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the GET call.
65
+ #
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)
78
+ end
79
+ end
80
+
81
+ ##
82
+ # Execute a HEAD request without authentication.
83
+ #
84
+ # This is the same as a GET request, but will only return headers and not the response body.
85
+ #
86
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
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"+.
95
+ #
96
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the GET call.
97
+ #
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)
103
+ end
104
+ end
105
+
106
+ ##
107
+ # Execute a HEAD request with authentication.
108
+ #
109
+ # Currently, only Authorization: Bearer is supported. This is the same as a GET request, but will only return
110
+ # headers and not the response body.
111
+ #
112
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
113
+ # @param [Endpoint] endpoint The +Endpoint+ which should be requested
114
+ # @param [String] auth_token The authorization token to use for authentication.
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"+.
122
+ #
123
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the HEAD call.
124
+ #
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)
127
+
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)
130
+ end
131
+ end
132
+
133
+ ##
134
+ # Execute a POST request without authentication, but requiring an API key.
135
+ #
136
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
137
+ # @param [Endpoint] endpoint The +Endpoint+ which should be requested
138
+ # @param [String] api_key The API key to use to process the request. Will be placed in an 'X-API-KEY' header.
139
+ # @param [Hash] data A +Hash+ of key-value pairs that will be sent in the body of the http request.
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"+.
146
+ #
147
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the POST call.
148
+ #
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)
151
+
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)
154
+ end
155
+ end
156
+
157
+ ##
158
+ # Execute a POST request with authentication in the form of an authorization token.
159
+ #
160
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
161
+ # @param [Endpoint] endpoint The +Endpoint+ which should be requested
162
+ # @param [String] auth_token The authorization token retrieved during some former authentication call. Will be
163
+ # placed into a Authorization header.
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"+.
171
+ #
172
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the POST call.
173
+ #
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
183
+
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)
186
+ end
187
+ end
188
+
189
+ ##
190
+ # Execute a PATCH request with authentication in the form of an authorization token.
191
+ #
192
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
193
+ # @param [Endpoint] endpoint The +Endpoint+ which should be requested
194
+ # @param [String] auth_token The authorization token retrieved during some former authentication call. Will be
195
+ # placed into a Authorization header.
196
+ # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
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"+.
204
+ #
205
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the PATCH call.
206
+ #
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)
209
+
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)
212
+ end
213
+ end
214
+
215
+ ##
216
+ # Execute a PATCH request without authentication.
217
+ #
218
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
219
+ # @param [Endpoint] endpoint The +Endpoint+ which should be requested
220
+ # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
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"+.
228
+ #
229
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the PATCH call.
230
+ #
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)
236
+ end
237
+ end
238
+
239
+ ##
240
+ # Execute a PUT request with authentication in the form of an authorization token.
241
+ #
242
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
243
+ # @param [Endpoint] endpoint The +Endpoint+ which should be requested
244
+ # @param [String] auth_token The authorization token retrieved during some former authentication call. Will be
245
+ # placed into a Authorization header.
246
+ # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
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"+.
254
+ #
255
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the PUT call.
256
+ #
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)
259
+
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)
262
+ end
263
+ end
264
+
265
+ ##
266
+ # Execute a PUT request without authentication.
267
+ #
268
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
269
+ # @param [Endpoint] endpoint The +Endpoint+ which should be requested
270
+ # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
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"+.
278
+ #
279
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the PUT call.
280
+ #
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)
286
+ end
287
+ end
288
+
289
+ ##
290
+ # Execute a DELETE request with authentication in the form of an authorization token.
291
+ #
292
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
293
+ # @param [Endpoint] endpoint The +Endpoint+ which should be requested
294
+ # @param [String] auth_token The authorization token retrieved during some former authentication call. Will be
295
+ # placed into a Authorization header.
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
+ #
304
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the DELETE call.
305
+ #
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)
334
+ end
335
+ end
336
+
337
+ ##
338
+ # Retrieve the {RestEnvironment} to utilize from a {Symbol} describing it.
339
+ #
340
+ # @param [Symbol] environment A {Symbol} describing the environment to use. Must be one of:
341
+ # [:production, :staging, :local, nil]. If +nil+, note that +:production+ will be used.
342
+ #
343
+ # @return [RestEnvironment] The {RestEnvironment} corresponding to the given {Symbol}.
344
+ #
345
+ # def get_environment(environment)
346
+ # if !environment || environment == :production
347
+ # return self.production_environment
348
+ # elsif environment == :staging
349
+ # return self.staging_environment
350
+ # end
351
+ #
352
+ #
353
+ # self.local_environment
354
+ # end
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
+
412
+ private
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
+
431
+ ##
432
+ # Execute a REST call to the API.
433
+ #
434
+ # This is the workhorse of the +RestClientResources+ class. It performs the necessary setup of headers and the HTTP
435
+ # request, and then executes the remote API call.
436
+ #
437
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to make this API call. Must not be +nil+.
438
+ # @param [Endpoint] The +Endpoint+ to call. Must not be +nil+.
439
+ # @param [Hash] The body of the request. May be +nil+ or empty for requests not requiring a body.
440
+ # @param [String] auth_token The authorization token used to authenticate to the API. May be +nil+ for requests that
441
+ # don't require authentication.
442
+ # @param [Hash] headers A +Hash+ of key-value pairs to add to the HTTP request as headers. May be +nil+ if none are
443
+ # required.
444
+ # @param [Block] block The block to execute that actually performs the HTTP request.
445
+ #
446
+ # @return [RestClient::Response|OpenStruct] If "expect_json" is enabled for the +Endpoint+ being executed, then this
447
+ # will return an +OpenStruct+; otherwise, the +Response+ will be returned.
448
+ #
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
+
454
+ unless block
455
+ raise ArgumentError.new('This method requires that a block is given')
456
+ end
457
+
458
+ url = endpoint.get_expanded_url env, uri_params
459
+
460
+ begin
461
+ if data == nil
462
+ data = {}
463
+ end
464
+
465
+ unless auth_token == nil
466
+ headers[:authorization] = 'Bearer ' + auth_token
467
+ end
468
+
469
+ response = block.call(env, url, data, headers)
470
+
471
+ rescue *[SocketError, Errno::ECONNREFUSED]
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
478
+ end
479
+
480
+ if endpoint.return_type == :body_as_object and endpoint.method != :head
481
+ return JSON.parse(response, object_class: OpenStruct)
482
+ elsif endpoint.return_type == :body_as_string and endpoint.method != :head
483
+ return response.body
484
+ end
485
+
486
+ response
487
+ end
488
+ end
489
+ end
@@ -0,0 +1,67 @@
1
+ module Bubblez
2
+ class RestEnvironment
3
+ attr_accessor :host, :port, :api_key, :api_key_name
4
+
5
+ ##
6
+ # Construct a new instance of +RestEnvironment+.
7
+ #
8
+ # @param [String] scheme The scheme to use for communicating with the host. Currently, http and https are supported.
9
+ # @param [String] host The host to communicate with.
10
+ # @param [Integer] port The port on which the communication channel should operate.
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"+.
13
+ #
14
+ def initialize(scheme='https', host='api.foamfactory.com', port=443, api_key=nil, api_key_name='X-API-Key')
15
+ @scheme = scheme
16
+ @port = port
17
+
18
+ if @scheme == 'http' && @port == nil
19
+ @port = 80
20
+ elsif @scheme == 'https' && @port == nil
21
+ @port = 443
22
+ end
23
+
24
+ @host = host
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
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,24 @@
1
+ require 'date'
2
+
3
+ module Bubblez
4
+ class VersionInformation
5
+ def self.package_name
6
+ return 'bubblez'
7
+ end
8
+
9
+ def self.version_name
10
+ '1.0.0'
11
+ end
12
+
13
+ def self.version_code
14
+ get_code_from_version_name version_name
15
+ end
16
+
17
+ def self.get_code_from_version_name(name)
18
+ # The version code should be the patch version * 1 + the minor version * 2 + the major version * 4
19
+ splitVersion = name.split('.')
20
+
21
+ 4 * splitVersion[0].to_i + 2 * splitVersion[1].to_i + splitVersion[2].to_i
22
+ end
23
+ end
24
+ end
data/lib/bubblez.rb ADDED
@@ -0,0 +1,28 @@
1
+ require "bubblez/version"
2
+ require 'bubblez/config'
3
+ require 'base64'
4
+ require 'bubblez/rest_client_resources'
5
+ require 'bubblez/rest_environment'
6
+ require 'bubblez/version'
7
+ require 'rest-client'
8
+ require 'json'
9
+
10
+ module Bubblez
11
+ class Resources < RestClientResources
12
+ def initialize(api_key='')
13
+ super
14
+
15
+ @package_name = Bubblez::VersionInformation.package_name
16
+ @version_name = Bubblez::VersionInformation.version_name
17
+ @version_code = Bubblez::VersionInformation.version_code
18
+ end
19
+
20
+ def get_version_info
21
+ {
22
+ :name => @package_name,
23
+ :version_name => @version_name,
24
+ :version_code => @version_code
25
+ }
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'simplecov'
3
+ require 'os'
4
+
5
+ namespace :spec do
6
+ desc "Create rspec coverage"
7
+ task :coverage do
8
+ ENV['COVERAGE'] = 'true'
9
+ Rake::Task["spec"].execute
10
+ end
11
+
12
+ namespace :coverage do
13
+ desc 'View coverage information'
14
+ task :view => [:coverage] do
15
+ if OS.mac?
16
+ `open coverage/index.html`
17
+ elsif OS.posix?
18
+ `sensible-browser coverage/index.html`
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ begin
2
+ require 'rspec/core/rake_task'
3
+ RSpec::Core::RakeTask.new(:spec)
4
+ rescue LoadError
5
+ end