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 开发者_StackOverflow社区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.
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
.
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.
精彩评论