RPC protocol code generator - ruby(rails), as3...
Ruby JavaScript
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
bin
examples/rails_app
lib
test
.document
.gitignore
LICENSE
README.rdoc
Rakefile
VERSION

README.rdoc

peto

peto is a RPC code generator. Defining types and procedures on RPC from contract files. Contract file is simple YAML format.

name: foo
types:
  human: [name:string, age:integer]
procedures:
  find_human:
    args: [id:integer]
    returns: [found:human]
    errors: [human not found, invalid id]

peto generates some codes.

  • human.rb(or human.as) : human's structure class

  • foo.rb(or foo.as) : procedure class. this has find_human, find_human_response, find_human_error_human_not_found, find_human_error_invalid_id

Use Foo class methods to create hash and send it as JSON string for RPC.

peto has a Rails helper module. Look examples/rails_app and use it, if you want to create Rails RPC server.

Usage

installing:

% gem install peto

contract file (foo.yml):

name: foo

types:
  animal: [name:string]
  user: [name:string, age:integer, animals:array:animal]

procedures:
  set_user:
    args: [user:user]

invoke command:

% peto foo.yml rb -o generated/

use foo.rb:

$: << "generated/"
require "foo"
require "animal"
require "user"

cat = Peto::Animal.new(:name => "cat")
dog = Peto::Animal.new(:name => "dog")
user = Peto::User.new(:name => "alice", :age => 23, :animals=>[cat, dog])

# generating procedure hash
Peto::Foo.set_user(user)

in Rails

setup

create your new application

% rails new myapp

add autoload_paths to config/application.rb

config.autoload_paths += %W(#{config.root}/generated)

add including to app/controller/application_controller.rb

require "peto/rails/rails_controller_helper"

class ApplicationController < ActionController::Base
  include Peto::RailsControllerHelper
  # ...
end

add to test/test_helper.rb

require "peto/rails/rails_controller_test_helper"

class ActionController::TestCase
  include Peto::RailsControllerTestHelper
end

add match to config/routes.rb

match ':controller(/:action)'

using

% # on your rails home
% create contract/foo.yml
% mkdir generated
% peto contract/foo.yml -o generated/

add subaction to app/controller/foo_controller.rb

# this is subaction
def set_user(args)
  respond(123, "foo bar baz")
end

subaction is an action method in imitation of rails controller. this has args to use likes params.

post procedure name and parameters to application, be called subaction. in test/functional/foo_controller_test.rb

test "set_user" do
  peto_post("set_user", Peto::User.new(:name=>"alice"))
  assert_response :success
  assert_peto_response("a"=>123, "b"=>"foo bar baz")
end

run server

% rails server

post next JSON to server/foo/

{
  "procedure" : "set_user",
  "args" : {
    "user" : {
      "name" : "alice"
    }
  }
}

this JSON string is created by

ActiveSupport::JSON.encode(Peto::Foo.set_user(user))

Note on Patches/Pull Requests

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don't break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright © 2010 Toshiyuki Hirooka. See LICENSE for details.