Class: Dianping::Api::MiddleWare
- Inherits:
-
Object
- Object
- Dianping::Api::MiddleWare
- Defined in:
- lib/dianping/api/middle_ware.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
- #check_response(body) ⇒ Object
- #check_session(_env) ⇒ Object
-
#initialize(app, client) ⇒ MiddleWare
constructor
A new instance of MiddleWare.
Constructor Details
#initialize(app, client) ⇒ MiddleWare
Returns a new instance of MiddleWare.
6 7 8 9 |
# File 'lib/dianping/api/middle_ware.rb', line 6 def initialize(app, client) @app = app @client = client end |
Instance Method Details
#call(env) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/dianping/api/middle_ware.rb', line 11 def call(env) check_session(env) @app.call(env).on_complete do |response_env| hash = MultiJson.load response_env.body, symbolized_keys: true check_response(hash) end end |
#check_response(body) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/dianping/api/middle_ware.rb', line 27 def check_response(body) code = body[:code].to_i msg = format('[%<code>d]%<msg>s', code: code, msg: body[:msg]) raise TokenExpireError, msg if code == 608 raise UsageError, msg if code >= 800 raise Error, msg unless code == 200 end |
#check_session(_env) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/dianping/api/middle_ware.rb', line 19 def check_session(_env) raise TokenMissingError unless @client.token. return unless @client.token.expired? @client.token.refresh raise TokenExpireError end |