Class: Bubbles::Configuration

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

Overview

The configuration of the Bubbles rest client.

Use this class if you want to retrieve configuration values set during initialization.

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



41
42
43
44
45
46
47
48
# File 'lib/bubbles/config.rb', line 41

def initialize
  @environment_scheme = 'http'
  @environment_host = '127.0.0.1'
  @environment_port = '1234'
  @environment_api_key = nil

  @endpoints = Hash.new
end

Instance Method Details

#endpointsArray

Retrieve the list of Endpoints configured in this Configuration object.

Returns:



85
86
87
# File 'lib/bubbles/config.rb', line 85

def endpoints
  @endpoints
end

#endpoints=(endpoints) ⇒ Object

Add all Endpoint objects within this Bubbles::Configuration instance.

Endpoint objects are defined using two required parameters: type and location, and three optional parameters: authenticated, api_key_required and name.

- method: Indicates the HTTP method used to access the endpoint. Must be one of {Endpoint::METHODS}.
- location: Indicates the path at which the {Endpoint} can be accessed on the host environment.
- authenticated: (Optional) A true or false value indicating whether the {Endpoint} requires an authorization
                 token to access it. Defaults to false.
- api_key_required: (Optional) A true or false value indicating whether the {Endpoint} requires a API key to
                    access it. Defaults to false.
- name: (Optional): A +String+ indicating the name of the method to add. If not provided, the method name will
        be the same as the +location+.


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/bubbles/config.rb', line 103

