Class: Bubbles::RestClientResources

Inherits:
Object
  • Object
show all
Defined in:
lib/bubbles/rest_client_resources.rb

Direct Known Subclasses

Resources

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, api_key = nil, api_key_name = 'X-API-Key') ⇒ RestClientResources

Create a new instance of RestClientResources.

Parameters:

  • env

    The RestEnvironment that should be used for this set of resources.

  • api_key (String) (defaults to: nil)

    (Optional) The API key to use to send to the host for unauthenticated requests. Defaults to nil.

  • api_key_name (String) (defaults to: 'X-API-Key')

    (Optional) The name of the header in which to send the API key. Defaults to “X-API-Key”.



19
20
21
22
23
24
25
26
27
# File 'lib/bubbles/rest_client_resources.rb', line 19

def initialize(env, api_key = nil, api_key_name='X-API-Key')
  unless api_key
    api_key = ''
  end

  @api_key = api_key
  @auth_token = nil
  @api_key_name = api_key_name
end

Class Method Details

.build_composite_headers(headers, additional_headers) ⇒ Object

Build a set of headers from two existing sets.

This takes two sets of headers, as Hashes and merges them to create a single Hash with all of the members of the previous two.

Parameters:

  • headers (Hash)

    A set of existing headers.

  • additional_headers (Hash)

    Another set of headers

Returns:

  • A Hash containing all of the members of the input parameters. Key conflicts will be resolved to the benefit of additional_headers, meaning that whatever is in the value of the key within the additional_headers Hash will be used.



369
370
371
372
373
374
375
376
377
378
379
# File 'lib/bubbles/rest_client_resources.rb', line 369

def self.build_composite_headers(headers, additional_headers)
  composite_headers = headers

  unless additional_headers.empty?
    additional_headers.each { |nextHeader|
      composite_headers[nextHeader[0]] = nextHeader[1]
    }
  end

  composite_headers
end

.execute_delete_authenticated(env, endpoint, auth_token, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key') ⇒ RestClient::Response

Execute a DELETE request with authentication in the form of an authorization token.

Parameters:

  • env (RestEnvironment)

    The RestEnvironment to use to execute the request

  • endpoint (Endpoint)

    The Endpoint which should be requested

  • auth_token (String)

    The authorization token retrieved during some former authentication call. Will be placed into a Authorization header.

  • uri_params (Hash)

    A Hash of identifiers to values to replace in the URI string.

  • additional_headers (Hash) (defaults to: {})

    A Hash of key-value pairs that will be sent as additional headers in the API call. Defaults to an empty Hash.

  • api_key (String) (defaults to: nil)

    (Optional) The API key to use to send to the host for unauthenticated requests. Defaults to nil.

  • api_key_name (String) (defaults to: 'X-API-Key')

    (Optional) The name of the header in which to send the API key. Defaults to “X-API-Key”.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the DELETE call.



306
307
308
309
310
311
312
# File 'lib/bubbles/rest_client_resources.rb', line 306

def self.execute_delete_authenticated(env, endpoint, auth_token, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
  composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)

  execute_rest_call(env, endpoint, nil, auth_token, composite_headers, uri_params) do |env, url, data, headers|
    next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).delete(headers)
  end
end

.execute_delete_unauthenticated(env, endpoint, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-Api-Key') ⇒ RestClient::Response

Execute a DELETE request without authentication.

Parameters:

  • env (RestEnvironment)

    The RestEnvironment to use to execute the request

  • endpoint (Endpoint)

    The Endpoint which should be requested

  • uri_params (Hash)

    A Hash of identifiers to values to replace in the URI string.

  • additional_headers (Hash) (defaults to: {})

    A Hash of key-value pairs that will be sent as additional headers in the API call. Defaults to an empty Hash.

  • api_key (String) (defaults to: nil)

    (Optional) The API key to use to send to the host for unauthenticated requests. Defaults to nil.

  • api_key_name (String) (defaults to: 'X-Api-Key')

    (Optional) The name of the header in which to send the API key. Defaults to “X-API-Key”.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the DELETE call.



329
330
331
332
333
334
335
# File 'lib/bubbles/rest_client_resources.rb', line 329

def self.execute_delete_unauthenticated(env, endpoint, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-Api-Key')
  composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)

  execute_rest_call(env, endpoint, nil, nil, composite_headers, uri_params) do |env, url, data, headers|
    next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).delete(headers)
  end
end

