Class: Her::Middleware::SecondLevelParseJSON

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/her/middleware/second_level_parse_json.rb

Overview

This middleware expects the resource/collection data to be contained in the ‘data` key of the JSON object

Instance Method Summary collapse

Instance Method Details

#on_complete(env) ⇒ Object

This method is triggered when the response has been received. It modifies the value of ‘env`.

Parameters:

  • env (Hash)

    The response environment



23
24
25
# File 'lib/her/middleware/second_level_parse_json.rb', line 23

def on_complete(env)
  env[:body] = parse(env[:body])
end

#parse(body) ⇒ Mixed

Parse the response body

Parameters:

  • body (String)

    The response body

Returns:

  • (Mixed)

    the parsed response



10
11
12
13
14
15
16
17
# File 'lib/her/middleware/second_level_parse_json.rb', line 10

def parse(body)
  json = MultiJson.load(body, :symbolize_keys => true)
  {
    :data => json[:data],
    :errors => json[:errors],
    :metadata => json[:metadata]
  }
end