class Her::Middleware::ParseJSON

Public Instance Methods

assert_response_ok(status, message) click to toggle source
# File lib/her/middleware/parse_json.rb, line 20
def assert_response_ok(status, message)
  if exception_class = Her::Errors.exception_class_for_status(status)
    raise exception_class, message
  end
end
parse_json(body = nil) click to toggle source

@private

# File lib/her/middleware/parse_json.rb, line 5
def parse_json(body = nil)
  body = '{}' if body.blank?
  message = "Response from the API must behave like a Hash or an Array (last JSON response was #{body.inspect})"

  json = begin
    MultiJson.load(body, :symbolize_keys => true)
  rescue MultiJson::LoadError
    raise Her::Errors::ParseError, message
  end

  raise Her::Errors::ParseError, message unless json.is_a?(Hash) or json.is_a?(Array)

  json
end