Skip to content

Commit

Permalink
Move everything Cramp::Controller::* to Cramp::*.
Browse files Browse the repository at this point in the history
Before :

  class Home < Cramp::Controller::Action
  end

After :

  class Home < Cramp::Action
  end

Similarly, Cramp::Controller::Websocket now becomes Cramp::Websocket.
  • Loading branch information
lifo committed Jul 17, 2010
1 parent 7d3f477 commit 968e903
Show file tree
Hide file tree
Showing 37 changed files with 420 additions and 443 deletions.
4 changes: 2 additions & 2 deletions examples/full.rb
Expand Up @@ -3,7 +3,7 @@

Bundler.setup(:default, :example)

require 'cramp/controller'
require 'cramp'
require 'tramp'
require 'usher'
require 'thin'
Expand All @@ -17,7 +17,7 @@ class User < Tramp::Base
validates_presence_of :name
end

class UsersController < Cramp::Controller::Action
class UsersController < Cramp::Action
before_start :verify_id, :find_user

def verify_id
Expand Down
4 changes: 2 additions & 2 deletions examples/full.ru
@@ -1,7 +1,7 @@
require File.join(File.dirname(__FILE__), "../vendor/gems/environment")
$: << File.join(File.dirname(__FILE__), "../lib")

require 'cramp/controller'
require 'cramp'
require 'cramp/model'

Cramp::Model.init(:username => 'root', :database => 'arel_development')
Expand All @@ -13,7 +13,7 @@ class User < Cramp::Model::Base
validates_presence_of :name
end

class UsersController < Cramp::Controller::Action
class UsersController < Cramp::Action
before_start :verify_id, :find_user

def verify_id
Expand Down
6 changes: 3 additions & 3 deletions examples/hello_websock.rb
Expand Up @@ -2,12 +2,12 @@
require "bundler"
Bundler.setup(:default, :example)

require 'cramp/controller'
require 'cramp'
require 'thin'

Cramp::Controller::Websocket.backend = :thin
Cramp::Websocket.backend = :thin

class WelcomeController < Cramp::Controller::Websocket
class WelcomeController < Cramp::Websocket
periodic_timer :send_hello_world, :every => 2
on_data :received_data

Expand Down
4 changes: 2 additions & 2 deletions examples/hello_world.rb
Expand Up @@ -2,10 +2,10 @@
require "bundler"
Bundler.setup(:default, :example)

require 'cramp/controller'
require 'cramp'
require 'thin'

class WelcomeController < Cramp::Controller::Action
class WelcomeController < Cramp::Action
def start
render "Hello World"
finish
Expand Down
4 changes: 2 additions & 2 deletions examples/hello_world.ru
Expand Up @@ -2,9 +2,9 @@ require "rubygems"
require "bundler"
Bundler.setup(:default, :example)

require 'cramp/controller'
require 'cramp'

class WelcomeController < Cramp::Controller::Action
class WelcomeController < Cramp::Action
def start
render "Hello World"
finish
Expand Down
6 changes: 3 additions & 3 deletions examples/rainsocket.ru
Expand Up @@ -2,11 +2,11 @@ require "rubygems"
require "bundler"
Bundler.setup(:default, :example)

require 'cramp/controller'
require 'cramp'

Cramp::Controller::Websocket.backend = :rainbows
Cramp::Websocket.backend = :rainbows

class WelcomeController < Cramp::Controller::Websocket
class WelcomeController < Cramp::Websocket
periodic_timer :send_hello_world, :every => 2
on_data :received_data

Expand Down
4 changes: 2 additions & 2 deletions examples/streaming.rb
Expand Up @@ -2,10 +2,10 @@
require "bundler"
Bundler.setup(:default, :example)

require 'cramp/controller'
require 'cramp'
require 'thin'

class StreamController < Cramp::Controller::Action
class StreamController < Cramp::Action
periodic_timer :send_data, :every => 1
periodic_timer :check_limit, :every => 2

Expand Down
4 changes: 2 additions & 2 deletions examples/with_usher.rb
Expand Up @@ -2,11 +2,11 @@
require "bundler"
Bundler.setup(:default, :example)

require 'cramp/controller'
require 'cramp'
require 'usher'
require 'thin'

