Class: Nucleus::AdapterResolver
- Inherits:
-
Object
- Object
- Nucleus::AdapterResolver
- Includes:
- UrlConverter
- Defined in:
- lib/nucleus/adapter_resolver.rb
Overview
The AdapterResolver can be used within Ruby applications to retrieve a Nucleus adapter. Returned adapters are patched so that each call enforces authentication and retries a call when a token was expired.
Instance Method Summary collapse
-
#adapters ⇒ Hash<String, Hash<String, Nucleus::Adapters::BaseAdapter>>
Get a list of all adapters that are currently supported.
-
#initialize(requested_version) ⇒ AdapterResolver
constructor
A new instance of AdapterResolver.
-
#load(vendor, username, password, options = {}) ⇒ Nucleus::Adapters::BaseAdapter
Load the adapter to interact with the platform of the vendor that is offered at the endpoint_url.
Methods included from UrlConverter
Constructor Details
#initialize(requested_version) ⇒ AdapterResolver
Returns a new instance of AdapterResolver.
7 8 9 10 |
# File 'lib/nucleus/adapter_resolver.rb', line 7 def initialize(requested_version) fail 'No such version supported' unless Nucleus::VersionDetector.api_versions.include?(requested_version) @api_version = requested_version end |
Instance Method Details
#adapters ⇒ Hash<String, Hash<String, Nucleus::Adapters::BaseAdapter>>
Get a list of all adapters that are currently supported.
14 15 16 17 |
# File 'lib/nucleus/adapter_resolver.rb', line 14 def adapters setup @adapters end |
#load(vendor, username, password, options = {}) ⇒ Nucleus::Adapters::BaseAdapter
Load the adapter to interact with the platform of the vendor that is offered at the endpoint_url.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/nucleus/adapter_resolver.rb', line 34 def load(vendor, username, password, = {}) setup fail StandardError, "Could not find adapter for vendor '#{vendor}'" unless @adapters.key?(vendor) # load the endpoint's HTTPS enabled API URL endpoint_url = load_endpoint(vendor, ) # load default configuration if available if @configurations[vendor].key?(endpoint_url) default_configuration = @configurations[vendor][endpoint_url] = default_configuration.merge() end check_ssl = .key?(:check_ssl) ? [:check_ssl] : true adapter = @adapters[vendor].new(endpoint_url, [:app_domain], check_ssl) fake_env = { 'HTTP_AUTHORIZATION' => 'Basic ' + ["#{username}:#{password}"].pack('m*').gsub(/\n/, '') } # patch the adapter so that calls are wrapped and expect valid authentication AdapterAuthenticationInductor.patch(adapter, fake_env) cache_key = adapter.cache_key(username, password) # no auth object available, perform authentication first auth_object = adapter.auth_client # throws an error if the authentication failed auth_object.authenticate(username, password) # cache the auth object so it does not have to be retrieved per request adapter.cache(cache_key, auth_object) # return patched and initially authenticated adapter adapter end |