bubbles-rest-client 0.2.0 → 0.6.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.
@@ -74,13 +74,14 @@ module Bubbles
74
74
  # @param [Array<Symbol>] encode_authorization Parameters that should be treated as authorization parameters and
75
75
  # encoded using a Base64 encoding.
76
76
  #
77
- def initialize(method, location, auth_required = false, api_key_required = false, name = nil, return_type = :body_as_string, encode_authorization = {})
77
+ def initialize(method, location, auth_required = false, api_key_required = false, name = nil, return_type = :body_as_string, encode_authorization = {}, headers = {})
78
78
  @method = method
79
79
  @location = location
80
80
  @auth_required = auth_required
81
81
  @api_key_required = api_key_required
82
82
  @name = name
83
83
  @encode_authorization = encode_authorization
84
+ @additional_headers = headers
84
85
 
85
86
  unless Endpoint::RETURN_TYPES.include? return_type.to_s
86
87
  return_type = :body_as_string
@@ -145,7 +146,8 @@ module Bubbles
145
146
  #
146
147
  # @param [RestEnvironment] env The +RestEnvironment+ to use to access this +Endpoint+.
147
148
  #
148
- # @return [String] A +String+ containing the full URL to access this +Endpoint+ on the given {RestEnvironment}.
149
+ # @return [Addressable::URI] An +Addressable::URI+ containing the full URL to access this +Endpoint+ on the given
150
+ # +RestEnvironment+.
149
151
  #
150
152
  def get_expanded_url(env, uri_params = {})
151
153
  url = get_base_url env
@@ -235,7 +237,7 @@ module Bubbles
235
237
  # for the +Endpoint+, to be defined on {RestClientResources}; false, otherwise.
236
238
  #
237
239
  def name?
238
- @name == nil
240
+ @name != nil
239
241
  end
240
242
 
241
243
  ##
@@ -268,5 +270,17 @@ module Bubbles
268
270
  def has_uri_params?
269
271
  !@uri_params.empty?
270
272
  end
273
+
274
+ def additional_headers
275
+ unless @additional_headers
276
+ @additional_headers = {}
277
+ end
278
+
279
+ @additional_headers
280
+ end
281
+
282
+ def has_additional_headers?
283
+ not additional_headers.empty?
284
+ end
271
285
  end
272
286
  end
@@ -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
 
@@ -55,17 +53,20 @@ module Bubbles
55
53
  # @param [Endpoint] endpoint The +Endpoint+ which should be requested
56
54
  # @param [String] auth_token The authorization token to use for authentication.
57
55
  # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
56
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
57
+ # call. Defaults to an empty +Hash+.
58
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
59
+ # to +nil+.
60
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
61
+ # +"X-API-Key"+.
58
62
  #
59
63
  # @return [RestClient::Response] The +Response+ resulting from the execution of the GET call.
60
64
  #
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
65
+ def self.execute_get_authenticated(env, endpoint, auth_token, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
66
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
67
+
68
+ execute_rest_call(env, endpoint, nil, auth_token, composite_headers, uri_params) do |env, url, data, headers|
69
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).get(headers)
69
70
  end
70
71
  end
71
72
 
@@ -76,17 +77,21 @@ module Bubbles
76
77
  #
77
78
  # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
78
79
  # @param [Endpoint] endpoint The +Endpoint+ which should be requested
80
+ # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
81
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
82
+ # call. Defaults to an empty +Hash+.
83
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
84
+ # to +nil+.
85
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
86
+ # +"X-API-Key"+.
79
87
  #
80
88
  # @return [RestClient::Response] The +Response+ resulting from the execution of the GET call.
81
89
  #
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
90
+ def self.execute_head_unauthenticated(env, endpoint, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
91
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
92
+
93
+ execute_rest_call(env, endpoint, nil, nil, composite_headers, uri_params) do |env, url, data, headers|
94
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).head(headers)
90
95
  end
91
96
  end
92
97
 
@@ -100,17 +105,20 @@ module Bubbles
100
105
  # @param [Endpoint] endpoint The +Endpoint+ which should be requested
101
106
  # @param [String] auth_token The authorization token to use for authentication.
102
107
  # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
108
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
109
+ # call. Defaults to an empty +Hash+.
110
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
111
+ # to +nil+.
112
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
113
+ # +"X-API-Key"+.
103
114
  #
104
- # @return [RestClient::Response] The +Response+ resulting from the execution of the GET call.
115
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the HEAD call.
105
116
  #
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
117
+ def self.execute_head_authenticated(env, endpoint, auth_token, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
118
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
119
+
120
+ execute_rest_call(env, endpoint, nil, auth_token, composite_headers, uri_params) do |env, url, data, headers|
121
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).head(headers)
114
122
  end
