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
- #bid ⇒ Object
- #destory ⇒ Object
- #expired? ⇒ Boolean
- #expires_at ⇒ Object
- #expires_in ⇒ Object
-
#initialize(client) ⇒ Token
constructor
A new instance of Token.
- #load_token ⇒ Object
- #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(client.token_root || '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 |
# File 'lib/dianping/api/token.rb', line 13 def access_hash @access_hash ||= load_token end |
#access_token ⇒ Object
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
77 78 79 |
# File 'lib/dianping/api/token.rb', line 77 def !access_token.nil? end |
#bid ⇒ Object
56 57 58 |
# File 'lib/dianping/api/token.rb', line 56 def bid access_hash[:bid] end |
#destory ⇒ Object
81 82 83 |
# File 'lib/dianping/api/token.rb', line 81 def destory save_token({}) end |
#expired? ⇒ Boolean
68 69 70 71 |
# File 'lib/dianping/api/token.rb', line 68 def expired? # puts access_hash, authorized?, expires_at, Time.now ! || Time.now > expires_at end |
#expires_at ⇒ Object
64 65 66 |
# File 'lib/dianping/api/token.rb', line 64 def expires_at updated_at + expires_in rescue nil end |
#expires_in ⇒ Object
52 53 54 |
# File 'lib/dianping/api/token.rb', line 52 def expires_in access_hash[:expires_in] end |
#load_token ⇒ Object
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 |
#refresh ⇒ Object
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_token ⇒ Object
48 49 50 |
# File 'lib/dianping/api/token.rb', line 48 def refresh_token access_hash[:refresh_token] end |
#remain_refresh_count ⇒ Object
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_at ⇒ Object
60 61 62 |
# File 'lib/dianping/api/token.rb', line 60 def updated_at DateTime.parse(access_hash[:updated_at]).to_time end |