module MWS
  module API

    class Response < Hashie::Rash
      
      def self.parse(body, name, params)
        return body unless body.is_a?(Hash)

        rash = self.new(body)

        return rash if name == :create_inbound_shipment_plan && rash.error_response.present?
        return rash if name == :update_inbound_shipment && rash.error_response.present?

        handle_error_response(rash["error_response"]["error"]) unless rash["error_response"].nil?
        raise BadResponseError, "received non-matching response type #{rash.keys}" if rash["#{name}_response"].nil?
        rash = rash["#{name}_response"]

        if rash = rash["#{name}_result"]
          # only runs mods if correct result is present
          params[:mods].each {|mod| mod.call(rash) } if params[:mods]
          rash
        end
      end

      def self.handle_error_response(error)
        msg = "#{error.code}: #{error.message}"
        msg << " -- #{error.detail}" unless error.detail.nil?
        raise ErrorResponse, msg
      end

    end

  end
end