ActiveResource: REST, WSDL, XSD?
Monday, September 24, 2007 at 12:41 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.

Reader Comments (4)
A short time ago, the exact most stunning actress all over the world Angelina Jolie was spotted wearing a nice seeking replica watches on not a couple of occasions. And I've been very curious about what precisely the exact watch is. And these days my great friend Mike who functions for replica michael kors watches a fashion magazine pointed out the exact secret for me. I personal a thank to him.
very creative! I like it very much,but If you can add more video and pictures can be much better, I have never read such a lovely article and I am coming back tomorrow to continue reading.omega constellation
footwear you need the most. You can Nike Air Jordans Shoes find a better selection of shoes and sandal online that fits your UGG Classic Tall Boots 5812 style as well as your budget.
Through the years,
Your website have very interesting article. I got knowledge from here. Besides that, your blog is so popular among the searchers from search engines. It means yours website is very goodAlarm Monitoring Houston [url=http://www.smithmonitoring.com/houston-security/]Alarm Monitoring Houston[/url]