class Her::Middleware::MnoeApiV1ParseJson

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

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 @private

# File lib/her_extension/middleware/mnoe_api_v1_parse_json.rb, line 44
def on_complete(env)
  env[:body] = case env[:status]
  when 204
    parse('{}')
  else
    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 @private

# File lib/her_extension/middleware/mnoe_api_v1_parse_json.rb, line 11
def parse(body)
  json = parse_json(body)
  parse_types({
    :data => json[:data] || {},
    :errors => json[:errors] || {},
    :metadata => json[:metadata] || {}
  })
end
parse_types(res) click to toggle source
# File lib/her_extension/middleware/mnoe_api_v1_parse_json.rb, line 20
def parse_types(res)
  case
  when res.kind_of?(Array)
    return res.map { |e| parse_types(e) }
  when res.kind_of?(Hash) && res[:cents] && res[:currency]
    Money.new(res[:cents],res[:currency])
  when res.kind_of?(Hash)
    hash = res.dup
    hash.each do |k,v|
      hash[k] = parse_types(v)
    end
    return hash
  when res.is_a?(String) && res =~ /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/i
    return Time.iso8601(res)
  else
    return res
  end
end