Class: Payabli::Internal::OAuthProvider
- Inherits:
-
Object
- Object
- Payabli::Internal::OAuthProvider
- Defined in:
- lib/payabli/internal/oauth_provider.rb
Constant Summary collapse
- BUFFER_IN_SECONDS =
2 minutes
120
Instance Method Summary collapse
-
#auth_headers ⇒ Hash[String, String]
Returns the authentication headers to be included in requests.
- #initialize(auth_client:, options:) ⇒ void constructor
-
#token ⇒ String
Returns a cached access token, refreshing if necessary.
Constructor Details
#initialize(auth_client:, options:) ⇒ void
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 = @mutex = Mutex.new @access_token = nil @expires_at = nil end |
Instance Method Details
#auth_headers ⇒ Hash[String, String]
Returns the authentication headers to be included in requests.
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 |
#token ⇒ String
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.
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 |