module CTAAggregatorClient::API::Authenticator

Constants

THREE_HOURS

Public Class Methods

auth() click to toggle source
# File lib/cta_aggregator_client/api/authenticator.rb, line 17
def auth
  @auth ||= generate_auth
end
default_token_expiration() click to toggle source
# File lib/cta_aggregator_client/api/authenticator.rb, line 31
def default_token_expiration
  # API is more lenient on token expriation (way over 3 hours).
  # We're keeping low expiration here to avoid dealing with expired tokens.
  Time.now + THREE_HOURS
end
generate_auth() click to toggle source
# File lib/cta_aggregator_client/api/authenticator.rb, line 21
def generate_auth
  raw_response = RestClient.post(auth_url, nil, headers_with_auth_creds)
  token = raw_response.code == 201 ? JSON.parse(raw_response.body)["jwt"] : nil

  Auth.new(
    token,
    default_token_expiration
  )
end
headers_with_authentication() click to toggle source
# File lib/cta_aggregator_client/api/authenticator.rb, line 12
def headers_with_authentication 
  reset_auth unless Time.now < auth.expiration
  default_headers.merge(authorization: "Bearer: #{auth.token}")
end
reset_auth() click to toggle source
# File lib/cta_aggregator_client/api/authenticator.rb, line 37
def reset_auth
  @auth = nil
end