Class: Dianping::Api::Token
- Inherits:
-
Object
- Object
- Dianping::Api::Token
- Defined in:
- lib/dianping/api/token.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #access_hash ⇒ Object
- #access_token ⇒ Object
- #auth(authcode) ⇒ Object
- #authorized? ⇒ Boolean
- #destory ⇒ Object
- #expired? ⇒ Boolean
- #expires_at ⇒ Object
- #expires_in ⇒ Object
-
#initialize(client) ⇒ Token
constructor
A new instance of Token.
- #refresh ⇒ Object
- #refresh_token ⇒ Object
- #remain_refresh_count ⇒ Object
- #save_token(token) ⇒ Object
- #updated_at ⇒ Object
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('/tmp', "dianping-api-#{client.app_key}") end |
Instance Attribute Details
#client ⇒ Object (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_hash ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/dianping/api/token.rb', line 13 def access_hash @access_hash ||= begin token = MultiJson.load(File.read(@token_file), symbolize_keys: true) token[:access_hash] || (raise 'empty token') rescue Errno::ENOENT {} end end |
#access_token ⇒ Object
40 41 42 |
# File 'lib/dianping/api/token.rb', line 40 def access_token access_hash[:access_token] end |
#auth(authcode) ⇒ Object
29 30 31 |
# File 'lib/dianping/api/token.rb', line 29 def auth(authcode) save_token(client.auth_token(authcode)) end |
#authorized? ⇒ Boolean
69 70 71 |
# File 'lib/dianping/api/token.rb', line 69 def !access_token.nil? end |
#destory ⇒ Object
73 74 75 |
# File 'lib/dianping/api/token.rb', line 73 def destory save_token({}) end |
#expired? ⇒ Boolean
60 61 62 63 |
# File 'lib/dianping/api/token.rb', line 60 def expired? # puts access_hash, authorized?, expires_at, Time.now ! || Time.now > expires_at end |
#expires_at ⇒ Object
56 57 58 |
# File 'lib/dianping/api/token.rb', line 56 def expires_at updated_at + expires_in rescue nil end |
#expires_in ⇒ Object
48 49 50 |
# File 'lib/dianping/api/token.rb', line 48 def expires_in access_hash[:expires_in] end |
#refresh ⇒ Object
23 24 25 26 27 |
# File 'lib/dianping/api/token.rb', line 23 def refresh raise Error, 'no refresh_token' unless refresh_token && remain_refresh_count > 1 save_token(client.refresh_token(@access_hash[:refresh_token])) end |
#refresh_token ⇒ Object
44 45 46 |
# File 'lib/dianping/api/token.rb', line 44 def refresh_token access_hash[:refresh_token] end |
#remain_refresh_count ⇒ Object
65 66 67 |
# File 'lib/dianping/api/token.rb', line 65 def remain_refresh_count (access_hash[:remain_refresh_count] || 12).to_i end |
#save_token(token) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/dianping/api/token.rb', line 33 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 ⇒ Object
52 53 54 |
# File 'lib/dianping/api/token.rb', line 52 def updated_at Time.parse(access_hash[:updated_at]) end |