Apr 29 2007

Extending Apollo with Ruby using RubyAMF

Tag: Apollo, Flex, Ruby, RubyAMFDerek Wischusen @ 3:08 am

At the end of my last post about RubyAMF I mentioned that I was particularly interested in how it could be used to extend Apollo applications using Ruby in the same way that Artemis makes it possible to extend Apollo with Java.

Here’s why:

  • It enables AMF3 communication between Flex and Ruby.
  • It comes with its own webrick servlet, so deploying RubyAMF is dead simple (if you have Ruby installed).
  • It’s small: less than 300k. So it’s a quick download.

Here’s an example:

I put together a very simple demo app that uses RubyAMF to extend Apollo. I am calling the app Clairify (because everything needs a name). Clairify allows you to take a screenshot of whatever is underneath it, mark it up with some simple drawing tools and text boxes, and then save it to disk. The concept behind Clairify is that you could use it to quickly provide feedback on an application’s UI.

It would take several steps and downloads for many people to run the app, so I’ve put together a short video.

Here is a quick overview of what is going on in the video:

  1. I launch Clairify.
  2. When Clairify launches all you see are the controls. The rest of the app is transparent.
  3. I then bring up the interface that I want to capture (the login screen of my simple issue tracker).
  4. I click the capture button. This calls code in Ruby (see below) that captures the screen and saves the image to a PNG that Apollo loads.
  5. I add some comments to screen.
  6. I click save, which uses Apollo’s File API to save the marked up image to my desktop as PNG.
  7. I open up the PNG in Fireworks.

Click here to view the video.

If you want to take a look at the Apollo source code, click here. (If you really want to know how to run this on your own machine let me know and I will put together some instructions).

And, here is the Ruby code:

require 'win32screenshot'
require 'RMagick'
include Magick

class CaptureService
  def screenCapture
    width, height, bitmap = Win32::Screenshot.desktop
    img_lst = ImageList.new
    img_lst.from_blob(bitmap)
    img_lst.write('public/screen.png')
    true
  end
end

Credits:
Clairify is based on the ScreenPlay app that is avalable on Adobe Labs.


Apr 29 2007

RubyAMF – an open source AMF gateway for Flex/Flash remoting with Ruby

Tag: Flex, Rails, RubyAMF, weborbDerek Wischusen @ 3:08 am

RubyAMF is an open source Ruby application being developed by Aaron Smith that enables AMF0/3 communication between Flex/Flash and Ruby.

So, you might be asking yourself, how is this different from WebORB for Rails? Well, the main difference is in how the apps are architected. WebORB for Rails is built as Rails plugin. This makes it really easy to use WebORB for Rails with your Ruby on Rails applications. However, this means you need to use Rails if you want to use WebORB for Rails, even if you want to use it for an application that does require any Rails specific code.

RubyAMF, on the other hand, is designed to be integrated directly with a server and, as such, does not require Rails. It even comes with its own webrick servlet, which means that all you have to do to start RubyAMF is download it, point a command line to the root folder, and enter ruby server/start. This makes it dead simple to use RubyAMF to integrate Flex/Flash with a Ruby application. However, if you want to use RubyAMF with Rails then you are going to have to do a little extra work.

So, for right now my simple heuristic is that if you want to use AMF remoting with Rails, then use WebORB for Rails. If you want AMF remoting with just Ruby, then use RubyAMF. (I suppose the names make that kind of obvious).

That said, I think it would be relatively trivial to rearchitect either app to be like the other, but I think it is good that they are different. Choice is, generally, a good thing.

Right now the thing that has me most excited about RubyAMF is how it could potentially be used to extend Apollo using Ruby in much the same way that Artemis does using Java. More on this in my next post….


Apr 14 2007

The [Transient] and [Mixin] metadata tags

Tag: FlexDerek Wischusen @ 4:02 am

There are a couple useful metadata tags that are not particularly well documented in the Flex Livedocs that I have come across recently.

[Mixin]

The first is the [Mixin] metadata tag, which I saw a while back while reading about the RandomWalk component in the Flex docs for Creating Applications for Testing, but I never quite understood what it did until I came across this post by Adam Flater.

So, here is what it does: when you put the [Mixin] metadata just above your class definition and add a static init function to the class like so,

[Mixin]
public class Model
{
  public static function init (systemManager : ISystemManager)
  {
    trace ("I get called first")
  }
}

the static init function will be called as soon as your application loads (assuming this class is referenced somewhere in the app), much like static initialization blocks in Java, or static constructors in C#. This is useful if you have some code that you want to run before any of the other code in the class.

[Transient]

I just learned about this tag today from a comment by one the readers of this blog (thanks David).

The [Transient] tag is extremely useful for working with Value Objects because it allows you to tag properties that you do not want to send across the wire when the object is serialized.

For example,

package com.rxr.issuetracker.vo
{
  import com.adobe.cairngorm.vo.IValueObject;
  import mx.collections.ArrayCollection;
  [Bindable]
  [RemoteClass(alias="com.rxr.issuetracker.vo.Project")]
  public class ProjectVO implements IValueObject
  {
    private var _issues : ArrayCollection =  new ArrayCollection();

    public var id : int;

    public var user_id : int;

    public var title : String;

    [Transient]
    public function get issues () : ArrayCollection
    {
       return _issues;
    }

    public function set issues ( value : Array ) : void
    {
      _issues= new ArrayCollection(value);
    }
  }
}

Now when you send this ProjectVO across the wire to Rails (or PHP, ColdFusion, Java, .NET, etc) the issues ArrayCollection will not be sent with it.

As always, let me know if you have any questions or if any of this is unclear or incorrect. Also, please let me know if you know about any other useful metadata tags that are not included in the list on livedocs.