Class: Wechat::Token::AccessTokenBase

Inherits:
Object
  • Object
show all
Defined in:
lib/wechat/token/access_token_base.rb,
lib/generators/wechat/templates/config/initializers/wechat_redis_store.rb

Direct Known Subclasses

CorpAccessToken, PublicAccessToken

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, appid, secret, token_file, record = nil) ⇒ AccessTokenBase

Returns a new instance of AccessTokenBase.



8
9
10
11
12
13
14
15
# 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

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



6
7
8
# File 'lib/wechat/token/access_token_base.rb', line 6

def access_token
  @access_token
end

#appidObject (readonly)

Returns the value of attribute appid.



6
7
8
# File 'lib/wechat/token/access_token_base.rb', line 6

def appid
  @appid
end

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/wechat/token/access_token_base.rb', line 6

def client
  @client
end

#got_token_atObject (readonly)

Returns the value of attribute got_token_at.



6
7
8
# File 'lib/wechat/token/access_token_base.rb', line 6

def got_token_at
  @got_token_at
end

#recordObject (readonly)

Returns the value of attribute record.



6
7
8
# File 'lib/wechat/token/access_token_base.rb', line 6

def record
  @record
end

#secretObject (readonly)

Returns the value of attribute secret.



6
7
8
# File 'lib/wechat/token/access_token_base.rb', line 6

def secret
  @secret
end

#token_fileObject (readonly)

Returns the value of attribute token_file.



6
7
8
# File 'lib/wechat/token/access_token_base.rb', line 6

def token_file
  @token_file
end

#token_life_in_secondsObject (readonly)

Returns the value of attribute token_life_in_seconds.



6
7
8
# File 'lib/wechat/token/access_token_base.rb', line 6

def token_life_in_seconds
  @token_life_in_seconds
end

Instance Method Details

#read_tokenObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wechat/token/access_token_base.rb', line 43

def read_token
  if record_based_token?
    throw_error_if_missing_attributes!

    {
      'access_token' => record.access_token,
      'got_token_at' => record.got_token_at,
      'token_expires_in' => record.token_expires_in
    }
  else
    JSON.parse(File.read(token_file))
  end
end

#tokenObject



17
18
19
20
21
22
# 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) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/wechat/token/access_token_base.rb', line 57

def write_token(token_hash)
  if record_based_token?
    write_token_to_record(token_hash)
  else
    File.write(token_file, token_hash.to_json)
  end
end