class Wework::Token::Base

Attributes

client[RW]

Public Class Methods

new(client) click to toggle source
# File lib/wework/token/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/wework/token/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/wework/token/base.rb, line 16
def update_token
  result = refresh_token
  value = result.send(token_key)
  if value.nil?
    puts "#{self.class.name} refresh token error: #{result.inspect}"
  else
    expires_at = Time.now.to_i + result.expires_in.to_i - Wework.expired_shift_seconds

    redis.hmset(
      redis_key,
      token_key, value,
      "expires_at", expires_at
    )

    redis.expireat(redis_key, expires_at)
  end

  value
end

Private Instance Methods

expired?() click to toggle source
# File lib/wework/token/base.rb, line 54
def expired?
  redis.hvals(redis_key).empty? || redis.hget(redis_key, 'expires_at').to_i <= Time.now.to_i
end
redis() click to toggle source
# File lib/wework/token/base.rb, line 38
def redis
  Wework.redis
end
redis_key() click to toggle source
# File lib/wework/token/base.rb, line 42
def redis_key
  raise NotImplementedError
end
refresh_token() click to toggle source
# File lib/wework/token/base.rb, line 50
def refresh_token
  raise NotImplementedError
end
token_key() click to toggle source
# File lib/wework/token/base.rb, line 46
def token_key
  raise NotImplementedError
end