Class: Vacuum::Response::Base
- Inherits:
-
Object
- Object
- Vacuum::Response::Base
- Defined in:
- lib/vacuum/response/base.rb
Overview
An Amazon Web Services (AWS) API response.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body ⇒ Object
Gets/Sets the String response body.
-
#code ⇒ Object
Gets/Sets the Integer HTTP response code.
Instance Method Summary collapse
-
#find(query) ⇒ Object
(also: #[])
Queries the response.
-
#initialize(body, code) ⇒ Base
constructor
Initializes a new Response.
-
#to_hash ⇒ Object
Returns a Hash representation of the response.
-
#valid? ⇒ Boolean
Returns whether the HTTP response is OK.
-
#xml ⇒ Object
Returns an XML document.
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
#body ⇒ Object
Gets/Sets the String response body.
6 7 8 |
# File 'lib/vacuum/response/base.rb', line 6 def body @body end |
#code ⇒ Object
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_hash ⇒ Object
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.
47 48 49 |
# File 'lib/vacuum/response/base.rb', line 47 def valid? code == 200 end |
#xml ⇒ Object
Returns an XML document.
52 53 54 |
# File 'lib/vacuum/response/base.rb', line 52 def xml @xml ||= Nokogiri::XML.parse @body end |