Class: GithubAuthentication::Token
- Inherits:
-
Object
- Object
- GithubAuthentication::Token
- Defined in:
- lib/github_authentication/token.rb
Instance Attribute Summary collapse
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
Class Method Summary collapse
Instance Method Summary collapse
- #expired?(seconds_ttl: 300) ⇒ Boolean
- #expires_in ⇒ Object
-
#initialize(token, expires_at) ⇒ Token
constructor
A new instance of Token.
- #inspect ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object (also: #to_str)
- #valid_for?(ttl) ⇒ Boolean
Constructor Details
#initialize(token, expires_at) ⇒ Token
Returns a new instance of Token.
20 21 22 23 |
# File 'lib/github_authentication/token.rb', line 20 def initialize(token, expires_at) @token = token @expires_at = expires_at end |
Instance Attribute Details
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
7 8 9 |
# File 'lib/github_authentication/token.rb', line 7 def expires_at @expires_at end |
Class Method Details
.from_json(data) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/github_authentication/token.rb', line 10 def from_json(data) return if data.nil? token, expires_at = JSON.parse(data).values_at("token", "expires_at") new(token, Time.iso8601(expires_at)) rescue JSON::ParserError nil end |
Instance Method Details
#expired?(seconds_ttl: 300) ⇒ Boolean
29 30 31 |
# File 'lib/github_authentication/token.rb', line 29 def expired?(seconds_ttl: 300) @expires_at < Time.now.utc + seconds_ttl end |
#expires_in ⇒ Object
25 26 27 |
# File 'lib/github_authentication/token.rb', line 25 def expires_in (@expires_at - Time.now.utc).to_i end |
#inspect ⇒ Object
37 38 39 40 |
# File 'lib/github_authentication/token.rb', line 37 def inspect # Truncating the token should be enough not to leak it in error messages etc "#<#{self.class.name} @token=#{truncate(@token, 10)} @expires_at=#{@expires_at}>" end |
#to_json ⇒ Object
42 43 44 |
# File 'lib/github_authentication/token.rb', line 42 def to_json JSON.dump(token: @token, expires_at: @expires_at.iso8601) end |
#to_s ⇒ Object Also known as: to_str
46 47 48 |
# File 'lib/github_authentication/token.rb', line 46 def to_s @token end |
#valid_for?(ttl) ⇒ Boolean
33 34 35 |
# File 'lib/github_authentication/token.rb', line 33 def valid_for?(ttl) !expired?(seconds_ttl: ttl) end |