lib/sucker/response.rb in sucker-0.7.0 vs lib/sucker/response.rb in sucker-0.7.1

- old
+ new

@@ -1,44 +1,45 @@ module Sucker - # A wrapper around the cURL response + # A Nokogiri-driven wrapper around the cURL response class Response attr_accessor :body, :code, :time, :xml def initialize(curl) self.body = curl.body_str self.code = curl.response_code self.time = curl.total_time end - # Hashifies XML document. Optionally, parses for a node name and returns a collection - # of the hashified nodes. - def to_h(path=nil) - if path - xml.xpath("//xmlns:#{path}").map { |node| content_to_string(node.to_hash[path]) } - else - content_to_string(xml.to_hash) - end + # Queries an xpath and returns a collection of hashified matches + def node(path) + xml.xpath("//xmlns:#{path}").map { |node| strip_content(node.to_hash[path]) } end - alias :to_hash :to_h + # Hashifies XML document or node + def to_hash + strip_content(xml.to_hash) + end + alias :to_h :to_hash + + # The XML document def xml @xml ||= Nokogiri::XML(body) end private - def content_to_string(node) + def strip_content(node) case node when Array - node.map { |el| content_to_string(el) } + node.map { |el| strip_content(el) } when Hash if node.keys.size == 1 && node["__content__"] node["__content__"] else - node.inject({}) do |el, key_value| - el.merge({ key_value.first => content_to_string(key_value.last) }) + node.inject({}) do |coll, key_value| + coll.merge({ key_value.first => strip_content(key_value.last) }) end end else node end