Introduction

WebORB for Ruby on Rails provides an implementation of the RPC functionality available in Adobe's Flex Data Services. Using WebORB Ruby developers can integrate Flex client applications with services deployed in Rails applications.

This guide reviews the process of creating a Flex RPC client using Flex Builder 2.0 and connecting it with a Ruby on Rails application. At the end of the walkthrough you will have a Flex application communicating with a Ruby object exposed through WebORB on Rails.

WebORB Installation

Make sure the following software is installed before downloading WebORB for Rails:

  • Ruby v. 1.8 or newer
  • Ruby on Rails v.1.1.4 or newer

We recommend the following guide for installing Ruby and Ruby on Rails: http://wiki.rubyonrails.org/rails/pages/GettingStartedWithRails

Once Ruby and Rails is installed, create a new Rails application:

> rails myrailsapp

Change the current directory to the directory for the new rails application and run the following command to download and install WebORB into the current Rails application:

> ruby script/plugin install http://themidnightcoders.net:8089/svn/weborb

Now WebORB for Rails is installed and ready to process Flex and Flash Remoting invocation requests. Start a web server for your Rails application using the "ruby script/server" command:

> ruby script/server

You can verify the installation by running a remoting test suite application bundled with the WebORB distribution. Open http://localhost:3000/examples/main.html in a browser. When the application is loaded, you can run various invocation tests. Each tests invokes methods on Ruby classes distributed with WebORB and now deployed in your Rails application.

Creating Flex Project

Run Flex Builder 3 and select File -> New -> Flex Project.  The dialog window shown below will appear. Enter "SampleFlexToRubyProject" as the project name. Make sure to select "Other" as the Application server type. Make the selections as shown in the image below and click "Next >".

In the next step you need to point Flex Builder to a directory where it will compile the Flex application to. This tutorial uses the "sampleapp" in the "public" folder in the new rails application. You can enter any other directory under the "public" folder even if it does not exist at the time when the Flex Builder project is being created.

Use the "Browse..." button to select a directory or enter it manually as shown below and click "Next >":

Enter the URL corresponding to the output folder configured in the previous step. This guide used the following physical path:

C:\ruby\myrailsapp\public\sampleapp

The corresponding URL is:

http://localhost:3000/sampleapp

This URL is important for running and debugging the compiled Flex application directly from Flex Builder. Enter the output URL in the field at the bottom of the screen as shown below and click "Finish".

At this point Flex Builder has created the project and added a default, blank Application file. However, there is an additional configuration step and by far it is the most important one. 

Right click the project node in the "Flex Navigator" panel and select Properties. Select "Flex Compiler" and enter the following parameter into the "Additional compiler arguments" as shown in the image below:

-services c:\[RAILS-APP-PATH]\config\WEB-INF\flex\services-config.xml

Since the application from this guide is installed in c:\ruby\myrailsapp, the full path is:

-services c:\ruby\myrailsapp\config\WEB-INF\flex\services-config.xml

Enter the parameter as shown in the image below and click OK.

Finished Sample Flex Application

WebORB for Rails product distribution contains a finished Flex application demonstrating Flex to WebORB connectivity and a remoting invocation. Copy and paste the contents of the example.mxml file located at

     \public\examples\

into the mxml file created in Flex Builder. The code in the application connects to a Ruby object and retrieves some basic information about the computer where the object is running.

WebORB Configuration

Flex applications require a declaration of the exposed classes as "destinations". Destinations must be configured in remoting-config.xml located in the \config\WEB-INF\flex folder. The application in this example uses the "InfoService" destination defined as:

 <destination id="InfoService">
   <properties>
     <source>InfoService</source>
   </properties>
</destination>

Flex Builder loads the configuration file during the compile time. If you change the configuration file after the Flex application is compiled, make sure to use the Project > Clean feature of Flex Builder, since the configuration file may be cached.

Running Flex Application

When you run the application in Flex Builder, it opens a browser and loads the application from:

http://localhost:3000/examples/SampleFlexToRubyProject.html

The application connects to the backend service upon the startup or when user clicks the "Send Request" button:
 

Flex and Ruby Code Review

Flex application declares a remote object using the RemoteObject API:

remoteObject = new RemoteObject();
remoteObject.destination = "InfoService";
remoteObject.getComputerInfo.addEventListener("result", onResult);
remoteObject.addEventListener("fault", onFault);

Notice the destination name matches the destination entered in remoting-config.xml. When a user clicks the 'Get Computer Info' button, the following function executes a remote method invocation:

private function getInfo():void
{
  invokeButton.enabled = false;
  serverInfoText.text = "";
  requestIdText.text = "";
  osText.text = "";
  timeText.text = "";
  remoteObject.getComputerInfo("ABC123");
}

When an invocation response is available, Flex invokes a response handler specified in the tag. The response handler in the example, populates the text fields with the data available in the returned object:

private function onResult(event:ResultEvent):void
{
  var computerInfo:Object = event.result;
  serverInfoText.text = computerInfo.serverName;
  requestIdText.text = computerInfo.requestId;
  osText.text = computerInfo.os;
  timeText.text = computerInfo.currentTime.toString();
  invokeButton.enabled = true;
}

The source code for the server-side object is below:

require 'weborb/context'
require 'rbconfig'

class InfoService
  def getComputerInfo( requestId )
    computer_info = Hash.new
    request = RequestContext.get_request
    computer_info['serverName'] = request.server_software
    computer_info['requestId'] = requestId
    computer_info['os'] = Config::CONFIG["arch"].to_s
    computer_info['currentTime'] = Time.now
    computer_info
  end
end