class ChangeHealth::Authentication

Constants

AUTH_ENDPOINT

Attributes

response[RW]

Public Class Methods

new() click to toggle source
# File lib/change_health/authentication.rb, line 7
def initialize
  @response     = nil
  @request_time = nil
end

Public Instance Methods

access_header() click to toggle source
# File lib/change_health/authentication.rb, line 57
def access_header
  return {
    'Authorization' => "Bearer #{self.access_token}",
  }
end
access_token() click to toggle source
# File lib/change_health/authentication.rb, line 33
def access_token
  return @response['access_token'] if @response
end
authenticate() click to toggle source
# File lib/change_health/authentication.rb, line 12
def authenticate
  if (self.expires?)
    request = {
      body: { client_id: ChangeHealth.configuration.client_id, client_secret: ChangeHealth.configuration.client_secret, grant_type: ChangeHealth.configuration.grant_type },
      endpoint: AUTH_ENDPOINT
    }

    response = Connection.new.request(**request, auth: false)

    if (false == response.ok?)
      @response = nil
      raise ChangeHealthException.from_response(response, msg: 'Authentication')
    else
      @request_time = Time.now
      @response = response
    end
  end

  return self
end
expire!() click to toggle source
# File lib/change_health/authentication.rb, line 63
def expire!
  @response = nil
end
expires?(seconds_from_now = 60) click to toggle source
# File lib/change_health/authentication.rb, line 49
def expires?(seconds_from_now = 60)
  if (self.expiry)
    return self.expiry.utc <= (Time.now + seconds_from_now).utc
  else
    return true
  end
end
expires_in() click to toggle source
# File lib/change_health/authentication.rb, line 37
def expires_in
  return @response['expires_in'].to_i if @response
end
expiry() click to toggle source
# File lib/change_health/authentication.rb, line 45
def expiry
  @request_time + self.expires_in if @request_time && self.expires_in
end
token_type() click to toggle source
# File lib/change_health/authentication.rb, line 41
def token_type
  return @response['token_type'] if @response
end