Class: DnB::Direct::Plus

Inherits:
Object
  • Object
show all
Defined in:
lib/dnb/direct.rb,
lib/dnb/direct/plus.rb

Defined Under Namespace

Modules: Mappings Classes: Address, BusinessEntityType, Content, DunsControlStatus, IndustryCode, Match, Organization, RegistrationNumber, Search, Telephone

Constant Summary collapse

BASE_URL =
'https://plus.dnb.com'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_base_urlObject

Returns the value of attribute api_base_url.



9
10
11
# File 'lib/dnb/direct/plus.rb', line 9

def api_base_url
  @api_base_url
end

.api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/dnb/direct/plus.rb', line 9

def api_key
  @api_key
end

.api_secretObject

Returns the value of attribute api_secret.



9
10
11
# File 'lib/dnb/direct/plus.rb', line 9

def api_secret
  @api_secret
end

.connObject

Returns the value of attribute conn.



9
10
11
# File 'lib/dnb/direct/plus.rb', line 9

def conn
  @conn
end

.tokenObject

Returns the value of attribute token.



9
10
11
# File 'lib/dnb/direct/plus.rb', line 9

def token
  @token
end

Class Method Details

.api_authObject

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.

Raises:

  • (ArgumentError)


14
15
16
17
18
# 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_tokenObject

Fetches the token from the authorization service.



26
27
28
29
30
31
32
33
34
35
36
# 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

.connectionObject

def api_token



38
39
40
41
42
43
44
# 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) ⇒ Object

def api_auth



20
21
22
23
# File 'lib/dnb/direct/plus.rb', line 20

def use_credentials key, secret
  @api_key = key
  @api_secret = secret
end