O'Reilly logo

Ruby on Rails® for Microsoft Developers by Antonio Cangiano

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

10.1. ActiveResource

Because requesting GET /articles.xml in your blog example produces XML data, you could interact with the REST service in this manner, without any wrapper and with "low-level" requests. But just like interacting with databases via low-level queries is more time consuming and error prone than using ActiveRecord, so is working with Web Services by sending handmade HTTP requests.

The idea behind ActiveResource is then very simple. ActiveRecord simplifies the process of interacting with databases, by providing object-relational mapping between relational data and business objects. ActiveResource does the same, only mapping REST resources. Both ActiveRecord and ActiveResource rely on a series of conventions and assumptions to be able to provide a wrapper to map structures containing the data (relational tables and resources, respectively) to model objects.

A script, desktop application, or Web application, can take advantage of ActiveResource, by simply defining a model for a given remote REST resource. When this prerequisite has been fulfilled, it will be possible to use that model through an API similar to ActiveRecord's one. Unlike ActiveRecord, which operates on database records and sends queries to the database, ActiveResource operates on remote HTTP resources by sending XML requests to the remote Web Service. Any XML received in response will then be opportunely serialized, so that you can continue to work with Ruby objects, as opposed to raw data.

Under the ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required