= Active Resource -- Object-oriented REST services
Active Resource (ARes) connects business objects and REST web services. It is a library
intended to provide transparent proxying capabilities between a client and a RESTful
service (for which Rails provides the {Simply RESTful routing}[http://dev.rubyonrails.org/browser/trunk/actionpack/lib/action_controller/resources.rb] implementation).
=== Configuration & Usage
Configuration is as simple as inheriting from ActiveResource::Base and providing a site
class variable:
class Person < ActiveResource::Base
self.site = "http://api.people.com:3000/"
end
Person is now REST enable and can invoke REST services very similarly to how ActiveRecord invokes
lifecycle methods that operate against a persistent store.
# Find a person with id = 1
# This will invoke the following Http call:
# GET http://api.people.com:3000/people/1.xml
# and will load up the XML response into a new
# Person object
#
ryan = Person.find(1)
Person.exists?(1) #=> true
# To create a new person - instantiate the object and call 'save',
# which will invoke this Http call:
# POST http://api.people.com:3000/people.xml
# (and will submit the XML format of the person object in the request)
#
ryan = Person.new(:first => 'Ryan', :last => 'Daigle')
ryan.save #=> true
ryan.id #=> 2
Person.exists?(ryan.id) #=> true
ryan.exists? #=> true
# Updating is done with 'save' as well
# PUT http://api.people.com:3000/people/1.xml
#
ryan = Person.find(1)
ryan.first = 'Rizzle'
ryan.save #=> true
# And destruction
# DELETE http://api.people.com:3000/people/1.xml
#
ryan = Person.find(1)
ryan.destroy #=> true # Or Person.delete(ryan.id)
=== Protocol
ARes is built on a standard XML format for requesting and submitting resources. It mirrors the
RESTful routing built into ActionController, though it's useful to discuss what ARes expects
outside the context of ActionController as it is not dependent on a Rails-based RESTful implementation.
==== Find
GET Http requests expect the XML form of whatever resource/resources is/are being requested. So,
for a request for a single element - the XML of that item is expected in response: