Sha256: 3f738f479fcc167aaaeac98a07772724e7ebcf12d3ba022fc16653cb44cf90f1

Contents?: true

Size: 858 Bytes

Versions: 1

Compression:

Stored size: 858 Bytes

Contents

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)
        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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ruby-mws-0.1 lib/ruby-mws/api/response.rb