Skip to content

Commit

Permalink
Fix Response#to_hash bug lovehandle#1
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Closner committed Apr 6, 2012
1 parent 4c2a15a commit 7395a2e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 50 deletions.
2 changes: 1 addition & 1 deletion lib/agent_cooper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.configure(&block)

require 'agent_cooper/config'
require 'agent_cooper/response'
require 'agent_cooper/builder'
require 'agent_cooper/utils/nokogiri_hash'

require 'agent_cooper/request'
require 'agent_cooper/requests/finder'
Expand Down
46 changes: 0 additions & 46 deletions lib/agent_cooper/builder.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/agent_cooper/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def body

# @api public
def to_hash
AgentCooper::Builder.from_xml(xml)
xml.to_hash
end

# @api public
Expand Down
33 changes: 33 additions & 0 deletions lib/agent_cooper/utils/nokogiri_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Nokogiri::XML

class Document

def to_hash
self.root.element? ? self.root.to_hash : {self.root.name => self.root.text}
end

end

class Element

def to_hash
{ self.name => self.collect_attributes }
end

def collect_attributes
({}).tap do |hash|
self.children.each do |child|
next if child.text?
if child.element?
if child.children.length > 1
hash[child.name] = child.collect_attributes
else
hash[child.name] = child.text.strip
end
end
end
end
end

end
end
4 changes: 2 additions & 2 deletions spec/agent_cooper/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
its(:body) { should eql(body) }
its(:code) { should eql(code) }

its(:to_hash) { should eql({"foo" => "bar"}) }
its(:to_hash) { should eql({"root" => {"foo" => "bar"}}) }

its(:xml) { should be_a(Nokogiri::XML::Document) }
its(:valid?) { should be_true }
Expand All @@ -22,4 +22,4 @@
its(:valid?) { should be_false }
end

end
end

0 comments on commit 7395a2e

Please sign in to comment.