.execute_get_authenticated(env, endpoint, auth_type, auth_value, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key') ⇒ RestClient::Response

Execute a GET request with authentication.

Currently, only Authorization: Bearer is supported.

Parameters:

  • env (RestEnvironment)

    The RestEnvironment to use to execute the request

  • endpoint (Endpoint)

    The Endpoint which should be requested

  • auth_type (Symbol)

    The authorization type to use (Bearer, or Basic)

  • auth_value (String)

    The authorization token OR encoded value (login/password )to use for authentication.

  • uri_params (Hash)

    A Hash of identifiers to values to replace in the URI string.

  • additional_headers (Hash) (defaults to: {})

    A Hash of key-value pairs that will be sent as additional headers in the API call. Defaults to an empty Hash.

  • api_key (String) (defaults to: nil)

    (Optional) The API key to use to send to the host for unauthenticated requests. Defaults to nil.

  • api_key_name (String) (defaults to: 'X-API-Key')

    (Optional) The name of the header in which to send the API key. Defaults to “X-API-Key”.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the GET call.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bubbles/rest_client_resources.rb', line 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')
  if auth_type == :basic
    composite_headers = RestClientResources.build_composite_headers(endpoint.additional_headers, {
      Authorization: 'Basic ' + Base64.strict_encode64(auth_value)
    })
    auth_value = nil
  else
    composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
  end

  execute_rest_call(env, endpoint, nil, auth_value, composite_headers, uri_params) do |env, url, data, headers|
    next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).get(headers)
  end
end

.execute_get_unauthenticated(env, endpoint, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key') ⇒ RestClient::Response

Execute a GET request without authentication.

Parameters:

  • env (RestEnvironment)

    The RestEnvironment to use to execute the request

  • endpoint (Endpoint)

    The Endpoint which should be requested

  • additional_headers (Hash) (defaults to: {})

    A Hash of key-value pairs that will be sent as additional headers in the API call. Defaults to an empty Hash.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the GET call.



39
40
41
42
43
44
45
# File 'lib/bubbles/rest_client_resources.rb', line 39

def self.execute_get_unauthenticated(env, endpoint, uri_params, additional_headers = {}, api_key = nil, api_key_name='X-API-Key')
  composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)

  execute_rest_call(env, endpoint, nil, nil, composite_headers, uri_params) do |env, url, data, headers|
    next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).get(headers)
  end
end

.execute_head_authenticated(env, endpoint, auth_token, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key') ⇒ RestClient::Response

Execute a HEAD request with authentication.

Currently, only Authorization: Bearer is supported. This is the same as a GET request, but will only return headers and not the response body.

Parameters:

  • env (RestEnvironment)

    The RestEnvironment to use to execute the request

  • endpoint (Endpoint)

    The Endpoint which should be requested

  • auth_token (String)

    The authorization token to use for authentication.

  • uri_params (Hash)

    A Hash of identifiers to values to replace in the URI string.

  • additional_headers (Hash) (defaults to: {})

    A Hash of key-value pairs that will be sent as additional headers in the API call. Defaults to an empty Hash.

  • api_key (String) (defaults to: nil)

    (Optional) The API key to use to send to the host for unauthenticated requests. Defaults to nil.

  • api_key_name (String) (defaults to: 'X-API-Key')

    (Optional) The name of the header in which to send the API key. Defaults to “X-API-Key”.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the HEAD call.



125
126
127
128
129
130
131
# File 'lib/bubbles/rest_client_resources.rb', line 125

def self.execute_head_authenticated(env, endpoint, auth_token, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
  composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)

  execute_rest_call(env, endpoint, nil, auth_token, composite_headers, uri_params) do |env, url, data, headers|
    next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).head(headers)
  end
end

.execute_head_unauthenticated(env, endpoint, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key') ⇒ RestClient::Response

Execute a HEAD request without authentication.

This is the same as a GET request, but will only return headers and not the response body.

Parameters:

  • env (RestEnvironment)

    The RestEnvironment to use to execute the request

  • endpoint (Endpoint)

    The Endpoint which should be requested

  • uri_params (Hash)

    A Hash of identifiers to values to replace in the URI string.

  • additional_headers (Hash) (defaults to: {})

    A Hash of key-value pairs that will be sent as additional headers in the API call. Defaults to an empty Hash.

  • api_key (String) (defaults to: nil)

    (Optional) The API key to use to send to the host for unauthenticated requests. Defaults to nil.

  • api_key_name (String) (defaults to: 'X-API-Key')

    (Optional) The name of the header in which to send the API key. Defaults to “X-API-Key”.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the GET call.



98
99
100
101
102
103
104
# File 'lib/bubbles/rest_client_resources.rb', line 98

def self.execute_head_unauthenticated(env, endpoint, uri_params, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
  composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)

  execute_rest_call(env, endpoint, nil, nil, composite_headers, uri_params) do |env, url, data, headers|
    next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).head(headers)
  end
end

