Sha256: df39fafa6c63fb3d1c4a91bfa5672ee3065139b6c8c95f5674d7499d0d540a44

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

module Vacuum
  module Response
    # An Amazon Web Services (AWS) API response.
    class Base
      # Gets/Sets the String response body.
      attr_accessor :body

      # Gets/Sets the Integer HTTP response code.
      attr_accessor :code

      # Initializes a new Response.
      #
      # body - The String response body.
      # code - An HTTP response code that responds to to_i.
      def initialize(body, code)
        @body, @code = body, code.to_i
      end

      # Queries the response.
      #
      # query - String attribute to be queried.
      #
      # Yields matching nodes to a given block if one is given.
      #
      # Returns an Array of matching nodes or the return values of the yielded
      # block if latter was given.
      def find(query)
        path = if xml.namespaces.empty?
                 "//#{query}"
               else
                 "//xmlns:#{query}"
               end

        xml.xpath(path).map do |node|
          hsh = Utils.xml_to_hash node
          block_given? ? yield(hsh) : hsh
        end
      end
      alias [] find

      # Returns a Hash representation of the response.
      def to_hash
        Utils.xml_to_hash xml
      end

      # Returns whether the HTTP response is OK.
      def valid?
        code == 200
      end

      # Returns an XML document.
      def xml
        @xml ||= Nokogiri::XML.parse @body
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
vacuum-0.2.2 lib/vacuum/response/base.rb
vacuum-0.2.1 lib/vacuum/response/base.rb
vacuum-0.2.0 lib/vacuum/response/base.rb
vacuum-0.2.0.pre.1 lib/vacuum/response/base.rb
vacuum-0.2.0.pre lib/vacuum/response/base.rb