views:

670

answers:

2

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.

+2  A: 

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.

Blaine LaFreniere
Right - yes I know this part already and thank you for the code :). I don't have this luxury, however, as I'm working with XML-RPC for MetaWeblog, and I can't tell LiveWriter and Ecto to change the way they do things.Thus the quandry.
Rob Conery
ActiveResource speaks REST, it doesn't play the XML RPC game. For XML RPC, you need an XML RPC client.
Blaine LaFreniere
I have a client - it's LiveWriter and Echo - what I need is an XML-RPC server (thus my question above). I dig that ActiveResource speaks REST... as I mention...
Rob Conery
Your question is confusing. ActiveResource is a client library, which gives me the impression that you're trying to retrieve data from some server.Try this: http://blog.multiplay.co.uk/2008/10/serving-xml-rpc-from-rails-2x/
Blaine LaFreniere
A: 

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.

marshally