Web services with Ruby on Rails

The Ruby on Rails framework has added REST support, making it a Web service alternative to SOAP stack development.

Ruby was a rather obscure object-oriented dynamically typed "scripting language" until it suddenly got a lot of

notice due to the appearance of the Rails Web application framework. The combination quickly became known as Ruby on Rails, I suppose because that creates a better image than Rails on Ruby - in any case, we can use the "RoR" acronym.

As an interpreted language, Ruby performance is not particularly spectacular, but due to the popularity of RoR, serious effort is being applied to change this. The original interpreter was written in C, but there are now versions in Java ( JRuby), .NET ( IronRuby) and probably other languages. The current stable version of Ruby is 1.8.6, released in March of this year. A significant update to version 1.9 and a major upheaval update to version 2.0 are anticipated.

What is Rails?

The Rails Web application framework does not attempt to become all things to all developers. It concentrates on applications using relational databases and the Model-View-Controller design pattern. Ruby on Rails was initially created by David Hansson while working at the Web design company 37signals on a project management tool. The first open source version was released in 2004. Web developers discovered that RoR made creating database-oriented Web applications extremely easy and industry "buzz" was quickly generated.

Developers found that the many built in assumptions and naming conventions in RoR greatly reduce the lines of code needed to get a basic web application going. Naming conventions replace the complex XML configuration files typical of other Web application frameworks. RoR fans express this with the mantra "Convention over configuration."

RoR version 1.2 was released in October 2007 and incorporates many improvements including REST style request support. Rails 2.0, currently at the "release candidate 1" stage, promises even more REST support. SOAP support is available, but Rails developers are encouraging the REST approach.

MVC and Web applications

The Model-View-Controller design pattern first found expression in Smalltalk, the pioneering object-oriented language developed at Xerox. Many developers have found that thinking in MVC terms helps to clarify the architecture of complex applications.

Model

By placing all aspects of data storage and retrieval in a "Model" component, a developer can isolate details behind a well defined interface. You might start a project using a model that contains fake data, move to one with the same interface backed by an in-memory database and finally graduate to a large scale commercial database with no change to other parts of the project.

In Rails applications, a model class builds on base classes, which provide all basic functionality. One custom model class is created for each database table, drawing the names from the table to create the access methods. The RoR tools provide Ruby scripts which can generate source code skeletons for model classes automatically.

View

A "View" is responsible for rendering data in a particular style. These days a Web application might need to support a number of different views. Although RoR was originally intended for HTML pages, the newer editions can also support formats used in Web services. Views are typically generated from a template file which contains elements that always appear, such as HTML markup tags, plus tags which are evaluated dynamically with data from one or more models.

RoR provides base classes supporting view generation in HTML, XML and JavaScript styles. The following table suggests some corresponding clients. JSON stands for JavaScript Object Notation, a format that can convey complex data just like XML, but with savings in speed and bulk.

Format Clients Example
HTML Typical browser A HTML page filled in with user specific information
XML Browser using JavaScript AJAX style dynamic page
JSON Browser using JavaScript Dynamic page using JSON instead of XML
JSON Web Service Client Python and a variety of other languages support JSON parsing

XML templates could be used to create simple SOAP messages, but it appears that most attempts to create SOAP or XML-RPC Web services with RoR have put a lot of output generation into the controller component, thus straying from a strict MVC pattern. The SOAP service examples I could find do not implement the more advanced WS-* features found in other frameworks. RoR developers appear to be putting much more emphasis on REST style Web services for future development.

Controller

A "Controller" typically interprets inputs from a user or server request to decide which model objects will be used for data, the actions to be taken and which view will generate the response. Controller classes can take advantage of a large library of helper classes for dealing with HTML elements. Recent versions of RoR have increase support for REST style requests by creating controller base classes, which can interpret request URLs in REST terms.

Legal status of Ruby and Rails

The Ruby language is copyrighted free software with its own license conditions. The Rails framework is released under the extremely open conditions of the MIT License. David Hansson has filed for trademark protection on the use of "RUBY ON RAILS", "RAILS" and the official Rails logo. The initial attempt to create a parallel development based on the Java-based scripting language Groovy, known as Groovy on Rails, met with an objection from Hansson so the project is now known as Grails and appears to be doing its own thing.

Platforms for RoR

The Rails open source developers are generally using Apple Mac OS X, and the recently released OS X version 10.5, called Leopard, includes RoR. Windows and Linux based developers should have no problem installing Ruby and once you have the Ruby language installed, the "RubyGems" package manager can take care of installing the latest Rails distribution. You will also need a relational database, but RoR can work with most of the popular open source and commercial systems. MySQL, SQLite and PostgreSQL are popular choices. There are also various bundle packages that include a web server and database for even simpler installation.

The popular IDEs (Integrated Development Environments) Netbeans and Eclipse have added modules which support Ruby coding. There are also several projects to create IDEs specifically to support RoR. Most of these appear to be based on Eclipse.

Conclusions

RoR has mainly been used to create browser-based Web applications. Due to the flexibility of the Model-View-Controller design pattern, such applications can be extended to cover a variety of clients without changing the data model components. Developers who feel that the SOAP approach to Web services has just gotten too complex will be attracted to RoR for the REST support.

References

Ruby on Rails API introduction

Ruby as a Web service client language

This Wikipedia survey article cites many commercial projects using RoR


This was first published in November 2007

Dig deeper on Representational State Transfer (REST)

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide

SearchWinDevelopment

Close