Class: Payabli::Internal::OAuthProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/payabli/internal/oauth_provider.rb

Constant Summary collapse

BUFFER_IN_SECONDS =

2 minutes

120

Instance Method Summary collapse

Constructor Details

#initialize(auth_client:, options:) ⇒ void

Parameters:

  • auth_client (untyped)
  • options (Hash[String, untyped])


12
13
14
15
16
17
18
# File 'lib/payabli/internal/oauth_provider.rb', line 12

def initialize(auth_client:, options:)
  @auth_client = auth_client
  @options = options
  @mutex = Mutex.new
  @access_token = nil
  @expires_at = nil
end

Instance Method Details

#auth_headersHash[String, String]

Returns the authentication headers to be included in requests.

Returns:

  • (Hash[String, String])


38
39
40
41
42
43
# File 'lib/payabli/internal/oauth_provider.rb', line 38

def auth_headers
  access_token = token
  {
    Authorization: "Bearer #{access_token}"
  }
end

#tokenString

Returns a cached access token, refreshing if necessary. Refreshes the token if it's nil, or if we're within the buffer period before expiration. Only one thread refreshes the token at a time.

Returns:

  • (String)


25
26
27
28
29
30
31
32
33
# File 'lib/payabli/internal/oauth_provider.rb', line 25

def token
  return @access_token unless @access_token.nil? || token_needs_refresh?

  @mutex.synchronize do
    return @access_token unless @access_token.nil? || token_needs_refresh?

    refresh
  end
end