Class: Aliexpress::Token::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/aliexpress/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.


8
9
10
# File 'lib/aliexpress/token/store.rb', line 8

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject

Returns the value of attribute client


6
7
8
# File 'lib/aliexpress/token/store.rb', line 6

def client
  @client
end

Class Method Details

.init_with(client) ⇒ Object


12
13
14
15
16
17
18
# File 'lib/aliexpress/token/store.rb', line 12

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

Instance Method Details

#access_tokenObject


39
40
41
# File 'lib/aliexpress/token/store.rb', line 39

def access_token
  refresh_token if token_expired?
end

#authenticateObject


24
25
26
27
28
29
30
31
32
# File 'lib/aliexpress/token/store.rb', line 24

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


57
58
59
# File 'lib/aliexpress/token/store.rb', line 57

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

#http_get_access_tokenObject


53
54
55
# File 'lib/aliexpress/token/store.rb', line 53

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

#refresh_tokenObject


34
35
36
37
# File 'lib/aliexpress/token/store.rb', line 34

def refresh_token
  handle_valid_exception
  set_access_token
end

#set_access_token(access_token_infos = nil) ⇒ Object


47
48
49
50
51
# File 'lib/aliexpress/token/store.rb', line 47

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   = Time.now.to_i + token_infos["expires_in"].to_i
end

#token_expired?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)

43
44
45
# File 'lib/aliexpress/token/store.rb', line 43

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

#valid?Boolean

Returns:

  • (Boolean)

20
21
22
# File 'lib/aliexpress/token/store.rb', line 20

def valid?
  authenticate["valid"]
end