从 ruby​​ on rails 应用程序提供 XML-RPC 服务? [英] Providing an XML-RPC service from a ruby on rails application?

查看:37
本文介绍了从 ruby​​ on rails 应用程序提供 XML-RPC 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究从现有的 ruby​​ on rails 应用程序提供 XML-RPC 服务的可行性.我希望我可以通过实现一些额外的控制器方法并通过我现有的 apache/passenger 设置来实现它.这种方法是否可行,还是 XML-RPC 需要单独的 Web 服务器?

解决方案

假设 Rails 3:

从 Rails 应用程序提供 XMLRPC 服务有两种或三种基本方法.

首先是使用 Rack 中间件(gem 'rack-rpc' - https://github.com/datagraph/rack-rpc) 支持这一点,理论上应该可以与现有的基于 Rack 的应用程序在同一台服务器上运行,但是当我想使用 ActiveRecord 模型时,我发现这种体验令人沮丧.通过将 rack-rpc 添加到 application.rb 配置中的中间件列表,我能够让 rack-rpc 响应 xml-rpc 客户端请求,但我没有花时间想办法让 ActiveRecord 连接起来.

另一种方法是使用 Rails 1.x 天中已弃用的 actionwebservice 功能的分支.在撰写本文时,我发现一个 fork 在 Rails 3.0.5 上似乎比较实用:

gem 'rubyjedi-soap4r'gem 'actionwebservice', :git =>'https://github.com/mkoentopf/actionwebservice.git'

actionwebservice 功能已被弃用,以鼓励人们使用 RESTful API 来公开 Web 服务,但是如果您有希望能够发送 xmlrpc 请求的客户端(例如那里流行的十几个博客客户端),您显然会没有那个选项.

我按照文档实现了一个 ApiController,它有这样的行:

class ApiController <应用控制器act_as_web_serviceweb_service_dispatching_mode :分层skip_before_filter :your_default_auth_methodweb_service :metaWeblog, MetaweblogService.new(self)web_service :blogger, BloggerService.new(self)...定义xmlrpc接口结尾定义 APIdispatch_web_service_request结尾结尾

然后你需要实现一个Api和一个Service类.(如果您不需要命名空间样式的metaWeblog.methodname"xml-rpc 样式方法调用,您可以简化此操作;只需删除 :layered 调度方法并将其替换为 actionwebservice 文档中解释的替代方法之一.

您的 MyApi 类继承自 ActionWebService::API::Base 并使用 api_method 等方法指定支持的 xml-rpc 方法签名.您的 MyService 继承自 ActionWebService::Base 并简单地实现像普通 ruby​​ 代码一样的方法.您可能需要/想要传递对您实现的 ApiController 类的引用,您可以在服务的 initialize 方法中执行此操作.

I'm looking at the feasibility of providing an XML-RPC service from an existing ruby on rails application. I'm hoping that I can do it by just implementing some additional controller methods and serving it through my existing apache/passenger setup. Is this approach plausible, or will XML-RPC require a separate web server?

解决方案

Assuming Rails 3:

There are two or three basic approaches to offering XMLRPC services from a rails application.

The first is to use Rack middleware (gem 'rack-rpc' - https://github.com/datagraph/rack-rpc) that supports this, which, in theory should be possible to run on the same server as an existing Rack based application, but I found the experience frustrating when I wanted to work with ActiveRecord models. I was able to get rack-rpc to respond to xml-rpc client requests by adding it to the middleware list in the application.rb config, but I did not spend the time to figure out a way to get ActiveRecord hooked up.

An alternative is to use a fork of the deprecated actionwebservice feature from the rails 1.x days. As of this writing, I've found one fork that seems relatively functional on Rails 3.0.5:

gem 'rubyjedi-soap4r'
gem 'actionwebservice', :git => 'https://github.com/mkoentopf/actionwebservice.git'

The actionwebservice feature was deprecated to encourage people to use RESTful APIs to expose web services, but if you have clients that expect to be able to send xmlrpc requests (like a dozen blogging clients that are popular out there) you'll obviously not have that option.

I followed the documentation to implement an ApiController that has lines like this:

class ApiController < ApplicationController
  acts_as_web_service
  web_service_dispatching_mode :layered

  skip_before_filter :your_default_auth_method

  web_service :metaWeblog, MetaweblogService.new(self)
  web_service :blogger, BloggerService.new(self)
...

  def xmlrpc
    api
  end

  def api
    dispatch_web_service_request
  end

end

Then you need to implement an Api and a Service class. (You can simplify this if you don't need the namespace-style "metaWeblog.methodname" xml-rpc style method calls; just remove the :layered dispatching method and replace it with one of the alternatives explained in the actionwebservice docs.

Your MyApi class inherits from ActionWebService::API::Base and uses methods like api_method to specify the supported xml-rpc method signatures. Your MyService inherits from ActionWebService::Base and simply implements the methods like normal ruby code. It's possible you may need/want to pass a reference to the ApiController class that you implement, which you can do in the service's initialize method.

这篇关于从 ruby​​ on rails 应用程序提供 XML-RPC 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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