Mar 26 2008

RubyIZUMI – Open Source RTMP Server(MP4/H.264) for streaming Flash video/audio

Tag: RTMP, RubyDerek Wischusen @ 3:04 am

Here is yet another exciting Flash-related Ruby project. Takuma Mori at Vixy.tv recently released RubyIZUMI, an open source streaming Flash server that can stream H.264 encoded videos over RTMP that is written in pure Ruby.

You can check out a demo of the server in action here.


Mar 24 2008

HotRuby – Ruby for Flash and JavaScript

Tag: Flex, Ruby, hotruby, iron monkeyDerek Wischusen @ 2:19 am

It looks like there is another project, in addition to IronMonkey (which I wrote about in a previous post), that is looking to bring Ruby support to Flash/Flex (and JavaScript).

HotRuby is a Ruby VM, which is written in JavaScript, that runs opcode that is compiled by YARV (the VM that is used in Ruby 1.9). 

Using HotRuby it is possible to use straight Ruby to build a Flash app.  Here are a couple Flash examples from the HotRuby website:

You can grab the source files for HotRuby from the Google Project site.


Jan 24 2008

We need a package manager like RubyGems for distributing Flex components – Revisited

Tag: AS3, ActionScript, Flex, Ruby, maven, package manager, sproutsDerek Wischusen @ 6:31 am

About a year ago I wrote a post about how the Flex/As3 community needs a package manager like RubyGems for managing libraries and components.

Well, I have some good news. Recently I’ve learned about two potential solutions to this problem.

The first I learned about from a comment on the original post by Luke Bayes. Here is an excerpt:

I’ve been working on ’sprouts’ for the past year and finally landed on an architecture that actually sits on top of Rubygems for package management. Even though we have a functional pre-alpha in the wild right now, we expect to release a production-ready build before the end of January 2008 that will really support versioning via Rubygems the repository.

Check it out: http://www.projectsprouts.org

The second potential solution is a Flex plugin for Maven. It appears that it is still under development, but it looks promising. You can check it out here.


Sep 19 2007

FunFX – A Ruby based automated testing framework for Flex

Tag: Flex, Ruby, TDD, TestingDerek Wischusen @ 4:31 pm

During the development of a large-scale (or even a medium-scale) Flex application it is very likely that you will quickly reach a point where manually testing your interface becomes impractical. This is where Flex’s automation framework comes into play. It makes it possible to simulate user level interactions in your Flex interfaces. So, you can write a script that performs a series of interactions (e.g., click this button, fill out this form, click here, click there, and so on) and then automatically run that script, and any others, with the rest of your tests. Pretty nice, right? Well, there is one potential problem; until recently there was only one existing solution that worked with Flex’s automation framework, and it is pretty expensive. Fortunately, there is now another option.

FunFX is a free, open source framework, developed by Peter Motzfeldt, that makes it possible to take advantage of Flex’s automation framework using test scripts written in Ruby. It is still relatively early in its development, but as you can see in this tutorial video, it already has some pretty impressive functionality. FunFX is packaged as a Ruby gem. I was unable to install it via the normal gem install route, so for now it looks like you’ll have to download it from here and do a local install.

CREDITS:

I came across this gem via Aslak Hellesoy’s blog (who, incidentally, just gave a well received tutorial on using Flex and Rails at RailsConf Europe).


Aug 07 2007

method_missing in ActionScript 3/Flex

Tag: AS3, ActionScript, Flex, RubyDerek Wischusen @ 2:05 am

method_missing is one of the small bits of Ruby magic that can be used to some really amazing things when used properly, and some really dangerous things when used improperly. Rails dynamic finders are good example of the amazing things that you can do. Here are some others.

It is possible to implement something like method_missing in AS3 using the Proxy class. Here is a relatively simplistic example:

import flash.utils.Proxy;
import flash.utils.flash_proxy;
 
dynamic public class BaseProxy extends Proxy
{     
	flash_proxy override function callProperty(method: *, ...args): * {
	   try { 		 
	   var clazz : Class = getDefinitionByName(getQualifiedClassName(this)) as Class;
	    return clazz.prototype[method].apply(method, args);
	   }
	   catch (e : Error) {
		return methodMissing (method, args);
	   }
 
	}
 
	protected function methodMissing(method : *, args : Array) : Object{
		throw( new Error("Method Missing"));
	}
}

