Class: Basecamp::Oauth::Token

Inherits:
Data
  • Object
show all
Defined in:
lib/basecamp/oauth/token.rb

Overview

OAuth 2 access token response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token:, token_type: "Bearer", refresh_token: nil, expires_in: nil, expires_at: nil, scope: nil) ⇒ Token

Returns a new instance of Token.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/basecamp/oauth/token.rb', line 21

def initialize(
  access_token:,
  token_type: "Bearer",
  refresh_token: nil,
  expires_in: nil,
  expires_at: nil,
  scope: nil
)
  # Calculate expires_at from expires_in if not provided
  calculated_expires_at = expires_at || (expires_in ? Time.now + expires_in : nil)
  super(
    access_token: access_token,
    token_type: token_type,
    refresh_token: refresh_token,
    expires_in: expires_in,
    expires_at: calculated_expires_at,
    scope: scope
  )
end

Instance Attribute Details

#access_tokenString (readonly)

The access token string

Returns:

  • (String)

    the current value of access_token



13
14
15
# File 'lib/basecamp/oauth/token.rb', line 13

def access_token
  @access_token
end

#expires_atTime? (readonly)

Calculated expiration time

Returns:

  • (Time, nil)

    the current value of expires_at



13
14
15
# File 'lib/basecamp/oauth/token.rb', line 13

def expires_at
  @expires_at
end

#expires_inInteger? (readonly)

Lifetime of the access token in seconds

Returns:

  • (Integer, nil)

    the current value of expires_in



13
14
15
# File 'lib/basecamp/oauth/token.rb', line 13

def expires_in
  @expires_in
end

#refresh_tokenString? (readonly)

The refresh token string

Returns:

  • (String, nil)

    the current value of refresh_token



13
14
15
# File 'lib/basecamp/oauth/token.rb', line 13

def refresh_token
  @refresh_token
end

#scopeString? (readonly)

OAuth scope granted

Returns:

  • (String, nil)

    the current value of scope



13
14
15
# File 'lib/basecamp/oauth/token.rb', line 13

def scope
  @scope
end

#token_typeString (readonly)

Token type (usually "Bearer")

Returns:

  • (String)

    the current value of token_type



13
14
15
# File 'lib/basecamp/oauth/token.rb', line 13

def token_type
  @token_type
end

Instance Method Details

#expired?(buffer_seconds = 60) ⇒ Boolean

Checks if the token is expired or about to expire.

Parameters:

  • buffer_seconds (Integer) (defaults to: 60)

    Buffer time before actual expiration (default: 60)

Returns:

  • (Boolean)

    true if expired or will expire within buffer time



45
46
47
48
49
# File 'lib/basecamp/oauth/token.rb', line 45

def expired?(buffer_seconds = 60)
  return false unless expires_at

  Time.now + buffer_seconds >= expires_at
end