Permalink
Browse files

Move everything Cramp::Controller::* to Cramp::*.

Before :

  class Home < Cramp::Controller::Action
  end

After :

  class Home < Cramp::Action
  end

Similarly, Cramp::Controller::Websocket now becomes Cramp::Websocket.
  • Loading branch information...
1 parent 7d3f477 commit 968e9030f253f74c94562555f3244aad4fe03e89 @lifo committed Jul 17, 2010
View
@@ -3,7 +3,7 @@
Bundler.setup(:default, :example)
-require 'cramp/controller'
+require 'cramp'
require 'tramp'
require 'usher'
require 'thin'
@@ -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
View
@@ -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')
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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"
View
@@ -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
View
@@ -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
View
@@ -0,0 +1,12 @@
+module Cramp
+ class Action < Abstract
+
+ include PeriodicTimer
+ include KeepConnectionAlive
+
+ def render(body)
+ @body.call(body)
+ end
+
+ end
+end
View
@@ -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
@@ -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
@@ -1,15 +0,0 @@
-require 'cramp'
-require 'rack'
-
-module Cramp
- module Controller
- autoload :Action, "cramp/controller/action"
- autoload :Websocket, "cramp/controller/websocket"
- autoload :Body, "cramp/controller/body"
- autoload :PeriodicTimer, "cramp/controller/periodic_timer"
- autoload :KeepConnectionAlive, "cramp/controller/keep_connection_alive"
- autoload :Abstract, "cramp/controller/abstract"
- autoload :Callbacks, "cramp/controller/callbacks"
- autoload :TestCase, "cramp/controller/test_case"
- end
-end
Oops, something went wrong.

0 comments on commit 968e903

Please sign in to comment.