Class: Quovo::Token
Instance Attribute Summary collapse
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#storage ⇒ Object
readonly
Returns the value of attribute storage.
-
#ttl ⇒ Object
readonly
Returns the value of attribute ttl.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #generate ⇒ Object
- #get ⇒ Object
-
#initialize(storage: Quovo.config.token_storage, ttl: Quovo.config.token_ttl, prefix: Quovo.config.token_prefix, username: Quovo.config.username, password: Quovo.config.password) ⇒ Token
constructor
A new instance of Token.
Methods included from Request
Constructor Details
#initialize(storage: Quovo.config.token_storage, ttl: Quovo.config.token_ttl, prefix: Quovo.config.token_prefix, username: Quovo.config.username, password: Quovo.config.password) ⇒ Token
Returns a new instance of Token.
6 7 8 9 10 11 12 13 14 |
# File 'lib/quovo/token.rb', line 6 def initialize(storage: Quovo.config.token_storage, ttl: Quovo.config.token_ttl, prefix: Quovo.config.token_prefix, username: Quovo.config.username, password: Quovo.config.password) @storage = storage @ttl = ttl @prefix = prefix @username = username @password = password @token = nil @expires = nil end |
Instance Attribute Details
#password ⇒ Object (readonly)
Returns the value of attribute password.
36 37 38 |
# File 'lib/quovo/token.rb', line 36 def password @password end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
36 37 38 |
# File 'lib/quovo/token.rb', line 36 def prefix @prefix end |
#storage ⇒ Object (readonly)
Returns the value of attribute storage.
36 37 38 |
# File 'lib/quovo/token.rb', line 36 def storage @storage end |
#ttl ⇒ Object (readonly)
Returns the value of attribute ttl.
36 37 38 |
# File 'lib/quovo/token.rb', line 36 def ttl @ttl end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
36 37 38 |
# File 'lib/quovo/token.rb', line 36 def username @username end |
Instance Method Details
#generate ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/quovo/token.rb', line 25 def generate params = { name: [prefix, expires_from_now, salt].join('---'), expires: expires_from_now } payload = request(:post, '/tokens', params, :json) do |req| req.basic_auth(username, password) end.fetch('access_token') [payload.fetch('token'), payload.fetch('expires').to_time.utc] end |
#get ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/quovo/token.rb', line 16 def get token, expires = read_cache if token.nil? || expires < now token, expires = generate write_cache(token, expires) end token end |