Class: PDND::TokenManager

Inherits:
Object
  • Object
show all
Defined in:
lib/pdnd-ruby-client/token_manager.rb

Overview

Gestisce caricamento e salvataggio del token su file esterno.

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#expObject (readonly)

Returns the value of attribute exp.



9
10
11
# File 'lib/pdnd-ruby-client/token_manager.rb', line 9

def exp
  @exp
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/pdnd-ruby-client/token_manager.rb', line 9

def path
  @path
end

#tokenObject (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

#loadObject



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

Returns:

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