From cc603a79439dc035d2413ccc75fce0c886ec4936 Mon Sep 17 00:00:00 2001 From: maxwell Date: Wed, 7 Jul 2010 15:27:44 -0700 Subject: [PATCH] autotest for rafi --- app/controllers/socket_controller.rb | 43 ++++++++++++++++++++++ app/helpers/socket_helper.rb | 22 +++++++++++ spec/controllers/socket_controller_spec.rb | 36 ++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 app/controllers/socket_controller.rb create mode 100644 app/helpers/socket_helper.rb create mode 100644 spec/controllers/socket_controller_spec.rb diff --git a/app/controllers/socket_controller.rb b/app/controllers/socket_controller.rb new file mode 100644 index 000000000..65f9518a0 --- /dev/null +++ b/app/controllers/socket_controller.rb @@ -0,0 +1,43 @@ +class SocketController < ApplicationController + + def incoming(msg) + puts msg + end + + + def new_subscriber + WebSocket.subscribe + end + + + + def outgoing(object) + puts "made it sucka" + WebSocket.push_to_clients(action_hash(object)) + end + + + def delete_subscriber(sid) + WebSocket.unsubscribe(sid) + end + + +# need a data strucutre to keep track of who is where + +#the way this is set up now, we have users on pages + +#could have... a channel for every page/collection...not that cool +#or, have a single channel, which has a corresponding :current page => [sid] +# can i cherry pick subscribers from a a channel? + + +# we want all sorts of stuff that comes with being a controller +# like, protect from forgery, view rendering, etc + + +#these functions are not really routes +#so the question is, whats the best way to call them? + +#also, this is an input output controller + +end diff --git a/app/helpers/socket_helper.rb b/app/helpers/socket_helper.rb new file mode 100644 index 000000000..485b053b9 --- /dev/null +++ b/app/helpers/socket_helper.rb @@ -0,0 +1,22 @@ +module SocketHelper + + def obj_id(object) + object.is_a? Post ? object.id : object.post_id + end + + + def action_hash(object) + begin + v = render_to_string(type_partial(object), :post => object) unless object.is_a? Retraction + + rescue Exception => e + puts "in failzord " + v.inspect + puts object.inspect + puts e.message + raise e + end + + {:class =>object.class.to_s.underscore.pluralize, :html => v, :post_id => obj_id(object)} + end + +end \ No newline at end of file diff --git a/spec/controllers/socket_controller_spec.rb b/spec/controllers/socket_controller_spec.rb new file mode 100644 index 000000000..b2c8e723a --- /dev/null +++ b/spec/controllers/socket_controller_spec.rb @@ -0,0 +1,36 @@ +require File.dirname(__FILE__) + '/../spec_helper' + +describe SocketController do + before do + Factory.create(:user) + WebSocket.unstub!(:push_to_clients) + WebSocket.unstub!(:unsubscribe) + WebSocket.unstub!(:subscribe) + #EventMachine::WebSocket.stub!(:start) + @controller = SocketController.new + end + + it 'should unstub the websocket' do + EventMachine.run { + puts"hi" + WebSocket.initialize_channel + WebSocket.push_to_clients("what").should_not == "stub" + WebSocket.unsubscribe(1).should_not == "stub" + WebSocket.subscribe.should_not == "stub" + EventMachine.stop + } + puts "yo" + end + + it 'should add a new subscriber to the websocket channel' do + EventMachine.run { + puts "foo" + WebSocket.initialize_channel + @controller.new_subscriber.should == 1 + + EventMachine.stop + } + end + + +end