lib/her/middleware/default_parse_json.rb in her-0.1.7 vs lib/her/middleware/default_parse_json.rb in her-0.1.8
- old
+ new
@@ -1,18 +1,28 @@
module Her
module Middleware
+ # This is the default middleware used to parse JSON responses. It returns
+ # a Hash with three elements: `data`, `errors` and `metadata`.
class DefaultParseJSON < Faraday::Response::Middleware
- def parse(body)
- json = JSON.parse(body, :symbolize_names => true)
- return {
+ # Parse the response body
+ #
+ # @param [String] body The response body
+ # @return [Mixed] the parsed response
+ def parse(body) # {{{
+ json = MultiJson.load(body, :symbolize_keys => true)
+ {
:data => json[:data],
:errors => json[:errors],
- :metadata => json[:metadata],
+ :metadata => json[:metadata]
}
- end
+ end # }}}
- def on_complete(env)
- env[:body] = parse env[:body]
- end
+ # This method is triggered when the response has been received. It modifies
+ # the value of `env[:body]`.
+ #
+ # @param [Hash] env The response environment
+ def on_complete(env) # {{{
+ env[:body] = parse(env[:body])
+ end # }}}
end
end
end