Skip to content

Commit

Permalink
use the builder instead of a block's return value
Browse files Browse the repository at this point in the history
currently, when passing a block to both soap#body and soap#xml to use builder to
create the xml, savon uses the block's return value instead of the builder's state.

this fixes #322.
  • Loading branch information
rubiii committed Sep 18, 2012
1 parent c36be54 commit 0179e5a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## UPCOMING

* Fix: [#322](https://github.com/savonrb/savon/issues/322) use the builder's state instead of the
block's return value to set the soap body/xml values.

## 1.2.0 (2012-09-15)

* Fix: [#312](https://github.com/savonrb/savon/pull/312) recursively determines the proper namespaces
Expand Down
13 changes: 11 additions & 2 deletions lib/savon/soap/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,12 @@ def encoding
# Accepts a +block+ and yields a <tt>Builder::XmlMarkup</tt> object to let you create
# custom body XML.
def body
@body = yield builder(nil) if block_given?
if block_given?
xml = builder(nil)
yield xml
@body = xml.target!
end

@body
end

Expand All @@ -147,7 +152,11 @@ def body
# Accepts a +block+ and yields a <tt>Builder::XmlMarkup</tt> object to let you create
# a completely custom XML.
def xml(directive_tag = :xml, attrs = {})
@xml = yield builder(directive_tag, attrs) if block_given?
return unless block_given?

xml = builder(directive_tag, attrs)
yield xml
@xml = xml.target!
end

# Accepts an XML String and lets you specify a completely custom request body.
Expand Down
56 changes: 40 additions & 16 deletions spec/savon/soap/xml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,24 @@ def xml(endpoint = nil, input = nil, body = nil)
xml.to_xml.should include("<id>1</id>")
end

it "should accept a block" do
xml.body do |body|
body.user { body.id 1 }
context "with a block" do
it "should yield a Builder::XmlMarkup object" do
xml.body do |body|
body.user { body.id 1 }
end

xml.to_xml.should include("<authenticate><user><id>1</id></user></authenticate>")
end

xml.to_xml.should include("<authenticate><user><id>1</id></user></authenticate>")
# https://github.com/savonrb/savon/issues/322
it "should use the builder (and not the block's return value) to generate the body" do
xml.body do |body|
body.user { body.id 1 }
body.whatever if false
end

xml.to_xml.should include("<authenticate><user><id>1</id></user></authenticate>")
end
end
end

Expand All @@ -135,22 +147,34 @@ def xml(endpoint = nil, input = nil, body = nil)
xml.to_xml.should == "<custom>xml</custom>"
end

it "yields a Builder::XmlMarkup object to a given block" do
xml.xml { |xml| xml.using("Builder") }
xml.to_xml.should == '<?xml version="1.0" encoding="UTF-8"?><using>Builder</using>'
end
context "with a block" do
it "yields a Builder::XmlMarkup object" do
xml.xml { |xml| xml.using("Builder") }
xml.to_xml.should == '<?xml version="1.0" encoding="UTF-8"?><using>Builder</using>'
end

it "accepts options to pass to the Builder::XmlMarkup instruct!" do
xml.xml :xml, :aaa => :bbb do |xml|
xml.using("Builder")
it "accepts options to pass to the Builder::XmlMarkup instruct!" do
xml.xml :xml, :aaa => :bbb do |xml|
xml.using("Builder")
end

xml.to_xml.should == '<?xml version="1.0" encoding="UTF-8" aaa="bbb"?><using>Builder</using>'
end

xml.to_xml.should == '<?xml version="1.0" encoding="UTF-8" aaa="bbb"?><using>Builder</using>'
end
it "allows to change the encoding" do
xml.xml(:xml, :encoding => "US-ASCII") { |xml| xml.using("Builder") }
xml.to_xml.should == '<?xml version="1.0" encoding="US-ASCII"?><using>Builder</using>'
end

it "allows to change the encoding" do
xml.xml(:xml, :encoding => "US-ASCII") { |xml| xml.using("Builder") }
xml.to_xml.should == '<?xml version="1.0" encoding="US-ASCII"?><using>Builder</using>'
# https://github.com/savonrb/savon/issues/322
it "uses the builder (and not the block's return value) to generate the xml" do
xml.xml do |xml|
xml.using("Builder")
xml.whatever if false
end

xml.to_xml.should == '<?xml version="1.0" encoding="UTF-8"?><using>Builder</using>'
end
end
end

Expand Down

0 comments on commit 0179e5a

Please sign in to comment.