Class: Dingtalk::Token::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/dingtalk/token/store.rb

Direct Known Subclasses

ObjectStore, RedisStore

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Store

Returns a new instance of Store.



5
6
7
# File 'lib/dingtalk/token/store.rb', line 5

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/dingtalk/token/store.rb', line 4

def client
  @client
end

Class Method Details

.init_with(client) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/dingtalk/token/store.rb', line 9

def self.init_with(client)
  if Dingtalk.redis.nil?
    ObjectStore.new(client)
  else
    RedisStore.new(client)
  end
end

Instance Method Details

#access_tokenObject



36
37
38
# File 'lib/dingtalk/token/store.rb', line 36

def access_token
  refresh_token if token_expired?
end

#authenticateObject



26
27
28
29
30
31
32
33
34
# File 'lib/dingtalk/token/store.rb', line 26

def authenticate
  auth_result = http_get_access_token
  auth = false
  if auth_result.is_ok?
    set_access_token(auth_result.result)
    auth = true
  end
  {"valid" => auth, "handler" => auth_result}
end

#authenticate_headersObject



54
55
56
# File 'lib/dingtalk/token/store.rb', line 54

def authenticate_headers
  {corpid: client.app_id, corpsecret: client.app_secret}
end

#http_get_access_tokenObject



50
51
52
# File 'lib/dingtalk/token/store.rb', line 50

def http_get_access_token
  Dingtalk.http_get_without_token("/gettoken", authenticate_headers)
end

#refresh_tokenObject



21
22
23
24
# File 'lib/dingtalk/token/store.rb', line 21

def refresh_token
  handle_valid_exception
  set_access_token
end

#set_access_token(access_token_infos = nil) ⇒ Object



40
41
42
43
44
# File 'lib/dingtalk/token/store.rb', line 40

def set_access_token(access_token_infos=nil)
  token_infos = access_token_infos || http_get_access_token.result
  client.access_token = token_infos["access_token"]
  client.expired_at = Dingtalk.calculate_expire(token_infos["expires_in"])
end

#token_expired?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/dingtalk/token/store.rb', line 46

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

#valid?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/dingtalk/token/store.rb', line 17

def valid?
  authenticate["valid"]
end