rpcoder

installation

$ gem install rpcoder

ruby code for AS3 RPC

#!/usr/bin/env ruby

require 'rpcoder'
require 'fileutils'

RPCoder.name_space = 'foo.bar.rpc'
RPCoder.api_class_name = 'RPC'

RPCoder.type "Mail" do |t|
  t.add_field :subject, :String
  t.add_field :body,    :String
end

RPCoder.function "getMail" do |f|
  f.path        = "/mails/:id" # => ("/mails/" + id)
  f.method      = "GET"
  f.add_return_type :mail, "Mail"
  f.add_param  :id, "int"
  f.description = 'メールを取得'
end

RPCoder.function "getMails" do |f|
  f.path        = "/mails"
  f.method      = "GET"
  f.add_return_type :mails, "Mail", {:array? => true}
  f.description = 'メールを送信'
end

RPCoder.function "sendMail" do |f|
  f.path        = "/mails/create"
  f.method      = "POST"
  f.add_param  :subject, "String"
  f.add_param  :body,    "String"
  f.description = 'メールを送信'
end

# output codes
dir = File.expand_path('src', File.dirname(__FILE__))
RPCoder.export(dir)

as3 code for using RPC

var rpc:RPC = new RPC('http://localhost:3000');

rpc.getMail(1, function(mail:Mail):void {
	// success
}, function():void {
	// failure
})

rpc.getMails(function(mails:Array):void {
	// success

for each (var mail as Mail in mails) { }

}, function():void {
	// failure
})

dummy RPC

Dummy RPC works standalone. You can set anything responses at callback.

var rpc:RPCInterface = new RPCDummy();
(rpc as RPCDummy).setDummySuccess("getMail", [
    Mail.create('foo', 'bar')
]);

rpc.getMail(function(mail:Mail):void {
    // success
    // responded mail as dummy
}, function():void {
    // error
});

// rpc.getMail() may callback as error.
(rpc as RPCDummy).setDummyError("getMail", true);