Getting Started with WebORB for Ruby on Rails
Search:
 

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.

GETTING STARTED - 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.
 

GETTING STARTED - CREATING FLEX APPLICATION

Start Flex Builder 2.0 and select File -> New -> Flex Project. A dialog window shown below will appear. Make the selections as shown below and click "Next >".

The next step is very important as it establishes the configuration paths. Uncheck the 'Use default location..' checkbox. The 'Root folder' field must contain the path to the config folder of your Rails application. The 'Root URL' field must point to the WebORB controller deployed into the Rails application.

 Click "Next >" to continue.

The next step is to assign a name to the Flex project. Enter  "SampleFlexToRubyProject" for the project the name as shown below and click 'Next' to continue.

The final step in Flex project creation is to set the output folder path and output folder url. Both of these values must point to the /public folder of the Rails application (or a subfolder, but the location must be the same for both).

The 'Output folder' field is the second from the bottom of the wizard window. Click 'Browse...' and select the /public/examples folder in your Rails application as shown below. The 'Output folder URL' field is a web-based URL corresponding to the folder selected above. Enter 'http://localhost:3000/examples' as it maps to the /public/examples folder.

Click 'Finish' to finalize project construction.

Flex Builder creates an empty Flex application. The steps below will guide through building a Flex application and connecting it with a Ruby object.
 

CONFIGURATION - FLEX BUILDER

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.
 

CONFIGURATION - WEBORB

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.
 

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:
 

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 <RemoteObject> 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

 

 
Copyright © 2003-2006 Midnight Coders, LLC.  Privacy Policy | Contact Webmaster
Home | Products | Customers | Download | Licensing | Forum | Developers | About