質問

私のクライアントから、サードパーティ API を Rails アプリに統合してほしいと頼まれました。唯一の問題は、API が SOAP を使用していることです。Ruby は基本的に SOAP を廃止し、REST を採用しました。彼らは明らかに Java-Ruby ブリッジで動作する Java アダプターを提供していますが、可能であればすべてを Ruby で維持したいと考えています。soap4rについて調べてみましたが、若干評判が悪いようです。

では、SOAP 呼び出しを Rails アプリに統合する最良の方法は何でしょうか?

役に立ちましたか?

解決

内蔵のものを使用しました soap/wsdlDriver クラス、実際には SOAP4R です。とても遅いですが、とてもシンプルです。gems/etc から入手できる SOAP4R は、同じものの単なる更新バージョンです。

コード例:

require 'soap/wsdlDriver'

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

それくらいです

他のヒント

私が建てました サボン Ruby を介した SOAP Web サービスとの対話をできるだけ簡単にします。
ぜひチェックしてみてください。

ハンドソープからサボンに切り替えました。

がここにあります 一連のブログ投稿 2 つのクライアント ライブラリを比較します。

私もお勧めします サボン. 。Soap4R に対処するのにあまりにも多くの時間を費やしましたが、結果は得られませんでした。機能が大幅に欠如しており、ドキュメントもありません。

サボンが私にとっての答えです。

試す SOAP4R

Rails Envy ポッドキャスト (第 31 話) でこれについて聞きました。

Savon を使用して 3 時間以内に作業が完了しました。

Savon のホームページにある Getting Started ドキュメントは非常にわかりやすく、実際に私が見ていたものと一致していました (常にそうであるとは限りません)

ケント・シビレフより データノイズ また、Rails ActionWebService ライブラリを Rails 2.1 (以降) に移植しました。これにより、独自の Ruby ベースの SOAP サービスを公開できるようになります。ブラウザを使用してサービスをテストできるスキャフォールド/テスト モードもあります。

私も同じ問題を抱えていて、Savon に切り替えて、オープン WSDL でテストしました (私は使用しました http://www.webservicex.net/geoipservice.asmx?WSDL)そして今のところとても良いです!

https://github.com/savonrb/savon

受け入れテスト用に偽の SOAP サーバーを作成する必要があったときに、Ruby で SOAP を使用しました。これが問題に対処する最善の方法かどうかはわかりませんが、私にとってはうまくいきました。

私はSinatra gemを使用しました(Sinatraを使用したモックエンドポイントの作成について書きました) ここ) サーバー用、また 鋸切 XML に関するもの (SOAP は XML で動作します)。

そこで、最初に 2 つのファイルを作成しました (例:config.rb およびresponse.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>'

応答.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

これがお役に立てば幸いです。

以下のような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")
*/

それが役に立てば幸い。乾杯。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top