class Dianping::Api::Token
Attributes
client[R]
Public Class Methods
new(client)
click to toggle source
# 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
Public Instance Methods
access_hash()
click to toggle source
# File lib/dianping/api/token.rb, line 13 def access_hash @access_hash ||= load_token end
access_token()
click to toggle source
# File lib/dianping/api/token.rb, line 44 def access_token access_hash[:access_token] end
auth(authcode)
click to toggle source
# File lib/dianping/api/token.rb, line 33 def auth(authcode) save_token(client.auth_token(authcode)) end
bid()
click to toggle source
# File lib/dianping/api/token.rb, line 56 def bid access_hash[:bid] end
destory()
click to toggle source
# File lib/dianping/api/token.rb, line 81 def destory save_token({}) end
expired?()
click to toggle source
# 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_at()
click to toggle source
# File lib/dianping/api/token.rb, line 64 def expires_at updated_at + expires_in rescue nil end
expires_in()
click to toggle source
# File lib/dianping/api/token.rb, line 52 def expires_in access_hash[:expires_in] end
load_token()
click to toggle source
# 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
refresh()
click to toggle source
# 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_token()
click to toggle source
# File lib/dianping/api/token.rb, line 48 def refresh_token access_hash[:refresh_token] end
remain_refresh_count()
click to toggle source
# File lib/dianping/api/token.rb, line 73 def remain_refresh_count (access_hash[:remain_refresh_count] || 12).to_i end
save_token(token)
click to toggle source
# 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_at()
click to toggle source
# File lib/dianping/api/token.rb, line 60 def updated_at DateTime.parse(access_hash[:updated_at]).to_time end