Class: Her::Middleware::FirstLevelParseJSON
- Inherits:
-
Faraday::Response::Middleware
- Object
- Faraday::Response::Middleware
- Her::Middleware::FirstLevelParseJSON
- Defined in:
- lib/her/middleware/first_level_parse_json.rb
Overview
This middleware treat the received first-level JSON structure as the resource data.
Instance Method Summary collapse
-
#on_complete(env) ⇒ Object
This method is triggered when the response has been received.
-
#parse(body) ⇒ Mixed
Parse the response body.
Instance Method Details
#on_complete(env) ⇒ Object
This method is triggered when the response has been received. It modifies the value of ‘env`.
24 25 26 27 28 29 30 31 |
# File 'lib/her/middleware/first_level_parse_json.rb', line 24 def on_complete(env) case env[:status] when 204 env[:body] = parse('{}') else env[:body] = parse(env[:body]) end end |
#parse(body) ⇒ Mixed
Parse the response body
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/her/middleware/first_level_parse_json.rb', line 9 def parse(body) json = MultiJson.load(body, :symbolize_keys => true) errors = json.delete(:errors) || {} = json.delete(:metadata) || [] { :data => json, :errors => errors, :metadata => } end |