[ACCEPTED]-What's the best way to use SOAP with Ruby?-soap
I built Savon to make interacting with SOAP webservices 2 via Ruby as easy as possible.
I'd recommend 1 you check it out.
We used the built in soap/wsdlDriver
class, which is actually 4 SOAP4R.
It's dog slow, but really simple. The 3 SOAP4R that you get from gems/etc is just 2 an updated version of the same thing.
Example 1 code:
require 'soap/wsdlDriver'
client = SOAP::WSDLDriverFactory.new( 'http://example.com/service.wsdl' ).create_rpc_driver
result = client.doStuff();
That's about it
We switched from Handsoap to Savon.
Here 1 is a series of blog posts comparing the two client libraries.
I also recommend Savon. I spent too many hours 3 trying to deal with Soap4R, without results. Big 2 lack of functionality, no doc.
Savon is the 1 answer for me.
Try SOAP4R
And I just heard about this on the Rails 1 Envy Podcast (ep 31):
Just got my stuff working within 3 hours 4 using Savon.
The Getting Started documentation 3 on Savon's homepage was really easy to follow 2 - and actually matched what I was seeing 1 (not always the case)
Kent Sibilev from Datanoise had also ported the Rails 5 ActionWebService library to Rails 2.1 (and 4 above). This allows you to expose your own 3 Ruby-based SOAP services. He even has a 2 scaffold/test mode which allows you to test 1 your services using a browser.
I have used HTTP call like below to call 1 a SOAP method,
require 'net/http'
class MyHelper
def initialize(server, port, username, password)
@server = server
@port = port
@username = username
@password = password
puts "Initialised My Helper using #{@server}:#{@port} username=#{@username}"
end
def post_job(job_name)
puts "Posting job #{job_name} to update order service"
job_xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns=\"http://test.com/Test/CreateUpdateOrders/1.0\">
<soapenv:Header/>
<soapenv:Body>
<ns:CreateTestUpdateOrdersReq>
<ContractGroup>ITE2</ContractGroup>
<ProductID>topo</ProductID>
<PublicationReference>#{job_name}</PublicationReference>
</ns:CreateTestUpdateOrdersReq>
</soapenv:Body>
</soapenv:Envelope>"
@http = Net::HTTP.new(@server, @port)
puts "server: " + @server + "port : " + @port
request = Net::HTTP::Post.new(('/XISOAPAdapter/MessageServlet?/Test/CreateUpdateOrders/1.0'), initheader = {'Content-Type' => 'text/xml'})
request.basic_auth(@username, @password)
request.body = job_xml
response = @http.request(request)
puts "request was made to server " + @server
validate_response(response, "post_job_to_pega_updateorder job", '200')
end
private
def validate_response(response, operation, required_code)
if response.code != required_code
raise "#{operation} operation failed. Response was [#{response.inspect} #{response.to_hash.inspect} #{response.body}]"
end
end
end
/*
test = MyHelper.new("mysvr.test.test.com","8102","myusername","mypassword")
test.post_job("test_201601281419")
*/
Hope it helps. Cheers.
I have used SOAP in Ruby when i've had to 16 make a fake SOAP server for my acceptance 15 tests. I don't know if this was the best 14 way to approach the problem, but it worked 13 for me.
I have used Sinatra gem (I wrote 12 about creating mocking endpoints with Sinatra 11 here) for server and also Nokogiri for XML stuff (SOAP 10 is working with XML).
So, for the beginning 9 I have create two files (e.g. config.rb 8 and responses.rb) in which I have put the 7 predefined answers that SOAP server will 6 return. In config.rb I have put the WSDL file, but 5 as a string.
@@wsdl = '<wsdl:definitions name="StockQuote"
targetNamespace="http://example.com/stockquote.wsdl"
xmlns:tns="http://example.com/stockquote.wsdl"
xmlns:xsd1="http://example.com/stockquote.xsd"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns="http://schemas.xmlsoap.org/wsdl/">
.......
</wsdl:definitions>'
In responses.rb I have put samples for 4 responses that SOAP server will return for 3 different scenarios.
@@login_failure = "<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<LoginResponse xmlns="http://tempuri.org/">
<LoginResult xmlns:a="http://schemas.datacontract.org/2004/07/WEBMethodsObjects" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<a:Error>Invalid username and password</a:Error>
<a:ObjectInformation i:nil="true"/>
<a:Response>false</a:Response>
</LoginResult>
</LoginResponse>
</s:Body>
</s:Envelope>"
So now let me show you 2 how I have actually created the server.
require 'sinatra'
require 'json'
require 'nokogiri'
require_relative 'config/config.rb'
require_relative 'config/responses.rb'
after do
# cors
headers({
"Access-Control-Allow-Origin" => "*",
"Access-Control-Allow-Methods" => "POST",
"Access-Control-Allow-Headers" => "content-type",
})
# json
content_type :json
end
#when accessing the /HaWebMethods route the server will return either the WSDL file, either and XSD (I don't know exactly how to explain this but it is a WSDL dependency)
get "/HAWebMethods/" do
case request.query_string
when 'xsd=xsd0'
status 200
body = @@xsd0
when 'wsdl'
status 200
body = @@wsdl
end
end
post '/HAWebMethods/soap' do
request_payload = request.body.read
request_payload = Nokogiri::XML request_payload
request_payload.remove_namespaces!
if request_payload.css('Body').text != ''
if request_payload.css('Login').text != ''
if request_payload.css('email').text == some username && request_payload.css('password').text == some password
status 200
body = @@login_success
else
status 200
body = @@login_failure
end
end
end
end
I 1 hope you'll find this helpful!
I was having the same issue, switched to 2 Savon and then just tested it on an open 1 WSDL (I used http://www.webservicex.net/geoipservice.asmx?WSDL) and so far so good!
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.