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) ⇒ RestClientResources

Create a new instance of RestClientResources.

Parameters:

  • env

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

  • api_key

    The API key to use to send to the host for unauthenticated requests.



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

def initialize(env, api_key)
  unless env
    env = :local
  end

  unless api_key
    api_key = ''
  end

  @environment = get_environment env
  @api_key = api_key
  @auth_token = nil
end

Class Method Details

.execute_delete_authenticated(env, endpoint, auth_token, uri_params) ⇒ 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.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the DELETE call.



232
233
234
235
236
237
238
239
240
241
# File 'lib/bubbles/rest_client_resources.rb', line 232

def self.execute_delete_authenticated(env, endpoint, auth_token, uri_params)
  execute_rest_call(env, endpoint, nil, auth_token, nil, uri_params) do |env, url, data, headers|
    if env.scheme == 'https'
      next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
        .delete(headers)
    else
      next RestClient.delete(url.to_s, headers)
    end
  end
end

.execute_get_authenticated(env, endpoint, auth_token, uri_params) ⇒ 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_token (String)

    The authorization token to use for authentication.

  • uri_params (Hash)

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

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the GET call.



61
62
63
64
65
66
67
68
69
70
# File 'lib/bubbles/rest_client_resources.rb', line 61

def self.execute_get_authenticated(env, endpoint, auth_token, uri_params)
  execute_rest_call(env, endpoint, nil, auth_token, nil, uri_params) do |env, url, data, headers|
    if env.scheme == 'https'
      next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
        .get(headers)
    else
      next RestClient.get(url.to_s, headers)
    end
  end
end

.execute_get_unauthenticated(env, endpoint) ⇒ 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

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the GET call.



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

def self.execute_get_unauthenticated(env, endpoint)
  execute_rest_call(env, endpoint, nil, nil, nil) do |env, url, data, headers|
    if env.scheme == 'https'
      next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
        .get(headers)
    else
      next RestClient.get(url.to_s, headers)
    end
  end
end

.execute_head_authenticated(env, endpoint, auth_token, uri_params) ⇒ 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.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the GET call.



106
107
108
109
110
111
112
113
114
115
# File 'lib/bubbles/rest_client_resources.rb', line 106

def self.execute_head_authenticated(env, endpoint, auth_token, uri_params)
  execute_rest_call(env, endpoint, nil, auth_token, nil, uri_params) do |env, url, data, headers|
    if env.scheme == 'https'
      next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
        .head(headers)
    else
      next RestClient.head(url.to_s, headers)
    end
  end
end

.execute_head_unauthenticated(env, endpoint, uri_params, additional_headers) ⇒ 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

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the GET call.



82
83
84
85
86
87
88
89
90
91
# File 'lib/bubbles/rest_client_resources.rb', line 82

def self.execute_head_unauthenticated(env, endpoint, uri_params, additional_headers)
  execute_rest_call(env, endpoint, nil, nil, additional_headers, uri_params) do |env, url, data, headers|
    if env.scheme == 'https'
      next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
        .head(headers)
    else
      next RestClient.head(url.to_s, headers)
    end
  end
end

.execute_patch_authenticated(env, endpoint, auth_token, uri_params, data) ⇒ 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.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the PATCH call.



185
186
187
188
189
190
191
192
193
194
195
# File 'lib/bubbles/rest_client_resources.rb', line 185

def self.execute_patch_authenticated(env, endpoint, auth_token, uri_params, data)
  return execute_rest_call(env, endpoint, data, auth_token, nil, uri_params) do |env, url, data, headers|
    if env.scheme == 'https'
      next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
        .patch(data.to_json, headers)

    else
      next RestClient.patch(url.to_s, data.to_json, headers)
    end
  end
end

.execute_post_authenticated(env, endpoint, auth_token, data) ⇒ 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.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the POST call.



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/bubbles/rest_client_resources.rb', line 161

def self.execute_post_authenticated(env, endpoint, auth_token, data)
  return execute_rest_call(env, endpoint, data, auth_token, nil) do |env, url, data, headers|
    if env.scheme == 'https'
      next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
        .post(data.to_json, headers)

    else
      next RestClient.post(url.to_s, data.to_json, headers)
    end
  end
end

.execute_post_with_api_key(env, endpoint, api_key, data, headers = nil) ⇒ 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)

    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.

  • headers (Hash) (defaults to: nil)

    (Optional) A Hash of key-value pairs that will be sent as HTTP headers as part of the request. Defaults to nil.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the POST call.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/bubbles/rest_client_resources.rb', line 129

def self.execute_post_with_api_key(env, endpoint, api_key, data, headers=nil)
  additional_headers = {
    'X-Api-Key' => api_key
  }

  unless headers.nil?
    headers.each { |nextHeader|
      additional_headers[nextHeader[0]] = nextHeader[1]
    }
  end

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

.execute_put_authenticated(env, endpoint, auth_token, uri_params, data) ⇒ 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.

Returns:

  • (RestClient::Response)

    The Response resulting from the execution of the PUT call.



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/bubbles/rest_client_resources.rb', line 209

def self.execute_put_authenticated(env, endpoint, auth_token, uri_params, data)
  return execute_rest_call(env, endpoint, data, auth_token, nil, uri_params) do |env, url, data, headers|
    if env.scheme == 'https'
      next RestClient::Resource.new(url.to_s, :verify_ssl => OpenSSL::SSL::VERIFY_NONE)
        .put(data.to_json, headers)

    else
      next RestClient.put(url.to_s, data.to_json, headers)
    end
  end
end

Instance Method Details

#environmentObject



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

def environment
  Bubbles.configuration.environment
end