Class: Nucleus::Adapters::BaseAdapter Abstract

Inherits:
Object
  • Object
show all
Includes:
HttpClient, HttpTailClient, Logging
Defined in:
lib/nucleus/adapters/base_adapter.rb

Overview

This class is abstract.

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

V1::Stub

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #log, logger_for

Methods included from HttpTailClient

#tail_http_response

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_urlObject (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.

Parameters:



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?

Parameters:

  • key (String)

    cache key

Returns:

  • (true, false)

    true if has cached auth info, else false



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.

Parameters:

  • username (String)

    the username for the authentication

  • password (String)

    the password for the authentication

Returns:

  • (String)

    calculated hash key for the input values



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.

Parameters:

  • key (String)

    cache key

Returns:



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.

Parameters:

  • method (Symbol)

    http method to use, one of: [:GET, :POST, :DELETE, :PUT, :PATCH]

  • path (String)

    url path to append to the endpoint’s URL

  • params (Hash)

    body params to use for PATCH, :PUT and :POST requests

Returns:

  • (Object)

    the actual response body of the vendor platform



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.

Parameters:

  • error_name (Symbol)

    error that shall be returned

  • params (Array<String>) (defaults to: nil)

    values that are to be included in the error message template

Raises:



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 = semantic_error_messages[error_name]
  fail Errors::PlatformSpecificSemanticError.new(error[:message] % params, error[:code])
end

#headersHash<String,String>

Get the cached authentication object and retrieve the presumably valid authentication header.

Returns:

  • (Hash<String,String>)

    hash including a 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