Class: Nucleus::Adapters::BaseAdapter Abstract
- Inherits:
-
Object
- Object
- Nucleus::Adapters::BaseAdapter
- Includes:
- HttpClient, HttpTailClient, Logging
- Defined in:
- lib/nucleus/adapters/base_adapter.rb
Overview
The BaseAdapter is an abstract class that shall be extended by all actual Adapters. It provides methods to common functionality:
-
authentication (+cache)
-
http client with general error handling
-
native platform API calls
Direct Known Subclasses
Instance Attribute Summary collapse
-
#endpoint_url ⇒ Object
readonly
Returns the value of attribute endpoint_url.
Instance Method Summary collapse
-
#cache(key, auth_object) ⇒ void
Cache the auth information.
-
#cache?(key) ⇒ true, false
Are there cached information for this key?.
-
#cache_key(username, password) ⇒ String
Create the cache key for the username / password combination and save it in the RequestStore to make it available throughout the current request.
-
#cached(key) ⇒ Hash<String,String>, Nucleus::Adapters::AuthClient
Get the currently cached authentication object.
-
#endpoint_call(method, path, params) ⇒ Object
Execute an API call, targeted directly against the vendors API.
-
#fail_with(error_name, params = nil) ⇒ Object
Fail with a Errors::PlatformSpecificSemanticError error and format the error message to include the values that are passed in the params.
-
#headers ⇒ Hash<String,String>
Get the cached authentication object and retrieve the presumably valid authentication header.
-
#initialize(endpoint_url, endpoint_app_domain = nil, check_certificates = true) ⇒ BaseAdapter
constructor
A new instance of BaseAdapter.
Methods included from Logging
configure_logger_for, #log, logger_for
Methods included from HttpTailClient
Methods included from HttpClient
#delete, #get, #head, #patch, #post, #put
Constructor Details
#initialize(endpoint_url, endpoint_app_domain = nil, check_certificates = true) ⇒ BaseAdapter
Returns a new instance of BaseAdapter.
18 19 20 21 22 23 |
# File 'lib/nucleus/adapters/base_adapter.rb', line 18 def initialize(endpoint_url, endpoint_app_domain = nil, check_certificates = true) fail ArgumentError, "'endpoint_url' must be a valid URL" unless endpoint_url =~ /\A#{URI.regexp(['https'])}\z/ @endpoint_url = endpoint_url @endpoint_app_domain = endpoint_app_domain @check_certificates = check_certificates end |
Instance Attribute Details
#endpoint_url ⇒ Object (readonly)
Returns the value of attribute endpoint_url.
16 17 18 |
# File 'lib/nucleus/adapters/base_adapter.rb', line 16 def endpoint_url @endpoint_url end |
Instance Method Details
#cache(key, auth_object) ⇒ void
This method returns an undefined value.
Cache the auth information.
32 33 34 |
# File 'lib/nucleus/adapters/base_adapter.rb', line 32 def cache(key, auth_object) auth_objects_cache[key] = auth_object end |
#cache?(key) ⇒ true, false
Are there cached information for this key?
39 40 41 |
# File 'lib/nucleus/adapters/base_adapter.rb', line 39 def cache?(key) auth_objects_cache.key? key end |
#cache_key(username, password) ⇒ String
Create the cache key for the username / password combination and save it in the RequestStore to make it available throughout the current request.
56 57 58 59 60 61 62 |
# File 'lib/nucleus/adapters/base_adapter.rb', line 56 def cache_key(username, password) # calculate the cache only once per request return RequestStore.store[:cache_key] if RequestStore.exist?(:cache_key) key = Digest::SHA256.hexdigest "#{endpoint_url}#{username}:#{password}" RequestStore.store[:cache_key] = key key end |
#cached(key) ⇒ Hash<String,String>, Nucleus::Adapters::AuthClient
Get the currently cached authentication object.
46 47 48 49 |
# File 'lib/nucleus/adapters/base_adapter.rb', line 46 def cached(key) return nil unless cache?(key) auth_objects_cache[key] end |
#endpoint_call(method, path, params) ⇒ Object
Execute an API call, targeted directly against the vendors API.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/nucleus/adapters/base_adapter.rb', line 77 def endpoint_call(method, path, params) case method when :GET get(path, native_call: true).body when :POST post(path, native_call: true, body: params).body when :DELETE delete(path, native_call: true).body when :PUT put(path, native_call: true, body: params).body when :PATCH patch(path, native_call: true, body: params).body else fail AdapterRequestError, 'Unsupported adapter call method. Allowed are: GET, POST, PATCH, PUT, DELETE' end end |
#fail_with(error_name, params = nil) ⇒ Object
Fail with a Errors::PlatformSpecificSemanticError error and format the error message to include the values that are passed in the params. Requires the adapter to provide a semantic_error_messages
method, which shall return a Hash with the platform specific semantic errors.
100 101 102 103 104 105 106 |
# File 'lib/nucleus/adapters/base_adapter.rb', line 100 def fail_with(error_name, params = nil) unless respond_to?(:semantic_error_messages) fail StandardError 'Invalid adapter implementation, no :semantic_error_messages method provided' end error = [error_name] fail Errors::PlatformSpecificSemanticError.new(error[:message] % params, error[:code]) end |
#headers ⇒ Hash<String,String>
Get the cached authentication object and retrieve the presumably valid authentication header.
66 67 68 69 70 |
# File 'lib/nucleus/adapters/base_adapter.rb', line 66 def headers auth_object = auth_objects_cache[RequestStore.store[:cache_key]] # AuthClient, generates the header for us auth_object.auth_header end |