3

我已经看到了大量关于 ActionWebService 和 XMLRPC 的示例,但它们已经 3 岁了,据我了解,ActiveResource 应该取代 ActionWebService。

我熟悉 ActiveResource 如何使用 XML 与其他网站“对话”并使用模型信息,但 XML-RPC 是一种完全不同类型的东西,其中您传递要执行的方法的名称和请求被移交等

编辑 - 我知道 ActiveResource 应该如何工作 - 但我有一个客户端应用程序需要使用 XML-RPC 和定义的 API (MetaWeblogAPI),我别无选择,只能实现它 - 手被束缚了。

所以 - 具体来说:我一直在尝试寻找一些关于如何使用 ActiveResource 使用 Rails 实现 XML-RPC 的文档或文章。也许它不能——我也想知道。我只是错过了“小飞跃”——“你如何将请求交给方法”部分,我可以从 XML-RPC 请求中提取方法名称并将其交给方法。我知道我想多了。忍不住——我是一个 .NET 人 :)。

我尝试“使用有效的方法” - 这意味着我尝试实现 ActionWebService 但它似乎与 Rails 2.3.5 (这是我安装的)不兼容,因为我不断收到“未知”指向已安装的 ActionWebService 的“常量”错误(这让我相信 Rails 2.x 不喜欢它)。

我有点n00b所以要温柔:) - 我相信这可能比我想象的要容易得多。

4

2 回答 2

2

比你想象的要容易得多。您不需要在 Rails 中使用 XMLRPC。您可以让 Rails 应用程序在请求时服务 XML,并且您可以通过简单地将 .xml 附加到任何 URL 来请求 XML,只要您告诉您的操作如何处理 .xml 请求。这是一个示例操作:

def show
  @post = Post.find(:all, :conditions => { :id => params[:id] }
  respond_to do |format|
    format.html do
      # this is the default, this will be executed when requesting http://site.com/posts/1
    end
    format.xml do
      # this will be rendered when requesting http://site.com/posts/1.xml
      render :xml => @post
    end
  end
end

这样,就不需要花哨的 XMLRPC 调用,只需将 .xml 附加到 URL,Rails 就会知道提供大量 XML 服务。

要将其与 ActiveResource 一起使用,您只需执行以下操作

class Resource < ActiveResource::Base
  self.site = Settings.activeresource.site # 'http://localhost:3000/
  self.user = Settings.activeresource.username # Only needed if there is basic or digest authentication
  self.password = Settings.activeresource.password
end
class GenreResource < Resource
  self.element_name = 'genre'
end
class ArtistResource < Resource
  self.element_name = 'artist'
end
class AlbumResource < Resource
  self.element_name = 'album'
end)
class TrackResource < Resource
  self.element_name = 'track'
end
class AlbumshareResource < Resource
  self.element_name = 'albumshare'
end

然后在使用内置 API rails 提供的应用程序中,您可以进行诸如此类的TrackResource.exists?(34)调用track = TrackResource.new(:name => "Track Name"); track.save

这是有关 ActiveResource 的文档。为了使 ActiveResource 工作,只需确保您的 Rails 应用程序知道在请求时服务 XML,使用respond_to.

于 2009-12-27T22:36:10.390 回答
0

在这种情况下,我会让我的 ActiveResource 站点/服务保持良好、干净和 RESTful。不要用 XML-RPC 搞砸它。

而是创建一个代理服务,它一方面接受 XML-RPC,另一方面将请求转换为 ActiveResource。

然后,LiveWriter 将通过 XML-RPC 与 ActiveResourceProxyService 对话,ActiveResourceProxyService 会将 ActiveResource 请求回退到 Web 应用程序。

听起来您正在实现一个简单的博客 API,因此不需要太多代码。

于 2009-12-29T04:12:17.167 回答