.execute_patch_authenticated(env, endpoint, auth_token, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key') ⇒ RestClient::Response

Execute a PATCH request with authentication in the form of an authorization token.

Parameters:

  • env (RestEnvironment)

    The RestEnvironment to use to execute the request

  • endpoint (Endpoint)

    The Endpoint which should be requested

  • auth_token (String)

    The authorization token retrieved during some former authentication call. Will be placed into a Authorization header.

  • uri_params (Hash)

    A Hash of identifiers to values to replace in the URI string.

  • data (Hash)

    A Hash of key-value pairs that will be sent in the body of the http request.

  • additional_headers (Hash) (defaults to: {})

    A Hash of key-value pairs that will be sent as additional headers in the API call. Defaults to an empty Hash.

  • api_key (String) (defaults to: nil)

    (Optional) The API key to use to send to the host for unauthenticated requests. Defaults to nil.

  • api_key_name (String) (defaults to: 'X-API-Key')

    (Optional) The name of the header in which to send the API key. Defaults to “X-API-Key”.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the PATCH call.



207
208
209
210
211
212
213
# File 'lib/bubbles/rest_client_resources.rb', line 207

def self.execute_patch_authenticated(env, endpoint, auth_token, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
  composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)

  return execute_rest_call(env, endpoint, data, auth_token, composite_headers, uri_params) do |env, url, data, headers|
    next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).patch(data.to_json, headers)
  end
end

.execute_patch_unauthenticated(env, endpoint, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key') ⇒ RestClient::Response

Execute a PATCH request without authentication.

Parameters:

  • env (RestEnvironment)

    The RestEnvironment to use to execute the request

  • endpoint (Endpoint)

    The Endpoint which should be requested

  • uri_params (Hash)

    A Hash of identifiers to values to replace in the URI string.

  • data (Hash)

    A Hash of key-value pairs that will be sent in the body of the http request.

  • additional_headers (Hash) (defaults to: {})

    A Hash of key-value pairs that will be sent as additional headers in the API call. Defaults to an empty Hash.

  • api_key (String) (defaults to: nil)

    (Optional) The API key to use to send to the host for unauthenticated requests. Defaults to nil.

  • api_key_name (String) (defaults to: 'X-API-Key')

    (Optional) The name of the header in which to send the API key. Defaults to “X-API-Key”.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the PATCH call.



231
232
233
234
235
236
237
# File 'lib/bubbles/rest_client_resources.rb', line 231

def self.execute_patch_unauthenticated(env, endpoint, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
  composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)

  return execute_rest_call(env, endpoint, data, nil, composite_headers, uri_params) do |env, url, data, headers|
    next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).patch(data.to_json, headers)
  end
end

.execute_post_authenticated(env, endpoint, auth_type, auth_value, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key') ⇒ RestClient::Response

Execute a POST request with authentication in the form of an authorization token.

Parameters:

  • env (RestEnvironment)

    The RestEnvironment to use to execute the request

  • endpoint (Endpoint)

    The Endpoint which should be requested

  • auth_token (String)

    The authorization token retrieved during some former authentication call. Will be placed into a Authorization header.

  • data (Hash)

    A Hash of key-value pairs that will be sent in the body of the http request.

  • additional_headers (Hash) (defaults to: {})

    A Hash of key-value pairs that will be sent as additional headers in the API call. Defaults to an empty Hash.

  • api_key (String) (defaults to: nil)

    (Optional) The API key to use to send to the host for unauthenticated requests. Defaults to nil.

  • api_key_name (String) (defaults to: 'X-API-Key')

    (Optional) The name of the header in which to send the API key. Defaults to “X-API-Key”.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the POST call.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/bubbles/rest_client_resources.rb', line 174

def self.execute_post_authenticated(env, endpoint, auth_type, auth_value, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
  if auth_type == :basic
    composite_headers = RestClientResources.build_composite_headers(endpoint.additional_headers, {
      Authorization: 'Basic ' + Base64.strict_encode64(auth_value)
    })
    auth_value = nil
  else
    composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)
  end

  execute_rest_call(env, endpoint, data, auth_value, composite_headers) do |env, url, data, headers|
    next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).post(data.to_json, headers)
  end
end