class HomeController < Cramp::Controller::Action
class HomeController < Cramp::Action
def before_start
if params[:password] != 'foo'
halt 401, {}, "Bad Password"
Expand Down
11 changes: 11 additions & 0 deletions lib/cramp.rb
Expand Up @@ -11,8 +11,19 @@
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/buffered_logger'

require 'rack'

module Cramp
VERSION = '0.10'

mattr_accessor :logger

autoload :Action, "cramp/action"
autoload :Websocket, "cramp/websocket"
autoload :Body, "cramp/body"
autoload :PeriodicTimer, "cramp/periodic_timer"
autoload :KeepConnectionAlive, "cramp/keep_connection_alive"
autoload :Abstract, "cramp/abstract"
autoload :Callbacks, "cramp/callbacks"
autoload :TestCase, "cramp/test_case"
end
69 changes: 69 additions & 0 deletions lib/cramp/abstract.rb
@@ -0,0 +1,69 @@
require 'active_support/core_ext/hash/keys'

module Cramp
class Abstract

include Callbacks

ASYNC_RESPONSE = [-1, {}, []].freeze

class << self
def call(env)
controller = new(env).process
end
end

def initialize(env)
@env = env
end

def process
EM.next_tick { before_start }
ASYNC_RESPONSE
end

def continue
init_async_body

status, headers = respond_with
send_initial_response(status, headers, @body)

EM.next_tick { start } if respond_to?(:start)
EM.next_tick { on_start }
end

def respond_with
[200, {'Content-Type' => 'text/html'}]
end

def init_async_body
@body = Body.new

if self.class.on_finish_callbacks.any?
@body.callback { on_finish }
@body.errback { on_finish }
end
end

def finish
@body.succeed
end

def send_initial_response(response_status, response_headers, response_body)
EM.next_tick { @env['async.callback'].call [response_status, response_headers, response_body] }
end

def halt(status, headers = {}, halt_body = '')
send_initial_response(status, headers, halt_body)
end

def request
@request ||= Rack::Request.new(@env)
end

def params
@params ||= request.params.update(@env['usher.params']).symbolize_keys
end

end
end
12 changes: 12 additions & 0 deletions lib/cramp/action.rb
@@ -0,0 +1,12 @@
module Cramp
class Action < Abstract

include PeriodicTimer
include KeepConnectionAlive

def render(body)
@body.call(body)
end

end
end
48 changes: 48 additions & 0 deletions lib/cramp/body.rb
@@ -0,0 +1,48 @@
# Copyright 2008 James Tucker <raggi@rubyforge.org>.

module Cramp
class Body
include EventMachine::Deferrable

def initialize
@queue = []

# Make sure to flush out the queue before closing the connection
callback { flush }
end

def call(body)
@queue << body
schedule_dequeue
end

def each &blk
@body_callback = blk
schedule_dequeue
end

def closed?
@deferred_status != :unknown
end

def flush
return unless @body_callback

until @queue.empty?
Array(@queue.shift).each {|chunk| @body_callback.call(chunk) }
end
end

def schedule_dequeue
return unless @body_callback

EventMachine.next_tick do
next unless body = @queue.shift

Array(body).each {|chunk| @body_callback.call(chunk) }
schedule_dequeue unless @queue.empty?
end
end

end
end
48 changes: 48 additions & 0 deletions lib/cramp/callbacks.rb
@@ -0,0 +1,48 @@
module Cramp
module Callbacks

extend ActiveSupport::Concern

included do
class_inheritable_accessor :before_start_callbacks, :on_finish_callbacks, :on_start_callback, :instance_reader => false
self.before_start_callbacks = []
self.on_finish_callbacks = []
self.on_start_callback = []
end

module ClassMethods
def before_start(*methods)
self.before_start_callbacks += methods
end

def on_finish(*methods)
self.on_finish_callbacks += methods
end

def on_start(*methods)
self.on_start_callback += methods
end
end

def before_start(n = 0)
if callback = self.class.before_start_callbacks[n]
EM.next_tick { send(callback) { before_start(n+1) } }
else
continue
end
end

def on_start
self.class.on_start_callback.each do |callback|
EM.next_tick { send(callback) }
end
end

def on_finish
self.class.on_finish_callbacks.each do |callback|
EM.next_tick { send(callback) }
end
end

end
end
15 changes: 0 additions & 15 deletions lib/cramp/controller.rb

This file was deleted.

0 comments on commit 968e903

Please sign in to comment.