lib/sucker/response.rb in sucker-1.0.0.beta.2 vs lib/sucker/response.rb in sucker-1.0.0.beta.3
- old
+ new
@@ -16,21 +16,28 @@
self.body = curl.body_str
self.code = curl.response_code
self.time = curl.total_time
end
- # Queries an xpath and returns result as an array of hashes
+ # Queries an xpath and returns an array of matching nodes
#
- # For instance, to get all items in an ItemLookup query:
+ # Will yield each match if a block is given
#
# response = worker.get
- # response.node("Item").each { |item| ... }
+ # response.find("Item") { |item| ... }
#
- def node(path)
- xml.xpath("//xmlns:#{path}").map { |node| strip_content(node.to_hash[path]) }
+ def find(path)
+ node = xml.xpath("//xmlns:#{path}").map { |node| strip_content(node.to_hash[path]) }
+ node.each { |e| yield e } if block_given?
+ node
end
+ def node(path) # :nodoc:
+ warn "[DEPRECATION] `node` is deprecated. Use `find` instead."
+ find(path)
+ end
+
# Parses the response into a simple hash
#
# response = worker.get
# response.to_hash
#
@@ -56,10 +63,10 @@
@xml ||= Nokogiri::XML(body)
end
private
- # Let's massage the somewhat-verbose XML Mini hash into better shape
+ # Let's massage that hash
def strip_content(node)
case node
when Array
node.map { |child| strip_content(child) }
when Hash