Skip to content

Commit

Permalink
replaced Savon.response_pattern wth a new impl of Savon::SOAP::Respon…
Browse files Browse the repository at this point in the history
…se#to_array
  • Loading branch information
rubiii committed Nov 26, 2010
1 parent 1d6eda2 commit 6df6a67
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 97 deletions.
9 changes: 0 additions & 9 deletions lib/savon/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,6 @@ def strip_namespaces?
# Sets whether to strip namespaces in a SOAP response Hash.
attr_writer :strip_namespaces

# Returns the response pattern to apply.
def response_pattern
@response_pattern ||= []
end

# Sets the response pattern (an Array of Regexps or Symbols).
attr_writer :response_pattern

# Reset to default configuration.
def reset_config!
self.log = true
Expand All @@ -77,7 +69,6 @@ def reset_config!
self.raise_errors = true
self.soap_version = SOAP::DefaultVersion
self.strip_namespaces = true
self.response_pattern = []
end

end
Expand Down
36 changes: 10 additions & 26 deletions lib/savon/soap/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,20 @@ def http_error
end

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

# Returns the SOAP response body as a Hash and applies
# the <tt>Savon.response_pattern</tt> if defined.
def to_hash
@hash ||= apply_response_pattern original_hash
@hash ||= Savon::SOAP::XML.to_hash to_xml
end

# Returns the SOAP response Hash as an Array.
def to_array
@array ||= begin
array = to_hash.kind_of?(Array) ? to_hash : [to_hash]
array.compact
# 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.
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
end

# Returns the SOAP response XML.
Expand All @@ -74,20 +72,6 @@ def raise_errors
raise http_error if http_error?
end

def apply_response_pattern(hash)
return hash if Savon.response_pattern.blank?

Savon.response_pattern.inject(hash) do |memo, pattern|
key = case pattern
when Regexp then memo.keys.find { |key| key.to_s =~ pattern }
else memo.keys.find { |key| key.to_s == pattern.to_s }
end

return hash unless key
memo[key]
end
end

end
end
end
13 changes: 0 additions & 13 deletions spec/savon/savon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,6 @@
Savon.strip_namespaces?.should == false
end
end

describe "response_pattern" do
it "should default to an empty Array" do
Savon.response_pattern.should == []
end

it "should return the response pattern to apply" do
pattern = [/.+_response/, :return]
Savon.configure { |config| config.response_pattern = pattern }

Savon.response_pattern.should == pattern
end
end
end

end
61 changes: 12 additions & 49 deletions spec/savon/soap/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,72 +116,35 @@
end
end

describe "#original_hash" do
describe "#to_hash" do
it "should return the SOAP response body as a Hash" do
soap_response.original_hash[:authenticate_response][:return].should ==
soap_response.to_hash[:authenticate_response][:return].should ==
ResponseFixture.authentication(:to_hash)
end
end

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

it "should memoize the result" do
response.to_hash.should equal(response.to_hash)
end

context "without response pattern" do
it "should return the original Hash" do
response.to_hash[:authenticate_response].should be_a(Hash)
context "when the given path exists" do
it "should return an Array containing the path value" do
response.to_array(:authenticate_response, :return).should == [ResponseFixture.authentication(:to_hash)]
end
end

context "with response pattern" do
it "should apply the response pattern" do
Savon.response_pattern = [/.+_response/, :return]
response.to_hash[:success].should be_true

Savon.response_pattern = nil # reset to default
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 "with unmatched response pattern" do
it "should return the original Hash" do
Savon.response_pattern = [:unmatched, :pattern]
response.to_hash.should == response.original_hash

Savon.response_pattern = nil # reset to default
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
end
end

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

around do |example|
Savon.response_pattern = [/.+_response/, :return]
example.run
Savon.response_pattern = nil # reset to default
end

it "should memoize the result" do
response.to_array.should equal(response.to_array)
end

it "should return an Array for a single response element" do
response.to_array.should be_an(Array)
response.to_array.first[:success].should be_true
end

it "should return an Array for multiple response element" do
Savon.response_pattern = [/.+_response/, :history, :case]
list_response = soap_response :body => ResponseFixture.list

list_response.to_array.should be_an(Array)
list_response.to_array.should have(2).items
end
end

describe "#to_xml" do
it "should return the raw SOAP response body" do
soap_response.to_xml.should == ResponseFixture.authentication
Expand Down

0 comments on commit 6df6a67

Please sign in to comment.