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



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

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

#access_tokenObject



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

Returns:

  • (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_inObject



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

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

#expiryObject



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_typeObject



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

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