Web Services on Rails
A Web 2.0 perspective for SOA
Feb. 12, 2007 11:00 AM
The term Web Services refers to loosely coupled, executable application components linked dynamically over the network with open standards. Typically, they are software components that employ one or a combination of the following XML standards: SOAP, WSDL, and UDDI. Ruby on Rails is an open source Web application framework written in Ruby that closely follows the Model View Controller (MVC) architecture. It strives for simplicity, allowing real-world applications to be developed in less code than other frameworks and with a minimum of configuration. To define Rails - the Web Services or a Web Framework - we have to say that Rails is a full-stack, open source Web development framework that requires comparatively less time and effort to code XML interfaces than most other frameworks.
BlogController < ActionController::Base
Actions grouped in controller as methods
<% for post in @posts %>
Embedded Ruby for templates
Choosing a Web Server
Although WEBrick is the most convenient choice, it's not the most scalable or flexible choice. The Apache Web server is the most widely deployed Web server in the world. You can choose from an incredible array of plug-ins to run dozens of programming languages or serve other kinds of dynamic content. Apache scales well, with outstanding caching plug-ins and good support for load balancers and sprayers (machines that efficiently spread requests across multiple Web servers). If you're looking for a safe solution, look no further than the Apache Web server.
Other options are lighttpd, Mongrel, and any Web server that supports CGI.
Action Web Service
The Api can then be used in the controller class using the statement web_service_api <apiName>. The following are the advantages of using Action Web Services:
Although SOAP stands for Simple Object Access Protocol, creating messages that can comply with all the interoperability rules (Basic Profile Compliance) is difficult, and the messages generated are complex and huge. Thus, another standard called REST based Web Services are becoming mainstream with industry leaders like Yahoo and Amazon exposing their Web Services as REST Web Services.
The creation of REST Web Services is supported by Rails 1.1 and above; this makes the creation of Web Services in Rails a child's play. REST stands for Representational State Transfer; this basically means that each unique URL is a representation of some object. We can get the contents of that object using an HTTP GET; to delete it, we might use a POST, PUT, or DELETE to modify the object.
Some Advantages of REST over SOAP
Creating a Simple Web Service
Now, browse to the API folder and edit the helloworld_api.rb to define the interface. This step is optional but it's recommended to implement.
class HelloWorldApi < ActionWebService::API::Base
Next, write the controller. Edit the helloworld_controller.rb. Here, we want to write the actual Web service code and specify the name of the API for the Web Service.
class HelloWorldController < ApplicationController
Running the Web Service
ruby script/server -p <port>
The endpoint for the Web Service is given below:
The following is the WSDL endpoint:
Testing Web Services
Dynamic typed languages like Ruby extensively use REST instead of SOAP-based Web Services. The core architecture has many similarities with the J2EE architecture. However, the approach to development of Web applications is different in the two frameworks. Rails prefers explicit code instead of configuration files, and the dynamic nature of the Ruby language generates much of the plumbing code at runtime. Finally, the MVC structure of development of Web applications is the biggest advantage in Rails.
Reader Feedback: Page 1 of 1
SOA World Latest Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
SYS-CON Featured Whitepapers
Most Read This Week