Sha256: 948f73d7cd319be47b68f46891ab53431fa9c277f34a30f8c7ddfa16efe072c3

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

module AmazonProduct
  # A wrapper around the API response.
  class Response

    # The response body.
    attr_accessor :body

    # The HTTP status code of the response.
    attr_accessor :code

    def initialize(body, code)
      @body = body
      @code = code.to_i
    end

    # A shorthand that queries for a specified attribute and yields to a given
    # block each matching document.
    #
    #   response.each('Item') { |item| puts item }
    #
    def each(path, &block)
      find(path).each { |match| block.call(match) }
    end

    # An array of errors in the response.
    def errors
      find('Error')
    end

    # Queries for a specified attribute and returns an array of matching
    # documents.
    #
    #   items = response.find('Item')
    #
    def find(attribute)
      xml.xpath("//xmlns:#{attribute}").map do |element|
        HashBuilder.from_xml(element)
      end
    end
    alias [] find

    # Returns true if the response contains errors.
    def has_errors?
      errors.count > 0
    end

    # A shorthand that queries for a specifed attribute, yields to a given
    # block matching documents, and collects final values.
    #
    #   items = response.map('Item') { |item| # do something }
    #
    def map(path, &block)
      find(path).map { |match| block.call(match) }
    end

    # Parses the response into a simple hash.
    def to_hash
      HashBuilder.from_xml(xml)
    end

    # Checks if the HTTP response is OK.
    def valid?
      code == 200
    end

    # The XML document.
    def xml
      @xml ||= Nokogiri::XML(@body)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
amazon_product-3.0.0.pre.2 lib/amazon_product/response.rb