Class: DnB::Direct::Plus
- Inherits:
-
Object
- Object
- DnB::Direct::Plus
- 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
-
.api_base_url ⇒ Object
Returns the value of attribute api_base_url.
-
.api_key ⇒ Object
Returns the value of attribute api_key.
-
.api_secret ⇒ Object
Returns the value of attribute api_secret.
-
.conn ⇒ Object
Returns the value of attribute conn.
-
.token ⇒ Object
Returns the value of attribute token.
Class Method Summary collapse
-
.api_auth ⇒ Object
Constructs the authorization key by encoding {api_key}:{api_secret} to a base64 encoded string.
-
.api_token ⇒ Object
Fetches the token from the authorization service.
-
.connection ⇒ Object
def api_token.
-
.use_credentials(key, secret) ⇒ Object
def api_auth.
Class Attribute Details
.api_base_url ⇒ Object
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_key ⇒ Object
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_secret ⇒ Object
Returns the value of attribute api_secret.
9 10 11 |
# File 'lib/dnb/direct/plus.rb', line 9 def api_secret @api_secret end |
.conn ⇒ Object
Returns the value of attribute conn.
9 10 11 |
# File 'lib/dnb/direct/plus.rb', line 9 def conn @conn end |
.token ⇒ Object
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_auth ⇒ Object
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.
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_token ⇒ Object
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 |
.connection ⇒ Object
def api_token
38 39 40 41 42 43 44 |
# File 'lib/dnb/direct/plus.rb', line 38 def connection @conn ||= Faraday.new() 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 |