Permalink
Browse files

Add Rainbows! examples

  • Loading branch information...
1 parent cd3582f commit 4c68313f107fbc2ba9372aff241d7d2097d9bf00 @lifo committed Jan 9, 2010
Showing with 89 additions and 0 deletions.
  1. +71 −0 examples/full.ru
  2. +14 −0 examples/hello_world.ru
  3. +4 −0 examples/rainbows.conf
View
@@ -0,0 +1,71 @@
+require File.join(File.dirname(__FILE__), "../vendor/gems/environment")
+$: << File.join(File.dirname(__FILE__), "../lib")
+
+require 'cramp/controller'
+require 'cramp/model'
+
+Cramp::Model.init(:username => 'root', :database => 'arel_development')
+
+class User < Cramp::Model::Base
+ attribute :id, :type => Integer, :primary_key => true
+ attribute :name
+
+ validates_presence_of :name
+end
+
+class UsersController < Cramp::Controller::Action
+ before_start :verify_id, :find_user
+
+ def verify_id
+ if params[:id].nil? || params[:id] !~ /\d+/
+ halt 500, {}, "Bad Request"
+ else
+ yield
+ end
+ end
+
+ def find_user
+ User.where(User[:id].eq(params[:id])).first do |user|
+ if @user = user
+ yield
+ else
+ halt 404, {}, "User not found"
+ end
+ end
+ end
+
+ # Sends a space ( ' ' ) to the client for keeping the connection alive. Default : Every 15 seconds
+ keep_connection_alive :every => 1
+
+ # Polls every 1 second by default
+ periodic_timer :poll_user
+
+ on_start :start_benchmark
+ on_finish :stop_benchmark
+
+ def poll_user
+ User.where(User[:id].eq(@user.id)).first _(:on_user_find)
+ end
+
+ def on_user_find(user)
+ if @user.name != user.name
+ render "User's name changed from #{@user.name} to #{user.name}"
+ finish
+ end
+ end
+
+ def start_benchmark
+ @time = Time.now
+ end
+
+ def stop_benchmark
+ puts "It took #{Time.now - @time} seconds"
+ end
+end
+
+routes = Usher::Interface.for(:rack) do
+ add('/users/:id').to(UsersController)
+end
+
+# rainbows -E deployment -c rainbows.conf full.ru
+run routes
@@ -0,0 +1,14 @@
+require File.join(File.dirname(__FILE__), "../vendor/gems/environment")
+$: << File.join(File.dirname(__FILE__), "../lib")
+
+require 'cramp/controller'
+
+class WelcomeController < Cramp::Controller::Action
+ def start
+ render "Hello World"
+ finish
+ end
+end
+
+# rainbows -E deployment -c rainbows.conf hello_world.ru
+run WelcomeController
@@ -0,0 +1,4 @@
+Rainbows! do
+ use :EventMachine
+ worker_connections 1024
+end

0 comments on commit 4c68313

Please sign in to comment.