Class: Nucleus::AdapterResolver

Inherits:
Object
  • Object
show all
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

Methods included from UrlConverter

#secure_url

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

#adaptersHash<String, Hash<String, Nucleus::Adapters::BaseAdapter>>

Get a list of all adapters that are currently supported.

Returns:



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.

Parameters:

  • vendor (String)

    The vendor / adapter name that shall be used to communicate with the endpoint. Must be supported, otherwise a StandardError will be thrown.

  • username (String)

    The username that shall be used for authentication

  • password (String)

    The password that shall be used for authentication

  • options (Hash<Symbol,?>) (defaults to: {})

    Further options to apply when creating the adapter instance. If available, the default configuration of the vendor configuration is applied as default.

Options Hash (options):

  • :app_domain (String)

    The domain where applications of the platform will be made available at. This option must be set for custom deployments of platforms like Cloud Foundry or Openshift. For IBM Bluemix this value would be: eu-gb.mybluemix.net or ng.mybluemix.net, depending on the endpoint.

  • :check_ssl (String)

    Set to false if SSL certificates shall not be verified (trust self-signed)

  • :api_url (String)

    URL of the endpoint’s API that shall be used. Must be specified if there are multiple endpoints available and will be forced to https://

Returns:

Raises:

  • (StandardError)

    if the vendor is unknown / not supported or no unique API endpoint could be identified



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, options = {})
  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, options)

  # load default configuration if available
  if @configurations[vendor].key?(endpoint_url)
    default_configuration = @configurations[vendor][endpoint_url]
    options = default_configuration.merge(options)
  end

  check_ssl = options.key?(:check_ssl) ? options[:check_ssl] : true
  adapter = @adapters[vendor].new(endpoint_url, options[: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