autotest for rafi
This commit is contained in:
parent
5350b40e3c
commit
cc603a7943
3 changed files with 101 additions and 0 deletions
43
app/controllers/socket_controller.rb
Normal file
43
app/controllers/socket_controller.rb
Normal file
|
|
@ -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
|
||||||
22
app/helpers/socket_helper.rb
Normal file
22
app/helpers/socket_helper.rb
Normal file
|
|
@ -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
|
||||||
36
spec/controllers/socket_controller_spec.rb
Normal file
36
spec/controllers/socket_controller_spec.rb
Normal file
|
|
@ -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
|
||||||
Loading…
Reference in a new issue