class Lark::TokenStore::Base

Attributes

client[RW]

Public Class Methods

new(client) click to toggle source
# File lib/lark/token_store/base.rb, line 6
def initialize(client)
  @client = client
  raise RedisNotConfigException if redis.nil?
end

Public Instance Methods

token() click to toggle source
# File lib/lark/token_store/base.rb, line 11
def token
  update_token if expired?
  redis.hget(redis_key, token_key)
end
update_token() click to toggle source
# File lib/lark/token_store/base.rb, line 20
def update_token
  data = fetch_token.data
  value = data[token_key]
  if value.nil?
    Lark.logger.error "#{self.class.name} fetch token error: #{data.inspect}"
  else
    expires_at = Time.now.to_i + data['expire'].to_i - 120
    redis.hmset(
      redis_key,
      token_key, value,
      'expires_at', expires_at
    )
    redis.expireat(redis_key, expires_at)
  end

  value
end
valid?() click to toggle source
# File lib/lark/token_store/base.rb, line 16
def valid?
  token.present?
end

Private Instance Methods

expired?() click to toggle source
# File lib/lark/token_store/base.rb, line 56
def expired?
  redis.hvals(redis_key).empty? || redis.hget(redis_key, 'expires_at').to_i <= Time.now.to_i
end
fetch_token() click to toggle source
# File lib/lark/token_store/base.rb, line 40
def fetch_token
  raise NotImplementedError
end
redis() click to toggle source
# File lib/lark/token_store/base.rb, line 48
def redis
  Lark.redis
end
redis_key() click to toggle source
# File lib/lark/token_store/base.rb, line 52
def redis_key
  @redis_key ||= Digest::MD5.hexdigest "#{self.class.name}_#{client.app_id}_#{client.app_secret}"
end
token_key() click to toggle source
# File lib/lark/token_store/base.rb, line 44
def token_key
  raise NotImplementedError
end