Skip to content

Instantly share code, notes, and snippets.

@georgkreimer
Created February 14, 2011 14:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgkreimer/825959 to your computer and use it in GitHub Desktop.
Save georgkreimer/825959 to your computer and use it in GitHub Desktop.
# extensions
# …
module SOAPArray
def to_soap_array(item_name = "messageId")
result=Array.new
self.each do |val|
result.push({item_name => val.to_i})
end
result
end
end
Array.send :include, SOAPArray
# …
# client
class Client
# …
def get_messages
do_request("GetMessagesRequest").message.presence || Hashie::Mash.new
end
def received_messages(ids)
do_request "ReceivedMessagesRequest", ids.to_soap_array
end
private
def do_request(method, body = nil)
response_hash = @client.request(:wsdl, method) do
soap.body = body
end.to_hash
Hashie::Mash.new(response_hash)
end
end
client = Client.new
client.received_messages([14,2,3])
# produces XML like this:
# <?xml version="1.0" encoding="UTF-8"?>
# <env:Envelope xmlns:wsdl="somewebserviceprovider.de" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
# <env:Body>
# <wsdl:ReceivedMessagesRequest>messageId14messageId2messageId3</wsdl:ReceivedMessagesRequest>
# </env:Body>
#</env:Envelope>
# correct XML should be:
# <?xml version="1.0" encoding="UTF-8"?>
# <env:Envelope xmlns:wsdl="somewebserviceprovider.de" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
# <env:Body>
# <wsdl:ReceivedMessagesRequest>
# <messageId>14</messageId>
# <messageId>2</messageId>
# <messageId>3</messageId>
# </wsdl:ReceivedMessagesRequest>
# </env:Body>
# </env:Envelope>
@rubiii
Copy link

rubiii commented Feb 14, 2011

there you go:

client.request :wsdl, "ReceivedMessagesRequest" do
  soap.body = { :message_id => [14, 2, 3] }
end

@georgkreimer
Copy link
Author

works perfect, thx for your help.

@rubiii
Copy link

rubiii commented Feb 14, 2011

you're welcome. btw. if you're only using using the body method inside the request block, you could also use a somewhat "secret" little shortcut: https://github.com/rubiii/savon/blob/v0.8.5/lib/savon/client.rb#L112

client.request :wsdl, "ReceivedMessagesRequest", :body => { :message_id => [14, 2, 3] }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment