class SlsAdf::Util::Token

Token used to store the ADF API's client credentials token. This is used by SlsAdf::Util::Adapter to make API calls to ADF.

The token will be an empty string if:

i) The get_token call is unsuccessful (HTTP status code not 200).
ii) The credentials are invalid.

Public Class Methods

refresh_token() click to toggle source

Forces an API call to get the token.

@return [String] The ADF API token.

# File lib/sls_adf/util/token.rb, line 26
def refresh_token
  @token = get_token
end
token() click to toggle source

Returns the token. If no token exists, an API call is made to get the token.

@return [String] The ADF API token.

# File lib/sls_adf/util/token.rb, line 19
def token
  @token ||= get_token
end

Private Class Methods

body() click to toggle source
# File lib/sls_adf/util/token.rb, line 61
def body
  JSON.dump(
    clientId: client_id,
    clientSecret: client_secret,
    grantType: 'client_credentials',
    scope: 'all'
  )
end
client_id() click to toggle source
# File lib/sls_adf/util/token.rb, line 70
def client_id
  SlsAdf.configuration.client_id
end
client_secret() click to toggle source
# File lib/sls_adf/util/token.rb, line 74
def client_secret
  SlsAdf.configuration.client_secret
end
get_token() click to toggle source

Returns the token after making an API call to get the token. An empty string is returned if:

i) The call is unsuccessful (HTTP status code not 200).
ii) The credentials are invalid.

@return [String] The responded token, or an empty string.

# File lib/sls_adf/util/token.rb, line 38
def get_token # rubocop:disable Style/AccessorMethodName
  response = get_token_call
  response.code == 200 ? parse_response(response.body) : ''
end
get_token_call() click to toggle source

Returns the response for a POST token API call.

@return [Typhoeus::Response] The response object.

# File lib/sls_adf/util/token.rb, line 46
def get_token_call # rubocop:disable Style/AccessorMethodName
  Typhoeus.post(get_token_url, headers: COMMON_HEADERS, body: body)
end
get_token_url() click to toggle source
# File lib/sls_adf/util/token.rb, line 78
def get_token_url # rubocop:disable Style/AccessorMethodName
  SlsAdf.configuration.get_token_url
end
parse_response(body) click to toggle source

Attempts to parse the string in Json to obtain the token.

@param [String] body String to be parsed @return [String] The parsed token, or blank if an error occurs.

# File lib/sls_adf/util/token.rb, line 54
def parse_response(body)
  token = JSON.parse(body)['data']['token']
  token ? token : ''
rescue JSON::ParserError
  ''
end