Ruby-on-Rails
  Home arrow Ruby-on-Rails arrow Page 3 - Web Forms and Ruby on Rails
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
RUBY-ON-RAILS

Web Forms and Ruby on Rails
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2007-04-19

    Table of Contents:
  • Web Forms and Ruby on Rails
  • 15.17 Creating an Ajax Form
  • 15.18 Exposing Web Services on Your Web Site
  • 15.19 Sending Mail with Rails

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Web Forms and Ruby on Rails - 15.18 Exposing Web Services on Your Web Site


    (Page 3 of 4 )

    Problem

    You want to offer SOAP and XML-RPC web services from your web application.

    Solution

    Rails comes with a built-in web service generator that makes it easy to expose a controllers actions as web services. You don't have to spend time writing WSDL files or even really know how SOAP and XML-RPC work.

    Here's a simple example. First, follow the directions in Recipe 15.16 to create a database table named items, and to generate a model for that table. Don't generate a controller.

    Now, run this from the command line:

      ./script/generate web_service Item add edit fetch
            create  app/apis/
            exists  app/controllers/
            exists  test/functional/
            create  app/apis/item_api.rb
            create  app/controllers/ item_controller.rb
            create  test/functional/ item_api_test.rb

    This creates an item controller that supports three actions: add, edit, and fetch. But instead of web application actions with .rhtml views, these are web service actions that you access with SOAP or XML-RPC.

    A Ruby method doesn't care about the data types of the objects it accepts as arguments, or the data type of its return value. But a SOAP or XML-RPC web service method does care. To expose a Ruby method through a SOAP or XML-RPC interface, we need to define type information for its signature. Open up the file app/apis/ item_api.rb and edit it to look like this:

      class ItemApi < ActionWebService::API::Base
        api_method :add, :expects => [:string, :string], :returns => [:int]
        api_method :edit, :expects => [:int, :string, :string], :returns => [:bool]
        api_method :fetch, :expects => [:int], :returns => [Item]
     
    end

    Now we need to implement the actual web service interface. Open app/controllers/ item_controller.rb and edit it to look like this:

      class ItemController < ApplicationController
        wsdl_service_name 'Item'

        def add(name, value)
          Item.create(:name => name, :value => value).id
        end

        def edit(id, name, value)
          Item.find(id).update_attributes(:name => name, :value => value)
        end

        def fetch(id)
          Item.find(id)
        end
      end

    Discussion

    The item controller now implements SOAP and XML-RPC web services for the items table. This controller can live alongside an items controller that implements a traditional web interface.*

    The URL to the XML-RPC API is http://www.yourserver.com/item/api, and the URL to the SOAP API is http://www.yourserver.com/item/service.wsdl. To test these services, here's a short Ruby script that calls the web service methods through a SOAP client:

      require 'soap/wsdlDriver' 

      wsdl = http://localhost:3000/item/service.wsdl
      item_server = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver

      item_id = item_server.add('foo', 'bar')

      if item_server.edit(item_id, 'John', 'Doe')
        puts 'Hey, it worked!'
      else
        puts 'Back to the drawing board...'
      end
      # Hey, it worked!

      item = item_server.fetch(item_id)
      item.class     # => SOAP::Mapping::Object
      item.name      # => "John"
      item.value     # => "Doe"

    Here's the XML-RPC equivalent:

      require 'xmlrpc/client'
      item_server = XMLRPC::Client.new2('http://localhost:3000/item/api')

      item_id = item_server.call('Add', 'foo', "bar")
     
    if item_server.call('Edit', item_id, 'John', 'Doe')
        puts 'Hey, it worked!'
      else
       
    puts 'Back to the drawing board...'
      end
     
    # Hey, it worked!

      item = item_server.call('Fetch', item_id)
      # => {"name"=>"John", "id"=>2, "value"=>"Doe"}
      item.class                   # => Hash

    See Also

    1. Matt Biddulph's article "REST on Rails" describes how to create REST-style web services on top of Rails (http://www.xml.com/pub/a/2005/11/02/rest-on-rails.html)
    2. Recipe 16.3, "Writing an XML-RPC Client," and Recipe 16.4, "Writing a SOAP Client"
    3. Recipe 16.5, "Writing a SOAP Server," shows a nonRails implementation of SOAP web services

    More Ruby-on-Rails Articles
    More By O'Reilly Media


       · This article is an excerpt from the "Ruby Cookbook," published by O'Reilly. We hope...
     

    Buy this book now. This article is excerpted from chapter 15 of the Ruby Cookbook, written by Lucas Carlson and Leonard Richardson (O'Reilly, 2006; ISBN: 0596523696). Check it out today at your favorite bookstore. Buy this book now.

    RUBY-ON-RAILS ARTICLES

    - Iterating and Incrementing Strings in Ruby
    - Comparing and Manipulating Strings in Ruby
    - Strings in Ruby
    - Ruby On Rails: Making Your First Dynamic Site
    - Ruby on Rails: Beginning Rails
    - Ruby: Modules, Mixins, Fixins, and Rails
    - Controlling Information Access with the Rail...
    - URLs, Filters and the Rails Action Controller
    - Flash and the Rails Action Controller
    - Rails Action Controller
    - Dropping and Sorting with AJAX and script.ac...
    - Drag and Drop with script.aculo.us and Rails
    - Introducing script.aculo.us
    - Ruby Classes and Objects
    - Ruby Loops







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek