有使用 ActiveResource 和 XMLRPC for Rails 的示例吗?

发布于 2024-08-16 16:41:01 字数 686 浏览 8 评论 0原文

我看过大量 ActionWebService 和 XMLRPC 的示例,但它们已经有 3 年历史了,据我了解,ActiveResource 应该取代 ActionWebService。

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

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

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

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

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

I've seen a ton of examples with ActionWebService and XMLRPC, but they're 3 years old and from what I understand, ActiveResource is supposed to replace ActionWebService.

I'm familiar with how ActiveResource can use XML to "talk" to other web sites and consume model information, but XML-RPC is a whole different type of thing, wherein you pass the name of the method you want to execute and the request is handed off, etc.

EDIT - I know how ActiveResource is supposed to work - but I have a client app that needs to use XML-RPC with a defined API (MetaWeblogAPI) and I have no choice but to implement it - hands are tied.

So - specifically: I've been trying to find some docs or a writeup on how XML-RPC might be implemented with Rails using ActiveResource. Perhaps it can't - I'd like to know that too I spose. I'm simply missing the "little leap" - the "how do you hand the request to the method" part where I get to pull the method name out of the XML-RPC request and hand it to the method. I know I'm overthinking this. Can't help it - I'm a .NET guy :).

I've tried to "use what works" - meaning that I've tried to implement ActionWebService but it seems that it doesn't play nice with Rails 2.3.5 (which is what I have installed) as I keep getting an "Unknown Constant" error pointing to ActionWebService, which is installed (which leads me to believe that Rails 2.x doesn't like it).

I'm a bit of a n00b so be gentle :) - I'm sure this is probably a lot easier than I'm making it out to be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韶华倾负 2024-08-23 16:41:01

比您想象的要容易得多。您无需使用 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

然后在使用 Rails 提供的内置 API 的应用程序中,您可以执行诸如 TrackResource.exists?(34)等调用>track = TrackResource.new(:name => "曲目名称"); track.save

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

It is a lot easier than you think. You don't need to muck around with XMLRPC with Rails. You can just make your Rails app serve XML when requested, and you can request XML by simply appending .xml to any URL, as long as you tell your action how to handle .xml requests. Here's an example action:

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

This way, there are no fancy XMLRPC calls necessary, just append .xml to the URL and Rails will know to serve up a steaming serving of XML.

To use this with ActiveResource, you simply do something like this

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

Then in your apps that work with the built-in API rails provides, you can do calls such as TrackResource.exists?(34) and track = TrackResource.new(:name => "Track Name"); track.save etc.

Here's the documentation on ActiveResource. In order for ActiveResource to work, just make sure your Rails app knows to serve XML when requested, with respond_to.

鹊巢 2024-08-23 16:41:01

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

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

然后,LiveWriter 将通过 XML-RPC 与 ActiveResourceProxyService 通信,ActiveResourceProxyService 将向 Web 应用程序返回 ActiveResource 请求。

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

In this case I would keep my ActiveResource site/service nice, clean, and RESTful. Don't muck it up with XML-RPC.

Instead create a proxy service which accepts XML-RPC on one side and translates the requests to ActiveResource on the other.

LiveWriter would then talk to ActiveResourceProxyService via XML-RPC and ActiveResourceProxyService would kick back ActiveResource requests to the web app.

It sounds like you are implementing a simple blogging API, so it shouldn't take too much code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文