def endpoints=(endpoints)
  new_endpoints = Hash.new
  endpoints.each do |ep|
    endpoint_object = Endpoint.new ep[:method], ep[:location].to_s, ep[:authenticated], ep[:api_key_required], ep[:name], ep[:return_type], ep[:encode_authorization]

    new_endpoints[endpoint_object.get_key_string] = endpoint_object
  end

  @endpoints = new_endpoints

  # Define all of the endpoints as methods on RestEnvironment
  @endpoints.values.each do |endpoint|
    if endpoint.name != nil
      endpoint_name_as_sym = endpoint.name.to_sym
    else
      endpoint_name_as_sym = endpoint.get_location_string.to_sym
    end

    if Bubbles::RestEnvironment.instance_methods(false).include?(endpoint_name_as_sym)
      Bubbles::RestEnvironment.class_exec do
        remove_method endpoint_name_as_sym
      end
    end

    if endpoint.method == :get
      if endpoint.authenticated?
        Bubbles::RestEnvironment.class_exec do
          if endpoint.has_uri_params?
            define_method(endpoint_name_as_sym) do |auth_token, uri_params|
              RestClientResources.execute_get_authenticated self, endpoint, auth_token, uri_params
            end
          else
            define_method(endpoint_name_as_sym) do |auth_token|
              RestClientResources.execute_get_authenticated self, endpoint, auth_token, {}
            end
          end
        end
      else
        Bubbles::RestEnvironment.class_exec do
          define_method(endpoint_name_as_sym) do
            RestClientResources.execute_get_unauthenticated self, endpoint
          end
        end
      end
    elsif endpoint.method == :post
      if endpoint.authenticated?
        Bubbles::RestEnvironment.class_exec do
          define_method(endpoint_name_as_sym) do |auth_token, data|
            RestClientResources.execute_post_authenticated self, endpoint, auth_token, data
          end
        end
      else
        if endpoint.api_key_required?
          Bubbles::RestEnvironment.class_exec do
            define_method(endpoint_name_as_sym) do |data|
              additional_headers = {}
              if endpoint.encode_authorization_header?
                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
                }

                additional_headers[:Authorization] = 'Basic ' + Base64.strict_encode64(auth_value)
              end

              RestClientResources.execute_post_with_api_key self, endpoint, self.api_key, data, additional_headers
            end
          end
        else
          raise 'Unauthenticated POST requests without an API key are not allowed'
        end
      end
    elsif endpoint.method == :delete
      if endpoint.authenticated?
        Bubbles::RestEnvironment.class_exec do
          if endpoint.has_uri_params?
            define_method(endpoint_name_as_sym) do |auth_token, uri_params|
              RestClientResources.execute_delete_authenticated self, endpoint, auth_token, uri_params
            end
          else
            # NOTE: While MDN states that DELETE requests with a body are allowed, it seems that a number of
            # documentation sites discourage its use. Thus, it's possible that, depending on the server API
            # framework, the DELETE request could be rejected. As such, we're disallowing it here, BUT if we
            # get feedback from users that it should be supported, we can add support for it.
            raise 'DELETE requests without URI parameters are not allowed'
          #   define_method(endpoint_name_as_sym) do |auth_token|
          #     RestClientResources.execute_delete_authenticated self, endpoint, auth_token, {}
          #   end
          end
        end
      else
        raise 'Unauthenticated DELETE requests are not allowed'
        # Bubbles::RestEnvironment.class_exec do
          # define_method(endpoint_name_as_sym) do
          #   RestClientResources.execute_delete_unauthenticated self, endpoint
          # end
        # end
      end
    elsif endpoint.method == :patch
      if endpoint.authenticated?
        Bubbles::RestEnvironment.class_exec do
          if endpoint.has_uri_params?
            define_method(endpoint_name_as_sym) do |auth_token, uri_params, data|
              RestClientResources.execute_patch_authenticated self, endpoint, auth_token, uri_params, data
            end
          else
            define_method(endpoint_name_as_sym) do |auth_token, data|
              RestClientResources.execute_patch_authenticated self, endpoint, auth_token, nil, data
            end
          end
        end
      else
        raise 'Unauthenticated PATCH requests are not implemented'
        # Bubbles::RestEnvironment.class_exec do
        # define_method(endpoint_name_as_sym) do
        #   RestClientResources.execute_delete_unauthenticated self, endpoint
        # end
        # end
      end
    elsif endpoint.method == :put
      if endpoint.authenticated?
        Bubbles::RestEnvironment.class_exec do
          if endpoint.has_uri_params?
            define_method(endpoint_name_as_sym) do |auth_token, uri_params, data|
              RestClientResources.execute_put_authenticated self, endpoint, auth_token, uri_params, data
            end
          else
            define_method(endpoint_name_as_sym) do |auth_token, data|
              RestClientResources.execute_put_authenticated self, endpoint, auth_token, nil, data
            end
          end
        end
      else
        raise 'Unauthenticated PUT requests are not implemented'
        # Bubbles::RestEnvironment.class_exec do
        # define_method(endpoint_name_as_sym) do
        #   RestClientResources.execute_delete_unauthenticated self, endpoint
        # end
        # end
      end
    elsif endpoint.method == :head
      if endpoint.authenticated?
        Bubbles::RestEnvironment.class_exec do
          if endpoint.has_uri_params?
            define_method(endpoint_name_as_sym) do |auth_token, uri_params|
              RestClientResources.execute_head_authenticated self, endpoint, auth_token, uri_params
            end
          else
            define_method(endpoint_name_as_sym) do |auth_token|
              RestClientResources.execute_head_authenticated self, endpoint, auth_token, {}
            end
          end
        end
      elsif endpoint.api_key_required?
        Bubbles::RestEnvironment.class_exec do
          if endpoint.has_uri_params?
            define_method(endpoint_name_as_sym) do |api_key, uri_params|
              additional_headers = {}
              additional_headers['X-Api-Key'] = api_key
              RestClientResources.execute_head_unauthenticated self, endpoint, uri_params, additional_headers
            end
          else
            define_method(endpoint_name_as_sym) do |api_key|
              additional_headers = {}
              additional_headers['X-Api-Key'] = api_key
              RestClientResources.execute_head_unauthenticated self, endpoint, {}, additional_headers
            end
          end
        end
      else
        Bubbles::RestEnvironment.class_exec do
          if endpoint.has_uri_params?
            define_method(endpoint_name_as_sym) do |uri_params|
              RestClientResources.execute_head_unauthenticated self, endpoint, uri_params, {}
            end
          else
            define_method(endpoint_name_as_sym) do
              RestClientResources.execute_head_unauthenticated self, endpoint, {}, {}
            end
          end
        end
      end
    end
  end
end

#environmentObject

Retrieve the RestEnvironment object defined as part of this Configuration.

Note that this constructs a new RestEnvironment and returns it, rather than returning an existing object.



55
56
57
# File 'lib/bubbles/config.rb', line 55

def environment
  RestEnvironment.new(@environment_scheme, @environment_host, @environment_port, @environment_api_key)
end

#environment=(env) ⇒ Object

Set the current environment.

Examples:

In app/config/environments/staging.rb:

Bubbles.configure do |config|
  config.environment = {
     :scheme => 'https',
     :host => 'stage.api.somehost.com',
     :port => '443'
  }
end

Parameters:

  • env (Object)

    The environment, as a generic Ruby Object.



73
74
75
76
77
78
# File 'lib/bubbles/config.rb', line 73

def environment=(env)
  @environment_scheme = env[:scheme]
  @environment_host = env[:host]
  @environment_port = env[:port]
  @environment_api_key = env[:api_key]
end