Class: GithubAuthentication::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/github_authentication/token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_atObject (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

Returns:

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



25
26
27
# File 'lib/github_authentication/token.rb', line 25

def expires_in
  (@expires_at - Time.now.utc).to_i
end

#inspectObject



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_jsonObject



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_sObject 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

Returns:

  • (Boolean)


33
34
35
# File 'lib/github_authentication/token.rb', line 33

def valid_for?(ttl)
  !expired?(seconds_ttl: ttl)
end