Class: PDND::TokenManager
- Inherits:
-
Object
- Object
- PDND::TokenManager
- Defined in:
- lib/pdnd-ruby-client/token_manager.rb
Overview
Gestisce caricamento e salvataggio del token su file esterno.
Instance Attribute Summary collapse
-
#exp ⇒ Object
readonly
Returns the value of attribute exp.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
-
#initialize(path = 'tmp/pdnd_token.json') ⇒ TokenManager
constructor
A new instance of TokenManager.
- #load ⇒ Object
- #save(token, exp) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(path = 'tmp/pdnd_token.json') ⇒ TokenManager
Returns a new instance of TokenManager.
11 12 13 14 15 |
# File 'lib/pdnd-ruby-client/token_manager.rb', line 11 def initialize(path = 'tmp/pdnd_token.json') @path = path @token = nil @exp = nil end |
Instance Attribute Details
#exp ⇒ Object (readonly)
Returns the value of attribute exp.
9 10 11 |
# File 'lib/pdnd-ruby-client/token_manager.rb', line 9 def exp @exp end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/pdnd-ruby-client/token_manager.rb', line 9 def path @path end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
9 10 11 |
# File 'lib/pdnd-ruby-client/token_manager.rb', line 9 def token @token end |
Instance Method Details
#load ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/pdnd-ruby-client/token_manager.rb', line 17 def load return unless File.exist?(@path) data = JSON.parse(File.read(@path)) @token = data['token'] @exp = data['exp'] [@token, @exp] end |
#save(token, exp) ⇒ Object
26 27 28 29 30 |
# File 'lib/pdnd-ruby-client/token_manager.rb', line 26 def save(token, exp) @token = token @exp = exp File.write(@path, { token: token, exp: exp }.to_json) end |
#valid? ⇒ Boolean
32 33 34 |
# File 'lib/pdnd-ruby-client/token_manager.rb', line 32 def valid? @token.present? && @exp.present? && (Time.now.to_i < Time.new(@exp).to_i) end |