class Youzanyun::Token::Store

Attributes

client[RW]

Public Class Methods

init_with(client) click to toggle source
# File lib/youzanyun/token/store.rb, line 13
def self.init_with(client)
  if Youzanyun.youzanyun_redis.nil?
    ObjectStore.new(client)
  else
    RedisStore.new(client)
  end
end
new(client) click to toggle source
# File lib/youzanyun/token/store.rb, line 9
def initialize(client)
  @client = client
end

Public Instance Methods

access_token() click to toggle source
# File lib/youzanyun/token/store.rb, line 40
def access_token
  refresh_token if token_expired?
end
authenticate() click to toggle source
# File lib/youzanyun/token/store.rb, line 25
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_headers() click to toggle source
# File lib/youzanyun/token/store.rb, line 58
def authenticate_headers
  {
    client_id: client.client_id,
    client_secret: client.client_secret,
    authorize_type: "silent",
    grant_id: client.grant_id
  }
end
http_get_access_token() click to toggle source
# File lib/youzanyun/token/store.rb, line 54
def http_get_access_token
  Youzanyun.http_post_without_token("https://open.youzanyun.com/auth/token", authenticate_headers, {}, CUSTOM_ENDPOINT)
end
refresh_token() click to toggle source
# File lib/youzanyun/token/store.rb, line 35
def refresh_token
  handle_valid_exception
  set_access_token
end
set_access_token(access_token_infos=nil) click to toggle source
# File lib/youzanyun/token/store.rb, line 48
def set_access_token(access_token_infos=nil)
  token_infos = access_token_infos || http_get_access_token.result
  client.access_token = token_infos["data"]["access_token"]
  client.expired_at = Youzanyun.calculate_expire(token_infos["data"]["expires"])
end
token_expired?() click to toggle source
# File lib/youzanyun/token/store.rb, line 44
def token_expired?
  raise NotImplementedError, "Subclasses must implement a token_expired? method"
end
valid?() click to toggle source
# File lib/youzanyun/token/store.rb, line 21
def valid?
  authenticate["valid"]
end

Private Instance Methods

handle_valid_exception() click to toggle source
# File lib/youzanyun/token/store.rb, line 69
def handle_valid_exception
  auth_result = authenticate
  if !auth_result["valid"]
    result_handler = auth_result["handler"]
    raise ValidAccessTokenException, result_handler.full_error_message
  end
end