Class: XiWechatCorp::API::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/xi_wechat_corp/api/access_token.rb

Constant Summary collapse

EXPIRE_SECONDS =

Leave 5 minutes buffer

7200 - 300

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token = nil, expired_at = nil) ⇒ AccessToken

Returns a new instance of AccessToken.



10
11
12
# File 'lib/xi_wechat_corp/api/access_token.rb', line 10

def initialize(token = nil, expired_at = nil)
  assign(token, expired_at)
end

Instance Attribute Details

#expired_atObject (readonly)

Returns the value of attribute expired_at.



8
9
10
# File 'lib/xi_wechat_corp/api/access_token.rb', line 8

def expired_at
  @expired_at
end

#tokenObject (readonly)

Returns the value of attribute token.



7
8
9
# File 'lib/xi_wechat_corp/api/access_token.rb', line 7

def token
  @token
end

Instance Method Details

#assign(token, expired_at = nil) ⇒ Object Also known as: refresh



14
15
16
17
18
19
20
21
# File 'lib/xi_wechat_corp/api/access_token.rb', line 14

def assign(token, expired_at = nil)
  @token = token.to_s
  if !@token.empty?
    @expired_at = expired_at || (token.respond_to?(:expired_at) ? token.expired_at : Time.now.to_i + EXPIRE_SECONDS)
  else
    @expired_at = 0
  end
end

#expired?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/xi_wechat_corp/api/access_token.rb', line 29

def expired?
  @token.empty? || @expired_at < Time.now.to_i
end

#present?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/xi_wechat_corp/api/access_token.rb', line 25

def present?
  @token.size > 0
end

#to_sObject



37
38
39
# File 'lib/xi_wechat_corp/api/access_token.rb', line 37

def to_s
  @token
end

#valid?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/xi_wechat_corp/api/access_token.rb', line 33

def valid?
  !expired?
end