Class: Baiwang::TokenStore::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/baiwang/token_store/base.rb

Direct Known Subclasses

AppToken

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Base

Returns a new instance of Base.



6
7
8
9
# File 'lib/baiwang/token_store/base.rb', line 6

def initialize(client)
  @client = client
  raise RedisNotConfigException if redis.nil?
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/baiwang/token_store/base.rb', line 4

def client
  @client
end

Instance Method Details

#tokenObject



11
12
13
14
# File 'lib/baiwang/token_store/base.rb', line 11

def token
  update_token if expired?
  redis.hget(redis_key, token_key)
end

#update_tokenObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/baiwang/token_store/base.rb', line 20

def update_token
  data = fetch_token.data
  value = data[token_key]
  if value.nil?
    Baiwang.logger.error "#{self.class.name} fetch token error: #{data.inspect}"
  else
    expires_at = Time.now.to_i + data['expires_in'].to_i - 120
    redis.hmset(
      redis_key,
      token_key, value,
      'expires_at', expires_at
    )
    redis.expireat(redis_key, expires_at)
  end

  value
end

#valid?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/baiwang/token_store/base.rb', line 16

def valid?
  token.present?
end