Class: Bubbles::Configuration
- Inherits:
-
Object
- Object
- Bubbles::Configuration
- 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
-
#endpoints ⇒ Array
Retrieve the list of Endpoints configured in this
Configuration
object. -
#endpoints=(endpoints) ⇒ Object
Add all Endpoint objects within this Configuration instance.
-
#environment(environment_name = nil) ⇒ RestEnvironment
Retrieve the RestEnvironment object defined as part of this Configuration having a specified name.
-
#environments=(environments) ⇒ Object
Set the environments that can be used.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
41 42 43 44 |
# File 'lib/bubbles/config.rb', line 41 def initialize @environments = Hash.new @endpoints = Hash.new end |
Instance Method Details
#endpoints ⇒ Array
Retrieve the list of Endpoints configured in this Configuration
object.
128 129 130 |
# File 'lib/bubbles/config.rb', line 128 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+.
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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/bubbles/config.rb', line 146 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], ep[:headers] 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? if endpoint. define_method(endpoint_name_as_sym) do |username, password, uri_params| login_data = { :login => username, :password => password } auth_value = RestClientResources.(endpoint, login_data) RestClientResources.execute_get_authenticated self, endpoint, :basic, auth_value, uri_params, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do |auth_token, uri_params| RestClientResources.execute_get_authenticated self, endpoint, :bearer, auth_token, uri_params, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end else if endpoint. define_method(endpoint_name_as_sym) do |username, password| login_data = { :username => username, :password => password } auth_value = RestClientResources.(endpoint, login_data) RestClientResources.execute_get_authenticated self, endpoint, :basic, auth_value, {}, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do |auth_token| RestClientResources.execute_get_authenticated self, endpoint, :bearer, auth_token, {}, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end 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_get_unauthenticated self, endpoint, uri_params, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do RestClientResources.execute_get_unauthenticated self, endpoint, {}, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end end end elsif endpoint.method == :post if endpoint.authenticated? and !endpoint. Bubbles::RestEnvironment.class_exec do define_method(endpoint_name_as_sym) do |auth_token, data| RestClientResources.execute_post_authenticated self, endpoint, :bearer, auth_token, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end elsif endpoint. Bubbles::RestEnvironment.class_exec do define_method(endpoint_name_as_sym) do |username, password, data = {}| login_data = { :username => username, :password => password } auth_value = RestClientResources.(endpoint, login_data) # composite_headers = RestClientResources.build_composite_headers(endpoint.additional_headers, { # Authorization: 'Basic ' + Base64.strict_encode64(auth_value) # }) RestClientResources.execute_post_authenticated self, endpoint, :basic, auth_value, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end else Bubbles::RestEnvironment.class_exec do define_method(endpoint_name_as_sym) do |data| composite_headers = endpoint.additional_headers RestClientResources.execute_post_unauthenticated self, endpoint, data, composite_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end end elsif endpoint.method == :delete if endpoint.has_uri_params? if endpoint.authenticated? Bubbles::RestEnvironment.class_exec do define_method(endpoint_name_as_sym) do |auth_token, uri_params| RestClientResources.execute_delete_authenticated self, endpoint, auth_token, uri_params, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end else Bubbles::RestEnvironment.class_exec do define_method(endpoint_name_as_sym) do |uri_params| RestClientResources.execute_delete_unauthenticated self, endpoint, uri_params, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end end else # XXX_jwir3: 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. In addition, RestClient doesn't seem to support DELETE # requests with a body, so we're a bit stuck on this one, even if we wanted to support it. raise 'DELETE requests without URI parameters are not allowed' 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, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do |auth_token, data| RestClientResources.execute_patch_authenticated self, endpoint, auth_token, {}, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end end else Bubbles::RestEnvironment.class_exec do if endpoint.has_uri_params? define_method(endpoint_name_as_sym) do |uri_params, data| RestClientResources.execute_patch_unauthenticated self, endpoint, uri_params, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do |data| RestClientResources.execute_patch_unauthenticated self, endpoint, {}, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end 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, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do |auth_token, data| RestClientResources.execute_put_authenticated self, endpoint, auth_token, {}, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end end else Bubbles::RestEnvironment.class_exec do if endpoint.has_uri_params? define_method(endpoint_name_as_sym) do |uri_params, data| RestClientResources.execute_put_unauthenticated self, endpoint, uri_params, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do |data| RestClientResources.execute_put_unauthenticated self, endpoint, {}, data, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end 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, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do |auth_token| RestClientResources.execute_head_authenticated self, endpoint, auth_token, {}, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name 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, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end else define_method(endpoint_name_as_sym) do RestClientResources.execute_head_unauthenticated self, endpoint, {}, endpoint.additional_headers, self.get_api_key_if_needed(endpoint), self.api_key_name end end end end end end end |
#environment(environment_name = nil) ⇒ RestEnvironment
Retrieve the RestEnvironment object defined as part of this Configuration having a specified name.
The environment_name
is nil
by default, which will return the default configuration, if only one exists.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/bubbles/config.rb', line 57 def environment(environment_name = nil) if environment_name.nil? if @environments.length > 1 raise 'You must specify an environment_name parameter because more than one environment is defined' end env_hash = @environments[nil] else env_hash = @environments[environment_name] end if env_hash.nil? if environment_name.nil? raise 'No default environment specified' end raise 'No environment specified having name {}', environment_name end RestEnvironment.new(env_hash[:scheme], env_hash[:host], env_hash[:port], env_hash[:api_key], env_hash[:api_key_name]) end |
#environments=(environments) ⇒ Object
Set the environments that can be used.
One or more environments may be specified, but if more than one environment is specified, it is required that each environment have a :environment_name:
parameter to differentiate it from other environments.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/bubbles/config.rb', line 99 def environments=(environments) default = nil environments.each do |environment| if environments.length > 1 && environment[:environment_name].nil? = 'More than one environment was specified and at least one of the environments does not have an ' \ ':environment_name field. Verify all environments have an :environment_name.' raise end @environments = {} env_api_key = 'X-API-Key' env_api_key = environment[:api_key_name] if environment.key? :api_key_name @environments[environment[:environment_name]] = { scheme: environment[:scheme], host: environment[:host], port: environment[:port], api_key: environment[:api_key], api_key_name: env_api_key } end end |