115
123
  end
116
124
 
@@ -121,29 +129,20 @@ module Bubbles
121
129
  # @param [Endpoint] endpoint The +Endpoint+ which should be requested
122
130
  # @param [String] api_key The API key to use to process the request. Will be placed in an 'X-API-KEY' header.
123
131
  # @param [Hash] data A +Hash+ of key-value pairs that will be sent in the body of the http request.
124
- # @param [Hash] headers (Optional) A +Hash+ of key-value pairs that will be sent as HTTP headers as part of the
125
- # request. Defaults to +nil+.
132
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
133
+ # call. Defaults to an empty +Hash+.
134
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
135
+ # to +nil+.
136
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
137
+ # +"X-API-Key"+.
126
138
  #
127
139
  # @return [RestClient::Response] The +Response+ resulting from the execution of the POST call.
128
140
  #
129
- def self.execute_post_with_api_key(env, endpoint, api_key, data, headers=nil)
130
- additional_headers = {
131
- 'X-Api-Key' => api_key
132
- }
133
-
134
- unless headers.nil?
135
- headers.each { |nextHeader|
136
- additional_headers[nextHeader[0]] = nextHeader[1]
137
- }
138
- end
141
+ def self.execute_post_unauthenticated(env, endpoint, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
142
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
139
143
 
140
- execute_rest_call(env, endpoint, data, nil, additional_headers) do |env, url, data, headers|
141
- if env.scheme == 'https'
142
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
143
- .post(data.to_json, additional_headers)
144
- else
145
- next RestClient.post url.to_s, data.to_json, additional_headers
146
- end
144
+ execute_rest_call(env, endpoint, data, nil, composite_headers) do |env, url, data, headers|
145
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).post(data.to_json, headers)
147
146
  end
148
147
  end
149
148
 
@@ -155,18 +154,20 @@ module Bubbles
155
154
  # @param [String] auth_token The authorization token retrieved during some former authentication call. Will be
156
155
  # placed into a Authorization header.
157
156
  # @param [Hash] data A +Hash+ of key-value pairs that will be sent in the body of the http request.
157
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
158
+ # call. Defaults to an empty +Hash+.
159
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
160
+ # to +nil+.
161
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
162
+ # +"X-API-Key"+.
158
163
  #
159
164
  # @return [RestClient::Response] The +Response+ resulting from the execution of the POST call.
160
165
  #
161
- def self.execute_post_authenticated(env, endpoint, auth_token, data)
162
- return execute_rest_call(env, endpoint, data, auth_token, nil) do |env, url, data, headers|
163
- if env.scheme == 'https'
164
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
165
- .post(data.to_json, headers)
166
+ def self.execute_post_authenticated(env, endpoint, auth_token, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
167
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
166
168
 
167
- else
168
- next RestClient.post(url.to_s, data.to_json, headers)
169
- end
169
+ return execute_rest_call(env, endpoint, data, auth_token, composite_headers) do |env, url, data, headers|
170
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).post(data.to_json, headers)
170
171
  end
171
172
  end
172
173
 
@@ -179,18 +180,44 @@ module Bubbles
179
180
  # placed into a Authorization header.
180
181
  # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
181
182
  # @param [Hash] data A +Hash+ of key-value pairs that will be sent in the body of the http request.
183
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
184
+ # call. Defaults to an empty +Hash+.
185
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
186
+ # to +nil+.
187
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
188
+ # +"X-API-Key"+.
182
189
  #
183
190
  # @return [RestClient::Response] The +Response+ resulting from the execution of the PATCH call.
184
191
  #
185
- def self.execute_patch_authenticated(env, endpoint, auth_token, uri_params, data)
186
- return execute_rest_call(env, endpoint, data, auth_token, nil, uri_params) do |env, url, data, headers|
187
- if env.scheme == 'https'
188
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
189
- .patch(data.to_json, headers)
192
+ def self.execute_patch_authenticated(env, endpoint, auth_token, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
193
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
190
194
 
191
- else
192
- next RestClient.patch(url.to_s, data.to_json, headers)
193
- end
195
+ return execute_rest_call(env, endpoint, data, auth_token, composite_headers, uri_params) do |env, url, data, headers|
196
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).patch(data.to_json, headers)
197
+ end
198
+ end
199
+
200
+ ##
201
+ # Execute a PATCH request without authentication.
202
+ #
203
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
204
+ # @param [Endpoint] endpoint The +Endpoint+ which should be requested
205
+ # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
206
+ # @param [Hash] data A +Hash+ of key-value pairs that will be sent in the body of the http request.
207
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
208
+ # call. Defaults to an empty +Hash+.
209
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
210
+ # to +nil+.
211
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
212
+ # +"X-API-Key"+.
213
+ #
214
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the PATCH call.
215
+ #
216
+ def self.execute_patch_unauthenticated(env, endpoint, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
217
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
218
+
219
+ return execute_rest_call(env, endpoint, data, nil, composite_headers, uri_params) do |env, url, data, headers|
220
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).patch(data.to_json, headers)
194
221
  end
