Class: Her::Middleware::FirstLevelParseJSON

Inherits:
Faraday::Response::Middleware
  • Object
show all
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

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



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

Parameters:

  • body (String)

    The response body

Returns:

  • (Mixed)

    the parsed response



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