Class: WeixinAuthorize::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/weixin_authorize/adapter/storage.rb

Direct Known Subclasses

ClientStorage, RedisStorage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Storage

Returns a new instance of Storage.



8
9
10
# File 'lib/weixin_authorize/adapter/storage.rb', line 8

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



6
7
8
# File 'lib/weixin_authorize/adapter/storage.rb', line 6

def client
  @client
end

Class Method Details

.init_with(client) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/weixin_authorize/adapter/storage.rb', line 12

def self.init_with(client)
  if WeixinAuthorize.config.redis.nil?
    ClientStorage.new(client)
  else
    RedisStorage.new(client)
  end
end

Instance Method Details

#access_tokenObject



38
39
40
# File 'lib/weixin_authorize/adapter/storage.rb', line 38

def access_token
  authenticate if token_expired?
end

#authenticateObject



33
34
35
36
# File 'lib/weixin_authorize/adapter/storage.rb', line 33

def authenticate
  raise "APPID or APPSECRET is invalid" if !valid?
  set_access_token_for_client
end

#authenticate_headersObject



52
53
54
# File 'lib/weixin_authorize/adapter/storage.rb', line 52

def authenticate_headers
  {grant_type: "client_credential", appid: client.app_id, secret: client.app_secret}
end

#http_get_access_tokenObject



48
49
50
# File 'lib/weixin_authorize/adapter/storage.rb', line 48

def http_get_access_token
  WeixinAuthorize.http_get_without_token("/token", authenticate_headers)
end

#set_access_token_for_client(access_token_infos = nil) ⇒ Object



42
43
44
45
46
# File 'lib/weixin_authorize/adapter/storage.rb', line 42

def set_access_token_for_client(access_token_infos=nil)
  token_infos = access_token_infos || http_get_access_token
  client.access_token = token_infos["access_token"]
  client.expired_at   = Time.now.to_i + token_infos["expires_in"].to_i
end

#token_expired?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/weixin_authorize/adapter/storage.rb', line 29

def token_expired?
  raise NotImplementedError, "Subclasses must implement a token_expired? method"
end

#valid?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
# File 'lib/weixin_authorize/adapter/storage.rb', line 20

def valid?
  valid_result = http_get_access_token
  if valid_result.keys.include?("access_token")
    set_access_token_for_client(valid_result)
    return true
  end
  false
end