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) ⇒ AccessTokenBase

Returns a new instance of AccessTokenBase.



6
7
8
9
10
11
12
# File 'lib/wechat/token/access_token_base.rb', line 6

def initialize(client, appid, secret, token_file)
  @appid = appid
  @secret = secret
  @client = client
  @token_file = token_file
  @random_generator = Random.new
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



4
5
6
# File 'lib/wechat/token/access_token_base.rb', line 4

def access_token
  @access_token
end

#appidObject (readonly)

Returns the value of attribute appid.



4
5
6
# File 'lib/wechat/token/access_token_base.rb', line 4

def appid
  @appid
end

#clientObject (readonly)

Returns the value of attribute client.



4
5
6
# File 'lib/wechat/token/access_token_base.rb', line 4

def client
  @client
end

#got_token_atObject (readonly)

Returns the value of attribute got_token_at.



4
5
6
# File 'lib/wechat/token/access_token_base.rb', line 4

def got_token_at
  @got_token_at
end

#secretObject (readonly)

Returns the value of attribute secret.



4
5
6
# File 'lib/wechat/token/access_token_base.rb', line 4

def secret
  @secret
end

#token_fileObject (readonly)

Returns the value of attribute token_file.



4
5
6
# File 'lib/wechat/token/access_token_base.rb', line 4

def token_file
  @token_file
end

#token_life_in_secondsObject (readonly)

Returns the value of attribute token_life_in_seconds.



4
5
6
# File 'lib/wechat/token/access_token_base.rb', line 4

def token_life_in_seconds
  @token_life_in_seconds
end

Instance Method Details

#read_tokenObject



40
41
42
# File 'lib/wechat/token/access_token_base.rb', line 40

def read_token
  JSON.parse(File.read(token_file))
end

#tokenObject



14
15
16
17
18
19
# File 'lib/wechat/token/access_token_base.rb', line 14

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



44
45
46
# File 'lib/wechat/token/access_token_base.rb', line 44

def write_token(token_hash)
  File.write(token_file, token_hash.to_json)
end