class Her::Middleware::FirstLevelParseJSON

This middleware treat the received first-level JSON structure as the resource data.

Public Instance Methods

on_complete(env) click to toggle source

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

@param [Hash] env The response environment

# 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) click to toggle source

Parse the response body

@param [String] body The response body @return [Mixed] the parsed response

# 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) || {}
  metadata = json.delete(:metadata) || []
  {
    :data => json,
    :errors => errors,
    :metadata => metadata
  }
end