Skip to content

Commit

Permalink
Support for arrays of complex structures
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris Staal committed Feb 2, 2012
1 parent 54feec4 commit 96a21e5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 16 deletions.
18 changes: 12 additions & 6 deletions lib/wash_out/param.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ def load(data)
data = Array(data) if @multiplied

if struct?
map_struct(data) { |param, elem| param.load(elem) }
if @multiplied
data.map do |x|
map_struct(x) { |param, elem| param.load(elem) }
end
else
map_struct(data) { |param, elem| param.load(elem) }
end
else
operation = case type
when 'string'; :to_s
Expand All @@ -40,10 +46,10 @@ def load(data)
when 'boolean'; nil # Nori handles that for us
else raise RuntimeError, "Invalid WashOut simple type: #{type}"
end

if operation.nil?
data
elsif data.is_a? Array
elsif @multiplied
data.map{|x| x.send(operation)}
else
data.send(operation)
Expand Down Expand Up @@ -80,7 +86,7 @@ def namespaced_type
def self.parse_def(definition)
raise RuntimeError, "[] should not be used in your params. Use nil if you want to mark empty set." if definition == []
return [] if definition == nil

if [Array, Symbol].include?(definition.class)
definition = { :value => definition }
end
Expand All @@ -99,7 +105,7 @@ def self.parse_def(definition)
raise RuntimeError, "Wrong definition: #{type.inspect}"
end
end

def clone
copy = self.class.new(@name, @type.to_sym, @multiplied)
copy.map = @map.map{|x| x.clone}
Expand All @@ -124,7 +130,7 @@ def map_struct(data)
# Raise an appropriate exception if a required datum is missing.
def check_if_missing(data)
if data.nil?
raise WashOut::Dispatcher::SOAPError, "Required SOAP parameter #{@name} is missing"
raise WashOut::Dispatcher::SOAPError, "Required SOAP parameter '#{@name}' is missing"
end
end
end
Expand Down
51 changes: 41 additions & 10 deletions spec/wash_out_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_area
:distance_from_o => Math.sqrt(circle[:center][:x] ** 2 + circle[:center][:y] ** 2) }
end
end

client = savon_instance
xml = Nori.parse client.wsdl.xml

Expand Down Expand Up @@ -124,7 +124,7 @@ def get_area

it "should allow arbitrary action names" do
name = 'AnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything'

mock_controller do
soap_action name,
:args => nil, :return => :integer, :to => :answer
Expand Down Expand Up @@ -182,7 +182,7 @@ def error
client.request(:error)
}.should raise_exception(Savon::SOAP::Fault)
end

it "should handle nested returns" do
mock_controller do
soap_action "gogogo",
Expand All @@ -204,10 +204,10 @@ def gogogo
}
end
end

savon_instance.request(:gogogo)[:gogogo_response].should == {:zoo=>"zoo", :boo=>{:moo=>"moo", :doo=>"doo", :"@xsi:type"=>"tns:boo"}}
end

it "should handle arrays" do
mock_controller do
soap_action "rumba",
Expand All @@ -220,14 +220,45 @@ def rumba
render :soap => nil
end
end

savon_instance.request(:rumba) do
soap.body = {
:rumbas => [1, 2, 3]
}
end
end


it "should handle complex structures inside arrays" do
mock_controller do
soap_action "rumba",
:args => {
:rumbas => [ {
:zombies => :string,
:puppies => :string
} ]
},
:return => nil
def rumba
params.should == {
"rumbas" => [
{"zombies" => 'suck', "puppies" => 'rock'},
{"zombies" => 'slow', "puppies" => 'fast'}
]
}
render :soap => nil
end
end

savon_instance.request(:rumba) do
soap.body = {
:rumbas => [
{:zombies => 'suck', :puppies => 'rock'},
{:zombies => 'slow', :puppies => 'fast'}
]
}
end
end

it "should be able to return arrays" do
mock_controller do
soap_action "rumba",
Expand All @@ -237,10 +268,10 @@ def rumba
render :soap => [1, 2, 3]
end
end

savon_instance.request(:rumba).to_hash[:rumba_response].should == {:value => ["1", "2", "3"]}
end

it "should deprecate old syntax" do
# save rspec context check
raise_runtime_exception = raise_exception(RuntimeError)
Expand All @@ -256,5 +287,5 @@ def rumba
end
end
end

end

0 comments on commit 96a21e5

Please sign in to comment.