The callProperty method is called whenever an undefined method is called on an instance of this class, or any instance of class that extends this class. In the try block we check if the method was defined on the prototype. If it is not found there, we call methodMissing.

So, now if we create another class that extends this one like so

public dynamic class Model extends BaseProxy
{
	public function myMethod (arg1 : String, arg2 : Boolean) : String {
		return arg1 + " " + arg2;
	}
}

and then run the following trace statements

import flash.display.Sprite;
 
public class MethodMissingExample extends Sprite
{
	public function MethodMissingExample()
	{
		var m : Model =  new Model();
 
		Model.prototype.runtimeMethod = function (date : Date) : String {
			return "I was defined at runtime at " + date.toLocaleTimeString();
		};
 
		trace(m.myMethod("I exist", true));
                trace(m.runtimeMethod(new Date()));
		trace(m.someMethod(0, false, "x"));
	}
}

You will see the following in the console:

I exist true
I was defined at runtime at 09:58:00 PM
Error: Method Missing

The first call succeeds because myMethod is defined. someMethod is not defined, so a Method Missing error gets thrown.

Now, if we override the missingMethod in the Model class like so,

public dynamic class Model extends BaseProxy
{
	public function myMethod (arg1 : String, arg2 : Boolean) : String {
		return arg1 + " " + arg2;
	}
 
	override protected function methodMissing(method : *, args : Array) : Object {
		return "You called " + method + " with " + args.toString();
	}
}

and run the same trace statements you will see the following in the console:

I exist true
I was defined at runtime at 09:58:00 PM
You called someMethod with 0,false,x

Jun 13 2007

Ruby-like Mixins in ActionScript 3

Tag: AS3, RubyDerek Wischusen @ 2:26 am

That title may be a bit of a stretch, AS3 does not support mixins in the same way as Ruby, but it is possible to define a set of methods and/or attributes in a single file and then mix those methods and attributes into your AS3 classes. Here’s how:

First create a new ActionScript file (a file, not a class) then add the methods and/or attributes that you want to mix in to your classes.

For example, I created a file called classmixin.as that looks like this:

// ActionScript file 
 
	import flash.utils.*; 
 
        // returns the name of the class 
	public function whoAmI () : String 
	{ 
		return getQualifiedClassName(this); 
	} 
 
        // returns the class object 
	public function myClass () : Class 
	{ 
		return getDefinitionByName(whoAmI()) as Class; 
	} 
 
        // returns the prototype object 
	public function proto () : Object 
	{ 
		return myClass().prototype; 
	}

Now, let’s say that you want to add these methods directly to two classes, one is controller called ContactsController and one is a model called Contact. To do this you need to use the include statement along with the relative path to file that defines the methods. For example:

package com.rxr.mixinexample.control 
{ 
	public class ContactsController 
	{ 
		include "/../classmixin/classmixin.as" 
	} 
}
package com.rxr.mixinexample.model 
{ 
	public class Contact 
	{ 
		include "/../classmixin/classmixin.as" 
	} 
}

That’s it. Now both of these classes have all of the methods defined in classmixin.as, so if you were to run the following code:

package { 
	import flash.display.Sprite; 
	import com.rxr.mixinexample.control.ContactsController; 
	import com.rxr.mixinexample.model.Contact; 
 
	public class MixinExample extends Sprite 
	{ 
		public function MixinExample() 
		{ 
			var a : ContactsController = new ContactsController(); 
			var b : Contact = new Contact(); 
 
			trace(a.whoAmI()); 
			trace(b.whoAmI()); 
 
		} 
	} 
}

you would see these trace statements in the console:

com.rxr.mixinexample.control::ContactsController
com.rxr.mixinexample.model::Contact

This mixin capability in AS3 works a bit differently than mixins in Ruby. In Ruby when you mixin a module you are really creating a reference to that module and when you call a method that was mixed in it is actually being delegated to the module. Also, in Ruby when you mixin in a module only the member methods get mixed in, though there is way to mixin statics as well, but you have to add some extra code to make this happen. This is because the module is an object itself and the static methods belong to the module, so if you want to add them to your class you need to extend the module.

In AS3 when you include a .as file, it is though you are appending that file directly to the class definition. So you are not creating a reference to a module, you are just adding the contents of the file to your class, so if you define static attributes or methods they will be added to your class along with the member attributes and methods.

Please let me know if you have any questions, or if any of this is unclear.

You can grab the source files for this example here.


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.