Module: BlizzardApi::TokenManager

Included in:
BlizzardApi
Defined in:
lib/blizzard_api/token_manager.rb

Overview

Manages the current token and expiration date

Constant Summary collapse

REDIS_TOKEN_KEY =
'access_token'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_tokenString

Current access token.

Returns:

  • (String)

    access_token



13
14
15
# File 'lib/blizzard_api/token_manager.rb', line 13

def access_token
  @access_token
end

#expires_atString

Current access token expiration date.

Returns:

  • (String)

    expires_at



19
20
21
# File 'lib/blizzard_api/token_manager.rb', line 19

def expires_at
  @expires_at
end

Instance Method Details

#access_token_expired?Boolean

Returns if the current token has expired

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/blizzard_api/token_manager.rb', line 25

def access_token_expired?
  return true if access_token.nil?

  expires_at < Time.now
end

#restore_access_tokenObject



31
32
33
34
35
36
37
38
39
# File 'lib/blizzard_api/token_manager.rb', line 31

def restore_access_token
  return false unless use_cache? && cache_access_token

  return false unless redis_connection.exists? REDIS_TOKEN_KEY

  self.access_token = redis_connection.get REDIS_TOKEN_KEY
  self.expires_at = Time.now + redis_connection.ttl(REDIS_TOKEN_KEY)
  true
end

#save_access_token(token_data) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/blizzard_api/token_manager.rb', line 41

def save_access_token(token_data)
  ttl = token_data['expires_in'].to_i
  self.expires_at = Time.now + ttl
  self.access_token = token_data['access_token']

  redis_connection.setex REDIS_TOKEN_KEY, ttl, access_token if use_cache? && cache_access_token
end