195
222
  end
196
223
 
@@ -203,18 +230,44 @@ module Bubbles
203
230
  # placed into a Authorization header.
204
231
  # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
205
232
  # @param [Hash] data A +Hash+ of key-value pairs that will be sent in the body of the http request.
233
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
234
+ # call. Defaults to an empty +Hash+.
235
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
236
+ # to +nil+.
237
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
238
+ # +"X-API-Key"+.
206
239
  #
207
240
  # @return [RestClient::Response] The +Response+ resulting from the execution of the PUT call.
208
241
  #
209
- def self.execute_put_authenticated(env, endpoint, auth_token, uri_params, data)
210
- return execute_rest_call(env, endpoint, data, auth_token, nil, uri_params) do |env, url, data, headers|
211
- if env.scheme == 'https'
212
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
213
- .put(data.to_json, headers)
242
+ def self.execute_put_authenticated(env, endpoint, auth_token, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
243
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
214
244
 
215
- else
216
- next RestClient.put(url.to_s, data.to_json, headers)
217
- end
245
+ return execute_rest_call(env, endpoint, data, auth_token, composite_headers, uri_params) do |env, url, data, headers|
246
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).put(data.to_json, headers)
247
+ end
248
+ end
249
+
250
+ ##
251
+ # Execute a PUT request without authentication.
252
+ #
253
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
254
+ # @param [Endpoint] endpoint The +Endpoint+ which should be requested
255
+ # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
256
+ # @param [Hash] data A +Hash+ of key-value pairs that will be sent in the body of the http request.
257
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
258
+ # call. Defaults to an empty +Hash+.
259
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
260
+ # to +nil+.
261
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
262
+ # +"X-API-Key"+.
263
+ #
264
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the PUT call.
265
+ #
266
+ def self.execute_put_unauthenticated(env, endpoint, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
267
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
268
+
269
+ return execute_rest_call(env, endpoint, data, nil, composite_headers, uri_params) do |env, url, data, headers|
270
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).put(data.to_json, headers)
218
271
  end
219
272
  end
220
273
 
@@ -226,17 +279,43 @@ module Bubbles
226
279
  # @param [String] auth_token The authorization token retrieved during some former authentication call. Will be
227
280
  # placed into a Authorization header.
228
281
  # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
282
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
283
+ # call. Defaults to an empty +Hash+.
284
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
285
+ # to +nil+.
286
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
287
+ # +"X-API-Key"+.
229
288
  #
230
289
  # @return [RestClient::Response] The +Response+ resulting from the execution of the DELETE call.
231
290
  #
232
- def self.execute_delete_authenticated(env, endpoint, auth_token, uri_params)
233
- execute_rest_call(env, endpoint, nil, auth_token, nil, uri_params) do |env, url, data, headers|
234
- if env.scheme == 'https'
235
- next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
236
- .delete(headers)
237
- else
238
- next RestClient.delete(url.to_s, headers)
239
- end
291
+ def self.execute_delete_authenticated(env, endpoint, auth_token, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
292
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
293
+
294
+ execute_rest_call(env, endpoint, nil, auth_token, composite_headers, uri_params) do |env, url, data, headers|
295
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).delete(headers)
296
+ end
297
+ end
298
+
299
+ ##
300
+ # Execute a DELETE request without authentication.
301
+ #
302
+ # @param [RestEnvironment] env The +RestEnvironment+ to use to execute the request
303
+ # @param [Endpoint] endpoint The +Endpoint+ which should be requested
304
+ # @param [Hash] uri_params A +Hash+ of identifiers to values to replace in the URI string.
305
+ # @param [Hash] additional_headers A +Hash+ of key-value pairs that will be sent as additional headers in the API
306
+ # call. Defaults to an empty +Hash+.
307
+ # @param [String] api_key (Optional) The API key to use to send to the host for unauthenticated requests. Defaults
308
+ # to +nil+.
309
+ # @param [String] api_key_name (Optional) The name of the header in which to send the API key. Defaults to
310
+ # +"X-API-Key"+.
311
+ #
312
+ # @return [RestClient::Response] The +Response+ resulting from the execution of the DELETE call.
313
+ #
314
+ def self.execute_delete_unauthenticated(env, endpoint, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-Api-Key')
315
+ composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
316
+
317
+ execute_rest_call(env, endpoint, nil, nil, composite_headers, uri_params) do |env, url, data, headers|
318
+ next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).delete(headers)
240
319
  end
