class DnB::Direct::Plus

Constants

BASE_URL

Attributes

api_base_url[RW]
api_key[RW]
api_secret[RW]
conn[RW]
token[RW]

Public Class Methods

api_auth() click to toggle source

Constructs the authorization key by encoding {{api_key}}:{{api_secret}} to a base64 encoded string. Both required attributes must be set prior to generating the key.

# File lib/dnb/direct/plus.rb, line 14
def api_auth
    raise ArgumentError, 'api_key is missing' if api_key.blank?
    raise ArgumentError, 'api_secret is missing' if api_secret.blank?
    Base64.strict_encode64(api_key + ':' + api_secret)
end
api_token() click to toggle source

Fetches the token from the authorization service.

# File lib/dnb/direct/plus.rb, line 26
def api_token
    if @token.nil?
        response = connection.post do |req|
            req.url '/v2/token'
            req.headers[:authorization] = "Basic #{api_auth}"
            req.body = '{ "grant_type" : "client_credentials" }'
        end
        @token = JSON.parse(response.body)['access_token']
    end
    token
end
connection() click to toggle source
# File lib/dnb/direct/plus.rb, line 38
def connection
    @conn ||= Faraday.new(connection_options) do |faraday|
        faraday.request :url_encoded # form-encode POST params
        faraday.response(:logger) if $LOG_DNB
        faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
    end
end
use_credentials(key, secret) click to toggle source
# File lib/dnb/direct/plus.rb, line 20
def use_credentials key, secret
  @api_key = key
  @api_secret = secret
end

Private Class Methods

connection_options() click to toggle source
# File lib/dnb/direct/plus.rb, line 48
def connection_options
    {
        url: BASE_URL,
        headers: {
            content_type: 'application/json'
        }
    }
end