.execute_post_unauthenticated(env, endpoint, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key') ⇒ RestClient::Response

Execute a POST request without authentication, but requiring an API key.

Parameters:

  • env (RestEnvironment)

    The RestEnvironment to use to execute the request

  • endpoint (Endpoint)

    The Endpoint which should be requested

  • api_key (String) (defaults to: nil)

    The API key to use to process the request. Will be placed in an ‘X-API-KEY’ header.

  • data (Hash)

    A Hash of key-value pairs that will be sent in the body of the http request.

  • additional_headers (Hash) (defaults to: {})

    A Hash of key-value pairs that will be sent as additional headers in the API call. Defaults to an empty Hash.

  • api_key (String) (defaults to: nil)

    (Optional) The API key to use to send to the host for unauthenticated requests. Defaults to nil.

  • api_key_name (String) (defaults to: 'X-API-Key')

    (Optional) The name of the header in which to send the API key. Defaults to “X-API-Key”.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the POST call.



149
150
151
152
153
154
155
# File 'lib/bubbles/rest_client_resources.rb', line 149

def self.execute_post_unauthenticated(env, endpoint, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
  composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)

  execute_rest_call(env, endpoint, data, nil, composite_headers) do |env, url, data, headers|
    next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).post(data.to_json, headers)
  end
end

.execute_put_authenticated(env, endpoint, auth_token, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key') ⇒ RestClient::Response

Execute a PUT request with authentication in the form of an authorization token.

Parameters:

  • env (RestEnvironment)

    The RestEnvironment to use to execute the request

  • endpoint (Endpoint)

    The Endpoint which should be requested

  • auth_token (String)

    The authorization token retrieved during some former authentication call. Will be placed into a Authorization header.

  • uri_params (Hash)

    A Hash of identifiers to values to replace in the URI string.

  • data (Hash)

    A Hash of key-value pairs that will be sent in the body of the http request.

  • additional_headers (Hash) (defaults to: {})

    A Hash of key-value pairs that will be sent as additional headers in the API call. Defaults to an empty Hash.

  • api_key (String) (defaults to: nil)

    (Optional) The API key to use to send to the host for unauthenticated requests. Defaults to nil.

  • api_key_name (String) (defaults to: 'X-API-Key')

    (Optional) The name of the header in which to send the API key. Defaults to “X-API-Key”.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the PUT call.



257
258
259
260
261
262
263
# File 'lib/bubbles/rest_client_resources.rb', line 257

def self.execute_put_authenticated(env, endpoint, auth_token, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
  composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)

  return execute_rest_call(env, endpoint, data, auth_token, composite_headers, uri_params) do |env, url, data, headers|
    next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).put(data.to_json, headers)
  end
end

.execute_put_unauthenticated(env, endpoint, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key') ⇒ RestClient::Response

Execute a PUT request without authentication.

Parameters:

  • env (RestEnvironment)

    The RestEnvironment to use to execute the request

  • endpoint (Endpoint)

    The Endpoint which should be requested

  • uri_params (Hash)

    A Hash of identifiers to values to replace in the URI string.

  • data (Hash)

    A Hash of key-value pairs that will be sent in the body of the http request.

  • additional_headers (Hash) (defaults to: {})

    A Hash of key-value pairs that will be sent as additional headers in the API call. Defaults to an empty Hash.

  • api_key (String) (defaults to: nil)

    (Optional) The API key to use to send to the host for unauthenticated requests. Defaults to nil.

  • api_key_name (String) (defaults to: 'X-API-Key')

    (Optional) The name of the header in which to send the API key. Defaults to “X-API-Key”.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the PUT call.



281
282
283
284
285
286
287
# File 'lib/bubbles/rest_client_resources.rb', line 281

def self.execute_put_unauthenticated(env, endpoint, uri_params, data, additional_headers = {}, api_key = nil, api_key_name = 'X-API-Key')
  composite_headers = self.get_headers_with_api_key(endpoint, api_key, api_key_name, additional_headers)

  return execute_rest_call(env, endpoint, data, nil, composite_headers, uri_params) do |env, url, data, headers|
    next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE).put(data.to_json, headers)
  end
end

.get_encoded_authorization(endpoint, data) ⇒ Object

Retrieve an encoded authorization (username and password) for a given Endpoint and data set.

Parameters:

  • endpoint

    The Endpoint that this authorization will be used for.

  • data

    A set of elements (typically username and password) that should be encoded.

Returns:

  • A String containing an encoding of the values passed in as data, concatenated with a colon.



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/bubbles/rest_client_resources.rb', line 392

def self.get_encoded_authorization(endpoint, data)
  count = 0
  auth_value = ''
  endpoint.encode_authorization.each { |auth_key|
    if data[auth_key]
      if count > 0
        auth_value = auth_value + ':' + data[auth_key]
      else
        auth_value = data[auth_key]
      end

      count = count + 1

      data.delete(auth_key)
    end
  }

  auth_value
end

Instance Method Details

#environment(env_name = nil) ⇒ Object



6
7
8
# File 'lib/bubbles/rest_client_resources.rb', line 6

def environment(env_name = nil)
  Bubbles.configuration.environment(env_name)
end