Skip to content

Commit 4c68313

Browse files
committed
Add Rainbows! examples
1 parent cd3582f commit 4c68313

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

examples/full.ru

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require File.join(File.dirname(__FILE__), "../vendor/gems/environment")
2+
$: << File.join(File.dirname(__FILE__), "../lib")
3+
4+
require 'cramp/controller'
5+
require 'cramp/model'
6+
7+
Cramp::Model.init(:username => 'root', :database => 'arel_development')
8+
9+
class User < Cramp::Model::Base
10+
attribute :id, :type => Integer, :primary_key => true
11+
attribute :name
12+
13+
validates_presence_of :name
14+
end
15+
16+
class UsersController < Cramp::Controller::Action
17+
before_start :verify_id, :find_user
18+
19+
def verify_id
20+
if params[:id].nil? || params[:id] !~ /\d+/
21+
halt 500, {}, "Bad Request"
22+
else
23+
yield
24+
end
25+
end
26+
27+
def find_user
28+
User.where(User[:id].eq(params[:id])).first do |user|
29+
if @user = user
30+
yield
31+
else
32+
halt 404, {}, "User not found"
33+
end
34+
end
35+
end
36+
37+
# Sends a space ( ' ' ) to the client for keeping the connection alive. Default : Every 15 seconds
38+
keep_connection_alive :every => 1
39+
40+
# Polls every 1 second by default
41+
periodic_timer :poll_user
42+
43+
on_start :start_benchmark
44+
on_finish :stop_benchmark
45+
46+
def poll_user
47+
User.where(User[:id].eq(@user.id)).first _(:on_user_find)
48+
end
49+
50+
def on_user_find(user)
51+
if @user.name != user.name
52+
render "User's name changed from #{@user.name} to #{user.name}"
53+
finish
54+
end
55+
end
56+
57+
def start_benchmark
58+
@time = Time.now
59+
end
60+
61+
def stop_benchmark
62+
puts "It took #{Time.now - @time} seconds"
63+
end
64+
end
65+
66+
routes = Usher::Interface.for(:rack) do
67+
add('/users/:id').to(UsersController)
68+
end
69+
70+
# rainbows -E deployment -c rainbows.conf full.ru
71+
run routes

examples/hello_world.ru

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require File.join(File.dirname(__FILE__), "../vendor/gems/environment")
2+
$: << File.join(File.dirname(__FILE__), "../lib")
3+
4+
require 'cramp/controller'
5+
6+
class WelcomeController < Cramp::Controller::Action
7+
def start
8+
render "Hello World"
9+
finish
10+
end
11+
end
12+
13+
# rainbows -E deployment -c rainbows.conf hello_world.ru
14+
run WelcomeController

examples/rainbows.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Rainbows! do
2+
use :EventMachine
3+
worker_connections 1024
4+
end

0 commit comments

Comments
 (0)