class Wechat::Token::AccessTokenBase
Attributes
access_token[R]
appid[R]
client[R]
got_token_at[R]
record[R]
secret[R]
token_file[R]
token_life_in_seconds[R]
Public Class Methods
new(client, appid, secret, token_file, record = nil)
click to toggle source
# File lib/wechat/token/access_token_base.rb, line 8 def initialize(client, appid, secret, token_file, record = nil) @appid = appid @secret = secret @client = client @token_file = token_file @record = record @random_generator = Random.new end
Public Instance Methods
read_token()
click to toggle source
# File lib/generators/wechat/templates/config/initializers/wechat_redis_store.rb, line 11 def read_token JSON.parse(Wechat.redis.get(redis_key)) || {} end
token()
click to toggle source
# File lib/wechat/token/access_token_base.rb, line 17 def token # Possible two worker running, one worker refresh token, other unaware, so must read every time read_token_from_store refresh if remain_life_seconds < @random_generator.rand(30..(3 * 60)) access_token end
write_token(token_hash)
click to toggle source
# File lib/generators/wechat/templates/config/initializers/wechat_redis_store.rb, line 15 def write_token(token_hash) Wechat.redis.set redis_key, token_hash.to_json end
Protected Instance Methods
read_token_from_store()
click to toggle source
# File lib/wechat/token/access_token_base.rb, line 26 def read_token_from_store td = read_token @token_life_in_seconds = td.fetch('token_expires_in').to_i @got_token_at = td.fetch('got_token_at').to_i @access_token = td.fetch('access_token') # return access_token same time rescue JSON::ParserError, Errno::ENOENT, KeyError, TypeError refresh end
remain_life_seconds()
click to toggle source
# File lib/wechat/token/access_token_base.rb, line 65 def remain_life_seconds token_life_in_seconds - (Time.now.to_i - got_token_at) end
write_token_to_store(token_hash)
click to toggle source
# File lib/wechat/token/access_token_base.rb, line 35 def write_token_to_store(token_hash) raise InvalidCredentialError unless token_hash.is_a?(Hash) && token_hash['access_token'] token_hash['got_token_at'] = Time.now.to_i token_hash['token_expires_in'] = token_hash.delete('expires_in') write_token(token_hash) end
Private Instance Methods
missing_necessary_attributes?()
click to toggle source
# File lib/wechat/token/access_token_base.rb, line 84 def missing_necessary_attributes? return true unless record.respond_to?(:access_token) return true unless record.respond_to?(:token_expires_in) return true unless record.respond_to?(:got_token_at) false end
record_based_token?()
click to toggle source
# File lib/wechat/token/access_token_base.rb, line 71 def record_based_token? record.present? end
redis_key()
click to toggle source
# File lib/generators/wechat/templates/config/initializers/wechat_redis_store.rb, line 21 def redis_key "my_app_wechat_token_#{secret}" end
throw_error_if_missing_attributes!()
click to toggle source
# File lib/wechat/token/access_token_base.rb, line 92 def throw_error_if_missing_attributes! raise "Missing attributes #access_token or #token_expires_in or #got_token_at in #{record.class.name}" if missing_necessary_attributes? end
write_token_to_record(token_hash)
click to toggle source
# File lib/wechat/token/access_token_base.rb, line 75 def write_token_to_record(token_hash) throw_error_if_missing_attributes! record.access_token = token_hash['access_token'] record.token_expires_in = token_hash['token_expires_in'] record.got_token_at = Time.now record.save || record.save(validate: false) end