Class: ChangeHealth::Authentication
- Inherits:
-
Object
- Object
- ChangeHealth::Authentication
- Defined in:
- lib/change_health/authentication.rb
Constant Summary collapse
- AUTH_ENDPOINT =
'/apip/auth/v2/token'.freeze
Instance Attribute Summary collapse
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
- #access_header ⇒ Object
- #access_token ⇒ Object
- #authenticate(base_uri: nil) ⇒ Object
- #expire! ⇒ Object
- #expires?(seconds_from_now = 60) ⇒ Boolean
- #expires_in ⇒ Object
- #expiry ⇒ Object
-
#initialize ⇒ Authentication
constructor
A new instance of Authentication.
- #token_type ⇒ Object
Constructor Details
#initialize ⇒ Authentication
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
#response ⇒ Object
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_header ⇒ Object
58 59 60 61 62 |
# File 'lib/change_health/authentication.rb', line 58 def access_header return { 'Authorization' => "Bearer #{self.access_token}", } end |
#access_token ⇒ Object
34 35 36 |
# File 'lib/change_health/authentication.rb', line 34 def access_token return @response['access_token'] if @response end |
#authenticate(base_uri: nil) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/change_health/authentication.rb', line 12 def authenticate(base_uri: nil) if (self.expires?) base_uri ||= Connection.base_uri 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, base_uri: base_uri) 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
64 65 66 |
# File 'lib/change_health/authentication.rb', line 64 def expire! @response = nil end |
#expires?(seconds_from_now = 60) ⇒ Boolean
50 51 52 53 54 55 56 |
# File 'lib/change_health/authentication.rb', line 50 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 ⇒ Object
38 39 40 |
# File 'lib/change_health/authentication.rb', line 38 def expires_in return @response['expires_in'].to_i if @response end |
#expiry ⇒ Object
46 47 48 |
# File 'lib/change_health/authentication.rb', line 46 def expiry @request_time + self.expires_in if @request_time && self.expires_in end |
#token_type ⇒ Object
42 43 44 |
# File 'lib/change_health/authentication.rb', line 42 def token_type return @response['token_type'] if @response end |