Class: ChangeHealth::Authentication

Inherits:
Object
  • Object
show all
Defined in:
lib/change_health/authentication.rb

Constant Summary collapse

AUTH_ENDPOINT =
'/apip/auth/v2/token'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAuthentication

Returns a new instance of Authentication.



7
8
9
10
# File 'lib/change_health/authentication.rb', line 7

def initialize
  @response     = nil
  @request_time = nil
end

Instance Attribute Details

#responseObject

Returns the value of attribute response.



3
4
5
# File 'lib/change_health/authentication.rb', line 3

def response
  @response
end

Instance Method Details

#access_headerObject



57
58
59
60
61
# File 'lib/change_health/authentication.rb', line 57

def access_header
  return {
    'Authorization' => "Bearer #{self.access_token}",
  }
end

#access_tokenObject



33
34
35
# File 'lib/change_health/authentication.rb', line 33

def access_token
  return @response['access_token'] if @response
end

#authenticateObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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!Object



63
64
65
# File 'lib/change_health/authentication.rb', line 63

def expire!
  @response = nil
end

#expires?(seconds_from_now = 60) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
# 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_inObject



37
38
39
# File 'lib/change_health/authentication.rb', line 37

def expires_in
  return @response['expires_in'].to_i if @response
end

#expiryObject



45
46
47
# File 'lib/change_health/authentication.rb', line 45

def expiry
  @request_time + self.expires_in if @request_time && self.expires_in
end

#token_typeObject



41
42
43
# File 'lib/change_health/authentication.rb', line 41

def token_type
  return @response['token_type'] if @response
end