ActiveResource: REST, WSDL, XSD?

Posted by Andre Foeken Mon, 24 Sep 2007 14:28:28 GMT

We love REST. It's simple and clean, and combined with ActiveResource it is certainly the best app to app bridge we've worked with so far. But...

What if you want to have more freedom? Say we want to build a database based on a REST webservice. Now imagine we don't want any info about the service in the ruby program that actually builds the db.

We are facing two major problems:

  • We don't know which resources are available.
  • We don't know the fields and types of the resource in advance.

The first problem will eventually be solved with WSDL 2.0 or if you need a solution right now: by a default listing resource.

 # Our default object is called a Resource, on the server we have 
 # a Resource object that just returns each resource we have in 
 # a string array.
 resources = Resource.find(:all)
   => ["address","person","country"]
 # Now we can do all kinds of crazy stuff :)
 resources.each do |resource|
   name = resource.capitalize.to_sym
   new_resource = Object.const_set(name, Class.new(ActiveResource::Base))
   new_resource.site = Resource.site
 end

After this little piece of code we have a full dynamic set of ActiveResource classes ready to be used :)

The second problem we face is much more interesting. Normally some kind of resource definition would be applied like XSD. But native ruby XSD support is kind of lacking (it sucks) and more importantly it doesn't feel like a rails solution.

We wanted a cleaner, simpler and more elegant (more RESTy) solution. How about Person.schema?

It returns an ActiveResourceSchema object:

Person.schema.fields
   => { :name => FixNum, :date_of_birth => DateTime, 
:parents => [{ :id => FixNum }] }

What happens is actually quite simple. When the Resource objects receives the schema method call it calls find(:first, :params => { :schema => true }).

The server responds to this param with a sample record, with just the field names and types. We build an ActiveResourceSchema object to wrap those and return a nice array of fields :)

Note: These code snippets are just examples, we are currently building this. If there's enough interest we might submit it as a plugin/patch for ARes.

Posted in ,  | Tags , , , ,  | 1 comment

SOAP hell? ARes Heaven?

Posted by Andre Foeken Mon, 04 Jun 2007 16:44:31 GMT

Linking Ruby to Ruby was no problem. Even Java to Ruby seemed to work just fine. But the problems started as we grew past the infant stage and tried to send more that say a thousand records over the SOAPy connection.

The amount of XML was just to big to handle, the speed became unacceptable. Our first instinct was Gzipping the files, but that made almost no difference. Finally we tried SWA, SOAP with Attachments. SWA worked flawlessly...in Java...and in Ruby. Just not together. We tried everything but no luck.

Finally we decided we needed a fresh start, we threw the SOAP away (with the bathwater) and took a look at ActiveResource (Ruby) and RESTLets (Java).

ARes is wonderful, it worked (almost) out of the box and there was a lot less overhead. Combined with Zlib::Deflate this proved to be a good alternative. Sure it still doesn't win any speed races, but it is functional, easy to set up and simple to maintain.

The definition of almost:

Ticket 8563

Ticket 8566

Ticket 8567

Ticket 8568

Posted in ,  | Tags , , ,  | no comments