Providing an XML-RPC service from a ruby on rails application?

两盒软妹~` 提交于 2019-12-21 02:57:13

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/5541768/providing-an-xml-rpc-service-from-a-ruby-on-rails-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!