Class: Sucker::Response
- Inherits:
-
Object
- Object
- Sucker::Response
- Defined in:
- lib/sucker/response.rb,
lib/sucker/synchrony.rb
Overview
A wrapper around the API response.
Instance Attribute Summary collapse
-
#body ⇒ Object
The response body.
-
#code ⇒ Object
The HTTP status code of the response.
Instance Method Summary collapse
-
#each(path, &block) ⇒ Object
A shorthand that queries for a specified attribute and yields to a given block each matching document.
-
#errors ⇒ Object
An array of errors in the response.
-
#find(attribute) ⇒ Object
(also: #[])
Queries for a specified attribute and returns an array of matching documents.
-
#has_errors? ⇒ Boolean
Returns true if the response contains errors.
-
#initialize(http) ⇒ Response
constructor
A new instance of Response.
-
#map(path, &block) ⇒ Object
A shorthand that queries for a specifed attribute, yields to a given block matching documents, and collects final values.
-
#to_hash ⇒ Object
Parses the response into a simple hash.
-
#valid? ⇒ Boolean
Checks if the HTTP response is OK.
-
#xml ⇒ Object
The XML document.
Constructor Details
#initialize(http) ⇒ Response
Returns a new instance of Response.
14 15 16 17 |
# File 'lib/sucker/response.rb', line 14 def initialize(response) self.body = response.body self.code = response.code end |
Instance Attribute Details
#body ⇒ Object
The response body.
9 10 11 |
# File 'lib/sucker/response.rb', line 9 def body @body end |
#code ⇒ Object
The HTTP status code of the response.
12 13 14 |
# File 'lib/sucker/response.rb', line 12 def code @code end |
Instance Method Details
#each(path, &block) ⇒ Object
A shorthand that queries for a specified attribute and yields to a given block each matching document.
response.each('Item') { |item| puts item }
24 25 26 |
# File 'lib/sucker/response.rb', line 24 def each(path, &block) find(path).each { |match| block.call(match) } end |
#errors ⇒ Object
An array of errors in the response.
29 30 31 |
# File 'lib/sucker/response.rb', line 29 def errors find('Error') end |
#find(attribute) ⇒ Object Also known as: []
Queries for a specified attribute and returns an array of matching documents.
items = response.find('Item')
38 39 40 41 42 |
# File 'lib/sucker/response.rb', line 38 def find(attribute) xml.xpath("//xmlns:#{attribute}").map do |element| HashBuilder.from_xml(element) end end |
#has_errors? ⇒ Boolean
Returns true if the response contains errors.
46 47 48 |
# File 'lib/sucker/response.rb', line 46 def has_errors? errors.count > 0 end |
#map(path, &block) ⇒ Object
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 }
55 56 57 |
# File 'lib/sucker/response.rb', line 55 def map(path, &block) find(path).map { |match| block.call(match) } end |
#to_hash ⇒ Object
Parses the response into a simple hash.
60 61 62 |
# File 'lib/sucker/response.rb', line 60 def to_hash HashBuilder.from_xml(xml) end |
#valid? ⇒ Boolean
Checks if the HTTP response is OK.
response = request.get
response.valid?
=> true
70 71 72 |
# File 'lib/sucker/response.rb', line 70 def valid? code == 200 end |
#xml ⇒ Object
The XML document.
response = request.get
response.xml
79 80 81 |
# File 'lib/sucker/response.rb', line 79 def xml @xml ||= Nokogiri::XML(body) end |