241
320
  end
242
321
 
@@ -259,8 +338,81 @@ module Bubbles
259
338
  # self.local_environment
260
339
  # end
261
340
 
341
+ ##
342
+ # Build a set of headers from two existing sets.
343
+ #
344
+ # This takes two sets of headers, as +Hash+es and merges them to create a single +Hash+ with all of the members of
345
+ # the previous two.
346
+ #
347
+ # @param [Hash] headers A set of existing headers.
348
+ # @param [Hash] additional_headers Another set of headers
349
+ #
350
+ # @return A +Hash+ containing all of the members of the input parameters. Key conflicts will be resolved to the
351
+ # benefit of +additional_headers+, meaning that whatever is in the value of the key within the
352
+ # +additional_headers+ +Hash+ will be used.
353
+ #
354
+ def self.build_composite_headers(headers, additional_headers)
355
+ composite_headers = headers
356
+
357
+ unless additional_headers.empty?
358
+ additional_headers.each { |nextHeader|
359
+ composite_headers[nextHeader[0]] = nextHeader[1]
360
+ }
361
+ end
362
+
363
+ composite_headers
364
+ end
365
+
366
+ ##
367
+ # Retrieve an encoded authorization (username and password) for a given
368
+ # +Endpoint+ and data set.
369
+ #
370
+ # @param endpoint The +Endpoint+ that this authorization will be used for.
371
+ # @param data A set of elements (typically username and password) that
372
+ # should be encoded.
373
+ #
374
+ # @return A +String+ containing an encoding of the values passed in as
375
+ # +data+, concatenated with a colon.
376
+ #
377
+ def self.get_encoded_authorization(endpoint, data)
378
+ count = 0
379
+ auth_value = ''
380
+ endpoint.encode_authorization.each { |auth_key|
381
+ if data[auth_key]
382
+ if count > 0
383
+ auth_value = auth_value + ':' + data[auth_key]
384
+ else
385
+ auth_value = data[auth_key]
386
+ end
387
+
388
+ count = count + 1
389
+
390
+ data.delete(auth_key)
391
+ end
392
+ }
393
+
394
+ auth_value
395
+ end
396
+
262
397
  private
263
398
 
399
+ def self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers = {})
400
+ if api_key and endpoint.api_key_required?
401
+ composite_headers = RestClientResources.build_composite_headers(additional_headers, {
402
+ api_key_name.to_s => api_key,
403
+ :content_type => :json,
404
+ :accept => :json
405
+ })
406
+ else
407
+ composite_headers = RestClientResources.build_composite_headers(additional_headers, {
408
+ :content_type => :json,
409
+ :accept => :json
410
+ })
411
+ end
412
+
413
+ composite_headers
414
+ end
415
+
264
416
  ##
265
417
  # Execute a REST call to the API.
266
418
  #
@@ -280,8 +432,12 @@ module Bubbles
280
432
  # will return an +OpenStruct+; otherwise, the +Response+ will be returned.
281
433
  #
282
434
  def self.execute_rest_call(env, endpoint, data, auth_token, headers, uri_params = {}, &block)
435
+ unless headers
436
+ raise ArgumentError.new('Expected headers to be non-nil')
437
+ end
438
+
283
439
  unless block
284
- raise ArgumentError('This method requires that a block is given.')
440
+ raise ArgumentError.new('This method requires that a block is given')
285
441
  end
286
442
 
287
443
  url = endpoint.get_expanded_url env, uri_params
@@ -291,23 +447,19 @@ module Bubbles
291
447
  data = {}
292
448
  end
293
449
 
294
- if headers == nil
295
- headers = {
296
- :content_type => :json
297
- }
298
- else
299
- headers[:content_type] = :json
300
- end
301
-
302
450
  unless auth_token == nil
303
451
  headers[:authorization] = 'Bearer ' + auth_token
304
452
  end
305
453
 
306
- headers[:accept] = :json
307
-
308
454
  response = block.call(env, url, data, headers)
309
- rescue Errno::ECONNREFUSED
455
+
456
+ rescue *[SocketError, Errno::ECONNREFUSED]
310
457
  response = { :error => 'Unable to connect to host ' + env.host.to_s + ':' + env.port.to_s }.to_json
458
+ if endpoint.return_type == :body_as_object
459
+ response = JSON.parse(response, object_class: OpenStruct)
460
+ end
461
+
462
+ return response
311
463
  end
312
464
 
313
465
  if endpoint.return_type == :body_as_object and endpoint.method != :head
@@ -319,4 +471,4 @@ module Bubbles
319
471
  response
320
472
  end
321
473
  end
322
- end
474
+ end