Skip to content

Commit

Permalink
added Savon.response_pattern (from savon_model)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiii committed Nov 5, 2010
1 parent f54de43 commit 0a12fb3
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
9 changes: 9 additions & 0 deletions lib/savon/global.rb
Expand Up @@ -61,6 +61,14 @@ 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 @@ -69,6 +77,7 @@ def reset_config!
self.raise_errors = true
self.soap_version = SOAP::DefaultVersion
self.strip_namespaces = true
self.response_pattern = []
end

end
Expand Down
30 changes: 29 additions & 1 deletion lib/savon/soap/response.rb
Expand Up @@ -44,8 +44,22 @@ 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 ||= Savon::SOAP::XML.to_hash to_xml
@hash ||= apply_response_pattern original_hash
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
end
end

# Returns the SOAP response XML.
Expand All @@ -60,6 +74,20 @@ 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: 13 additions & 0 deletions spec/savon/savon_spec.rb
Expand Up @@ -81,5 +81,18 @@
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
63 changes: 61 additions & 2 deletions spec/savon/soap/response_spec.rb
Expand Up @@ -116,13 +116,72 @@
end
end

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

describe "#to_hash" 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)
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
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
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 0a12fb3

Please sign in to comment.