Rails - 使用另一个应用程序的 SOAP 界面 [英] Rails - Using another app's SOAP interface

查看:36
本文介绍了Rails - 使用另一个应用程序的 SOAP 界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个纯粹的、可运行的 Rails 应用程序.

我现在需要它来开始与另一个应用程序通信(sympa).这个应用程序公开了一个 SOAP 接口,所以我尝试使用它是有道理的(命令行界面不完整).

我应该遵循哪些步骤?

我应该使用哪些 gems/插件?

有人有工作教程/示例吗?

我或多或少熟悉一般 SOAP 概念,但我以前从未使用过 SOAP.我了解 REST.

解决方案

这是 Rails 2.x 的过时答案.要获得更现成的答案,我建议您观看 railscast #290 .如果有人出于某种原因仍在使用 rails 2.x,并且无法应用 Ryan Bates 在那里所说的内容,我将在这里留下这个答案.

我已经为此挣扎了好几天了,我想我自己找到了解决方案.

我能找到的用于 SOAP 交互的最简单、最活跃的 gem 称为 Savon.>

它应该适用于 Ruby 本身.以下是有关如何将其与 Rails 结合使用的快速浏览:

安装 gem.最简单的方法是编辑配置/环境并添加

config.gem "savon"

然后运行

rake gems:install

这应该会安装 savon 以及更多 gems.

接下来,在您的 app/models/ 目录中创建一个类(它不必是 ActiveRecord 的子类,只是您模型目录中的一个常规类)

如果你和我一样,你会希望尽可能远离 XML.你可以通过创建一个这样的类来做到这一点:

class MyWebserviceWSDL = "http://www.theWebSiteWithAService.com/wsdl"def self.client@@client ||= Savon::Client.new(WSDL)结尾def self.soap_actions返回 client.wsdl.soap_actions结尾def self.invoke(动作,参数)response = client.send(action) { |soap|soap.body = 参数 }返回 response.to_hash结尾结尾

您将主要使用它来调用方法.您能够调用的方法类型取决于其他站点"提供的服务.假设有 3 个操作可用 - :create_monkey:destroy_monkey &:list_monkeys.您可以通过在 rails 控制台上执行此操作来确认列表是否正确:

MyWebservice.soap_actions=>[:create_monkey, :destroy_monkey, :list_monkeys]

现在假设您要调用 :create_monkey.首先,您需要知道该调用需要哪些参数.查看此内容的最佳位置是 wsdl 文件本身.您应该会看到如下内容:

<part name="name" type="xsd:string"/><part name="hair_color" type="xsd:string"/></消息><消息名称="create_monkey_response"><part name="status" type="xsd:string"/></消息>

所以它需要两个参数:namehair_color.在 ruby​​ 控制台上,您可以像这样调用它:

MyWebService.invoke :create_monkey, {:name =>'坦率', :hair_color =>'红色的' }=>{:状态=>'好的'}

您将收到一个哈希作为响应.在这种情况下,我得到了ok"状态,但它可能要复杂得多.

稍后,您可以创建(例如)一个名为 tableless 模型code>Monkey,并定义使用 webservice 的方法,如 newcreate 等.

我遗漏了很多有趣的事情,例如安全性.但是,如果您遇到与我相同的问题,这应该可以帮助您入门.

问候!

I've got a pure, working, Rails application.

I now need it to start communicating with another application (sympa). This application exposes that exposes a SOAP interface, so it makes sense that I try to use it (the command line interface is incomplete).

Which steps should I follow?

What gems/plugins should I use?

Does anyone have working tutorials / examples?

I'm more or less familiar with the general SOAP concepts, but I've never used SOAP before. I understand REST.

解决方案

EDIT: this is an outdated answer dating of rails 2.x. For a more present answer, I recommend you to watch railscast #290 . I'm leaving this answer here in case someone is still using rails 2.x for some reason, and can't apply what Ryan Bates says there.

I've been fighting with this for some days now and I think I found a solution myself.

The simplest, most active gem that I could find for SOAP interaction is called Savon.

It's supposed to work with Ruby itself. Here's a quick tour on how you use it with Rails:

Install the gem. Easiest way is to edit config/environment and add

config.gem "savon"

And then run

rake gems:install

This should install savon along with a couple more gems.

Next, create a class on your app/models/ directory (it doesn't have to be a subclass of ActiveRecord, just a regular class on your models directory)

If you are like me, you will want to stay as far away from XML as possible. You can do so by creating a class like this one:

class MyWebservice

  WSDL = "http://www.theWebSiteWithAService.com/wsdl"

  def self.client
    @@client ||= Savon::Client.new(WSDL)
  end

  def self.soap_actions
    return client.wsdl.soap_actions
  end

  def self.invoke(action, parameters)
    response = client.send(action) { |soap| soap.body = parameters }
    return response.to_hash
  end

end

You will be mostly using it for invoking methods. The kind of methods you will be able to invoke depends on the services that "the other site" provides. Let's imagine that 3 actions are available - :create_monkey, :destroy_monkey & :list_monkeys. You can confirm that the list is correct by doing this on the rails console:

MyWebservice.soap_actions
=> [:create_monkey, :destroy_monkey, :list_monkeys]

Now imagine that you want to invoke :create_monkey. First you need to know which parameters are needed for that call. The best place to look at this is the wsdl file itself. You should see something like this:

<message name="create_monkey_request">
  <part name="name" type="xsd:string"/>
  <part name="hair_color" type="xsd:string"/>
</message>
<message name="create_monkey_response">
  <part name="status" type="xsd:string"/>
</message>

So it takes two parameters: name and hair_color. On the ruby console, you can invoke it like this:

MyWebService.invoke :create_monkey, {:name => 'frank', :hair_color => 'red' }
=> {:status => 'ok'}

You will get a hash as a response. In this case I got an 'ok' status, but it could be far more complex.

Later on, you can create (for example) a tableless model called Monkey, and define methods like new, create, etc that use the webservice.

I'm leaving out lots of interesting things, such as security. But this should get you started if you have the same problem I had.

Regards!

这篇关于Rails - 使用另一个应用程序的 SOAP 界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