Instant REST on Rails: ActiveResource
DHH gave his keynote at RailsConf tonight, and it was totally awesome. It focused on the new CRUD features that will show up in Rails 1.2, and all of the niceness that comes out of them.
Taking a page from REST protocols such as Atompub and the related GData, Rails will support "resources." These resources are designed to support basic CRUD operations, which are driven by the full set of HTTP verbs: GET, POST, PUT, and DELETE. This, combined with the #respond_to and #to_xml methods already in Rails, as well as some enchancements to the Routes package, allow programmers to build very DRY RESTful websites and webservices.
The coolest part of the keynote was the Steve Jobs-esque announcement of the ActiveResource library, which DHH just started building a few days ago. This new library would abstract a REST webservice built following certain conventions, similar to how ActiveRecord currently abstracts a relational database. For example, you would write:
Person = ActiveResource::Struct.new do |p|
p.uri "http://www.myhost.com/people"
p.credentials :username => "maurice", :password => "pass"
end
person = Person.find(1)
person.name = "Maurice"
person.save!
Which would connect to the REST resource located at the give URI, and then use GET/POST/PUT/DELETE to interact with it-- essentially giving your REST webservice a free library that enables you to consume it. In this example, it would call "GET /people/1", turn the resulting XML into a struct. Then it would POST an updated copy of the struct to the service.
ActiveResource will make it almost trivial for Ruby programs to consume RESTful services and make it really easy to integrate multiple Rails applications, all while keeping a very simple API. Overall it looks great, and I'm really looking forward to be able to play with it.
Tags: railsconf activeresource REST


0 Comments:
Post a Comment
<< Home