Class: Vacuum::Response::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vacuum/response/base.rb

Overview

An Amazon Web Services (AWS) API response.

Direct Known Subclasses

MWS, ProductAdvertising

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, code) ⇒ Base

Initializes a new Response.

body - The String response body. code - An HTTP response code that responds to to_i.



15
16
17
# File 'lib/vacuum/response/base.rb', line 15

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

Instance Attribute Details

#bodyObject

Gets/Sets the String response body.



6
7
8
# File 'lib/vacuum/response/base.rb', line 6

def body
  @body
end

#codeObject

Gets/Sets the Integer HTTP response code.



9
10
11
# File 'lib/vacuum/response/base.rb', line 9

def code
  @code
end

Instance Method Details

#find(query) ⇒ Object Also known as: []

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.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/vacuum/response/base.rb', line 27

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

#to_hashObject

Returns a Hash representation of the response.



42
43
44
# File 'lib/vacuum/response/base.rb', line 42

def to_hash
  Utils.xml_to_hash xml
end

#valid?Boolean

Returns whether the HTTP response is OK.

Returns:

  • (Boolean)


47
48
49
# File 'lib/vacuum/response/base.rb', line 47

def valid?
  code == 200
end

#xmlObject

Returns an XML document.



52
53
54
# File 'lib/vacuum/response/base.rb', line 52

def xml
  @xml ||= Nokogiri::XML.parse @body
end