Skip to content

Commit

Permalink
moved Savon::SOAP::Response#to_array to a class method at Savon::SOAP…
Browse files Browse the repository at this point in the history
…::XML.to_array
  • Loading branch information
rubiii committed Jan 8, 2011
1 parent 6a4ccd1 commit 05a7d30
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 29 deletions.
13 changes: 3 additions & 10 deletions lib/savon/soap/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,12 @@ def http_error

# Returns the SOAP response body as a Hash.
def to_hash
@hash ||= Savon::SOAP::XML.to_hash to_xml
@hash ||= Savon::SOAP::XML.to_hash http.body
end

# Traverses the SOAP response Hash for a given +path+ of Hash keys
# and returns the value as an Array. Defaults to return an empty Array
# in case the path does not exist or returns nil.
# Returns the SOAP response body as an Array.
def to_array(*path)
value = path.inject to_hash do |memo, key|
return [] unless memo[key]
memo[key]
end

value.kind_of?(Array) ? value.compact : [value].compact
Savon::SOAP::XML.to_array to_hash, *path
end

# Returns the SOAP response XML.
Expand Down
15 changes: 15 additions & 0 deletions lib/savon/soap/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,25 @@ class XML
"xmlns:xsi" => "http://www.w3.org/2001/XMLSchema-instance"
}

# Converts the given SOAP response +xml+ into a Hash.
def self.to_hash(xml)
(Crack::XML.parse(xml) rescue {}).find_soap_body
end

# Expects a SOAP response XML or Hash, traverses it for a given +path+ of Hash keys
# and returns the value as an Array. Defaults to return an empty Array in case the
# path does not exist or returns nil.
def self.to_array(object, *path)
hash = object.kind_of?(Hash) ? object : to_hash(object)

result = path.inject hash do |memo, key|
return [] unless memo[key]
memo[key]
end

result.kind_of?(Array) ? result.compact : [result].compact
end

# Accepts an +endpoint+, an +input+ tag and a SOAP +body+.
def initialize(endpoint = nil, input = nil, body = nil)
self.endpoint = endpoint if endpoint
Expand Down
22 changes: 3 additions & 19 deletions spec/savon/soap/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,9 @@
end

describe "#to_array" do
let(:response) { soap_response }

context "when the given path exists" do
it "should return an Array containing the path value" do
response.to_array(:authenticate_response, :return).should ==
[Fixture.response_hash(:authentication)[:authenticate_response][:return]]
end
end

context "when the given path returns nil" do
it "should return an empty Array" do
response.to_array(:authenticate_response, :undefined).should == []
end
end

context "when the given path does not exist at all" do
it "should return an empty Array" do
response.to_array(:authenticate_response, :some, :wrong, :path).should == []
end
it "should delegate to Savon::SOAP::XML.to_array" do
Savon::SOAP::XML.expects(:to_array).with(soap_response.to_hash, :authenticate_response, :return)
soap_response.to_array :authenticate_response, :return
end
end

Expand Down
23 changes: 23 additions & 0 deletions spec/savon/soap/xml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@
end
end

describe ".to_array" do
let(:response_hash) { Fixture.response_hash :authentication }

context "when the given path exists" do
it "should return an Array containing the path value" do
Savon::SOAP::XML.to_array(response_hash, :authenticate_response, :return).should ==
[response_hash[:authenticate_response][:return]]
end
end

context "when the given path returns nil" do
it "should return an empty Array" do
Savon::SOAP::XML.to_array(response_hash, :authenticate_response, :undefined).should == []
end
end

context "when the given path does not exist at all" do
it "should return an empty Array" do
Savon::SOAP::XML.to_array(response_hash, :authenticate_response, :some, :wrong, :path).should == []
end
end
end

describe ".new" do
it "should accept an endpoint, an input tag and a SOAP body" do
xml = Savon::SOAP::XML.new Endpoint.soap, :authentication, :id => 1
Expand Down

0 comments on commit 05a7d30

Please sign in to comment.