Class: Wework::Token::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/wework/token/base.rb

Direct Known Subclasses

AppToken, CorpToken, JsTicket, ProviderToken, SuiteToken

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Base

Returns a new instance of Base.



6
7
8
9
# File 'lib/wework/token/base.rb', line 6

def initialize(client)
  @client = client
  raise RedisNotConfigException if redis.nil?
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



4
5
6
# File 'lib/wework/token/base.rb', line 4

def client
  @client
end

Instance Method Details

#tokenObject



11
12
13
14
# File 'lib/wework/token/base.rb', line 11

def token
  update_token if expired?
  redis.hget(redis_key, token_key)
end

#update_tokenObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/wework/token/base.rb', line 16

def update_token
  result = refresh_token
  value = result.send(token_key)
  if value.nil?
    puts "#{self.class.name} refresh token error: #{result.inspect}"
  else
    expires_at = Time.now.to_i + result.expires_in.to_i - Wework.expired_shift_seconds

    redis.hmset(
      redis_key,
      token_key, value,
      "expires_at", expires_at
    )

    redis.expireat(redis_key, expires_at)
  end

  value
end