Class: Trustpilot::Auth::Authenticator

Inherits:
Object
  • Object
show all
Defined in:
lib/trustpilot/auth/authenticator.rb

Instance Method Summary collapse

Constructor Details

#initializeAuthenticator

Returns a new instance of Authenticator.

Raises:



8
9
10
11
12
# File 'lib/trustpilot/auth/authenticator.rb', line 8

def initialize
  return if Trustpilot.api_email && Trustpilot.api_password

  raise Trustpilot::Error, 'Trustpilot.api_email / Trustpilot.api_password are not set.'
end

Instance Method Details

#auth_headerObject

Returns the value of the authentication header to use when requesting an access token



35
36
37
38
39
40
41
# File 'lib/trustpilot/auth/authenticator.rb', line 35

def auth_header
  unless Trustpilot.api_key && Trustpilot.api_secret
    raise Trustpilot::Error, 'Trustpilot.api_key / Trustpilot.api_secret are not set.'
  end

  "Basic #{ Base64.strict_encode64( "#{ Trustpilot.api_key }:#{ Trustpilot.api_secret }" ) }"
end

#request_tokenObject

Gets a new access token from the API



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/trustpilot/auth/authenticator.rb', line 15

def request_token
  request = Request.new(
    'oauth/oauth-business-users-for-applications/accesstoken',
    headers: {
      'Authorization' => auth_header,
      'Content-Type' => 'application/x-www-form-urlencoded'
    },
    params: {
      grant_type: 'password',
      username: Trustpilot.api_email,
      password: Trustpilot.api_password
    },
    verb: 'post'
  )

  Api.request request
end