Class: Dianping::Api::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/dianping/api/token.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Token

Returns a new instance of Token.



8
9
10
11
# File 'lib/dianping/api/token.rb', line 8

def initialize(client)
  @client = client
  @token_file = File.join(client.token_root || 'tmp', "dianping-api-#{client.app_key}")
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/dianping/api/token.rb', line 6

def client
  @client
end

Instance Method Details

#access_hashObject



13
14
15
# File 'lib/dianping/api/token.rb', line 13

def access_hash
  @access_hash ||= load_token
end

#access_tokenObject



44
45
46
# File 'lib/dianping/api/token.rb', line 44

def access_token
  access_hash[:access_token]
end

#auth(authcode) ⇒ Object



33
34
35
# File 'lib/dianping/api/token.rb', line 33

def auth(authcode)
  save_token(client.auth_token(authcode))
end

#authorized?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/dianping/api/token.rb', line 77

def authorized?
  !access_token.nil?
end

#bidObject



56
57
58
# File 'lib/dianping/api/token.rb', line 56

def bid
  access_hash[:bid]
end

#destoryObject



81
82
83
# File 'lib/dianping/api/token.rb', line 81

def destory
  save_token({})
end

#expired?Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/dianping/api/token.rb', line 68

def expired?
  # puts access_hash, authorized?, expires_at, Time.now
  !authorized? || Time.now > expires_at
end

#expires_atObject



64
65
66
# File 'lib/dianping/api/token.rb', line 64

def expires_at
  updated_at + expires_in rescue nil
end

#expires_inObject



52
53
54
# File 'lib/dianping/api/token.rb', line 52

def expires_in
  access_hash[:expires_in]
end

#load_tokenObject



17
18
19
20
21
22
# File 'lib/dianping/api/token.rb', line 17

def load_token
  token = MultiJson.load(File.read(@token_file), symbolize_keys: true)
  token[:access_hash] || (raise 'empty token')
rescue Errno::ENOENT
  {}
end

#refreshObject

Raises:



24
25
26
27
28
29
30
31
# File 'lib/dianping/api/token.rb', line 24

def refresh
  @access_hash = load_token # try to use shared token first
  return unless expired?

  raise Error, 'no refresh_token' unless refresh_token && remain_refresh_count > 1

  save_token(client.refresh_token(@access_hash[:refresh_token]))
end

#refresh_tokenObject



48
49
50
# File 'lib/dianping/api/token.rb', line 48

def refresh_token
  access_hash[:refresh_token]
end

#remain_refresh_countObject



73
74
75
# File 'lib/dianping/api/token.rb', line 73

def remain_refresh_count
  (access_hash[:remain_refresh_count] || 12).to_i
end

#save_token(token) ⇒ Object



37
38
39
40
41
42
# File 'lib/dianping/api/token.rb', line 37

def save_token(token)
  token = { updated_at: Time.now.to_s }.merge(token)
  json = MultiJson.dump(access_hash: token )
  File.open(@token_file, 'w') { |f| f.write(json) }
  @access_hash = token
end

#updated_atObject



60
61
62
# File 'lib/dianping/api/token.rb', line 60

def updated_at
  DateTime.parse(access_hash[:updated_at]).to_time
end