Is There an Example of Using ActiveResource and XMLRPC for Rails?

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.

-------------Problems Reply------------

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.

Category:ruby on rails Views:0 Time:2009-12-27

Related post

  • Conditional 'attr_accessible' using ActiveResource in Ruby on Rails 2011-01-11

    I have two RoR3 application: http://users.domain.local http://profiles.domain.local I created the 'users/models/profile.rb': class Profile < ActiveResource::Base self.site = "http://profiles.domain.local" end In 'profiles/models/profile.rb' I have

  • How to use rest-client with activeresource for a non-rails api 2012-02-10

    I need to create an interface in rails which may not work directly with activeresource. How would I create a model that uses rest-client to talk to the api directly? Would it be better for me to keep using active resource but override the default beh

  • Problem Unit Testing ActiveResource Model with HttpMock 2010-04-02

    I'm working on unit testing an ActiveResource model in my Rails application. I have my ActiveResource::HttpMock code in my test file as follows: ActiveResource::HttpMock.respond_to do |mock| mock.get "/audience/42/people/1.xml", {"Authorization" =

  • Consume non-Rails REST API with ActiveResource 2012-02-10

    I am using ActiveResource against a non-Rails REST API... in fact even the "Rest" part is doubtful but they tried: Although RESTful applications are ideally stateless, the ALM platform requires sessions to manage locking, client life time, and perfor

  • Can I use just migrations from rails to maintain a database schema? 2009-09-19

    Our application isn't going to use rails for any part of its final state, but migrations are a fantastic way to define schema in my experience, so I'd like to use just that one aspect of rails. Is this a reasonable thing to do? If not, are there any

  • Show server output when running request specs 2011-05-10

    I use RSpec for integration tests. Unfortunately when running those request specs I often miss important errors as I don't directly see the output of the test web server. Is there a way to get this stuff on the console, too? --------------Solutions--

  • How Do I Authenticate to ActiveResource to Avoid the InvalidAuthenticityToken Response? 2008-09-29

    I created a Rails application normally. Then created the scaffold for an event class. Then tried the following code. When run it complains about a InvalidAuthenticityToken when the destroy method is executed. How do I authenticate to avoid this respo

  • How do I create an ActiveRecord relationship to an ActiveResource object? 2008-10-08

    Let's say I'm writing a Library application for a publishing company who already has a People application. So in my Library application I have class Person < ActiveResource::Base self.site = "http://api.people.mypublisher.com/" end and now I want

  • Ruby On Rails: ActiveResource and file uploads 2009-01-18

    Does anyone have experience with ActiveResource and file uploads? I have an Adobe Air desktop application which communicates with my Rails driven website's web services. I'd like to post files to the server but I'm not entirely sure if I could do thi

  • Can ActiveResource models integrate with ActiveRecord models? 2009-02-01

    I'm working on a Rails app that will serve as an authentication system for other Rails apps through Rails's ActiveResource functionality. The authentication app has an ActiveRecord model called User. The client app has an ActiveResource model called

  • Has anyone written a plugin to add the final polish to ActiveResource? 2009-02-04

    I have been using ActiveResource in my Ruby on Rails applications for some time, and I am starting to really feel that it needs a final polish to make it go from "pretty nice" to "beautiful". I would like to be able to use an ActiveResource as if it

  • Rails: ActiveResource - Can I set the ID explicitly? 2009-03-02

    I am using ActiveResource in Rails to manage entities in a separate database over REST. I have been trying to explicitly manage the IDs of the remote resources, as for the current case it is simplest to just re-use the local resource's ID, rather tha

  • Rails ActiveResource HABTM return values 2009-03-25

    I have two models: Company and User they have a has_and_belongs_to_many relationship. I'm using active resource with a method to get all users of a company: def users @company = Company.find( params[:id], :include => [:users] ) render :xml => @

  • How to consume JSON formatting with ruby's ActiveResource? 2009-05-05

    ActiveRecord introduced a change to its default JSON output format. It went from { "user": { some_junk } } to { some_junk } ActiveResource has apparently followed their lead, expecting to consume JSON as { some_junk } I am trying desperately to consu

  • rails activeresource messages 2009-05-07

    I have a quick question about active resource. If I have a User resource, when I do a call like User.find(1).put(:promote, :position => 'manager') According to the api it translates into this call /users/1/promote.xml?position=manager My question

  • ActiveResource error response header doesn't return body 2009-05-07

    I asked something similar to this before and never got an answer, here's a shortened version I have a User activeresource model. I make a simple call on my client to the service response = User.find(id).put(:activate, :activation_code => activatio

  • Rails, ActiveResource, and Pagination 2009-05-08

    What's the best way to implement pagination in a REST API so that an ActiveResource client can easily navigate paginated results? There have been some proposals, for example here and here, but being new to ActiveResource I'm wondering if there's a be

  • How do I use ActiveResource with a custom URL scheme? 2009-05-16

    I'm trying to create ActiveResource objects for three objects in an internal application. There are Tags, Taggings, and Taggables: http://tagservice/tags/:tag http://tagservice/taggings/:id http://tagservice/taggables/:type/:key Tag's :tag is the URL

  • How to Use ActiveResource with Shallow Nested Routes? 2009-05-20

    I have a Rails application that has a Company resource with a nested resource Employee. I'm using shallow routing, so to manipulate Employee, my routes are: GET /employees/1 PUT /employees/1 DELETE /employees/1 POST /companies/1/employees How can I c

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.116 (s). 11 q(s)