在Ruby中使用SOAP的最佳方法是什么?

时间:2008-09-02 18:46:09

标签: ruby-on-rails ruby soap

我的客户要求我将第三方API集成到他们的Rails应用程序中。唯一的问题是API使用SOAP。 Ruby基本上放弃了SOAP,转而使用REST。它们提供了一个显然适用于Java-Ruby桥接器的Java适配器,但是如果可能的话,我们希望将它全部保存在Ruby中。我看了一下soap4r,但它的声誉似乎有些不好。

那么将SOAP调用集成到Rails应用程序中的最佳方法是什么?

10 个答案:

答案 0 :(得分:167)

我构建Savon以尽可能简单地通过Ruby与SOAP webservices进行交互 我建议你看一下。

答案 1 :(得分:35)

我们使用了内置的soap/wsdlDriver类,它实际上是SOAP4R。 它的狗很慢,但很简单。你从gems / etc获得的SOAP4R只是同一件事的更新版本。

示例代码:

require 'soap/wsdlDriver'

client = SOAP::WSDLDriverFactory.new( 'http://example.com/service.wsdl' ).create_rpc_driver
result = client.doStuff();

那是关于它的

答案 2 :(得分:14)

我们从Handsoap切换到Savon。

这是比较两个客户端库的series of blog posts

答案 3 :(得分:6)

我还建议Savon。我花了太多时间试图处理Soap4R,没有结果。缺乏功能,没有文档。

萨文是我的答案。

答案 4 :(得分:4)

尝试 SOAP4R

我刚刚在Rails Envy播客(第31集)中听说过这个:

答案 5 :(得分:4)

使用Savon让我的东西在3小时内完成。

Savon主页上的入门文档非常容易理解 - 实际上与我看到的相符(并非总是如此)

答案 6 :(得分:2)

来自Datanoise的Kent Sibilev也将Rails ActionWebService库移植到Rails 2.1(及更高版本)。 这允许您公开自己的基于Ruby的SOAP服务。 他甚至有一个脚手架/测试模式,允许您使用浏览器测试您的服务。

答案 7 :(得分:1)

我遇到了同样的问题,转而使用Savon,然后在开放的WSDL上测试了它(我使用了http://www.webservicex.net/geoipservice.asmx?WSDL),到目前为止一直很好!

https://github.com/savonrb/savon

答案 8 :(得分:1)

我在Ruby中使用SOAP时,我必须为验收测试制作一个假的SOAP服务器。我不知道这是否是解决问题的最佳方式,但它对我有用。

我使用过Sinatra gem(我写过关于为Sinatra创建模拟端点here)用于服务器,还用Nokogiri用于XML东西(SOAP正在使用XML)。

所以,首先我创建了两个文件(例如config.rb和responses.rb),其中我已经放置了SOAP服务器将返回的预定义答案。 在 config.rb 中我放了WSDL文件,但是作为字符串。

@@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>'

responses.rb 中,我已为SOAP服务器将针对不同场景返回的响应提供样本。

@@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>"

现在让我告诉你我是如何实际创建服务器的。

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

我希望你会发现这很有用!

答案 9 :(得分:0)

我使用下面的HTTP调用来调用SOAP方法,

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")
*/

希望它有所帮助。欢呼声。