socket is now a controller
This commit is contained in:
parent
58ed75a289
commit
6293f9f585
7 changed files with 26 additions and 40 deletions
|
|
@ -5,7 +5,6 @@ class DashboardController < ApplicationController
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@posts = Post.paginate :page => params[:page], :order => 'created_at DESC'
|
@posts = Post.paginate :page => params[:page], :order => 'created_at DESC'
|
||||||
puts session.inspect
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,13 +19,8 @@ class SocketController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def outgoing(object)
|
def outgoing(object)
|
||||||
begin
|
@_request = ActionDispatch::Request.new({})
|
||||||
@_request = ActionDispatch::Request.new(:socket => true)
|
|
||||||
WebSocket.push_to_clients(action_hash(object))
|
WebSocket.push_to_clients(action_hash(object))
|
||||||
rescue Exception => e
|
|
||||||
puts e.inspect
|
|
||||||
raise e
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_subscriber(sid)
|
def delete_subscriber(sid)
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,23 @@
|
||||||
module SocketHelper
|
module SocketHelper
|
||||||
include ApplicationHelper
|
include ApplicationHelper
|
||||||
def obj_id(object)
|
|
||||||
|
def obj_id(object)
|
||||||
(object.is_a? Post) ? object.id : object.post_id
|
(object.is_a? Post) ? object.id : object.post_id
|
||||||
end
|
end
|
||||||
|
|
||||||
def url_options
|
def url_options
|
||||||
{:host => "", :only_path => true}
|
{:host => ""}
|
||||||
end
|
end
|
||||||
|
|
||||||
def action_hash(object)
|
def action_hash(object)
|
||||||
|
|
||||||
begin
|
begin
|
||||||
v = render_to_string(:partial => type_partial(object), :locals => {:post => object}) unless object.is_a? Retraction
|
v = render_to_string(:partial => type_partial(object), :locals => {:post => object}) unless object.is_a? Retraction
|
||||||
|
|
||||||
|
|
||||||
rescue Exception => e
|
rescue Exception => e
|
||||||
puts "web socket view rendering failed for some reason." + v.inspect
|
puts "web socket view rendering failed for some reason." + v.inspect
|
||||||
puts object.inspect
|
puts object.inspect
|
||||||
puts e.message
|
puts e.message
|
||||||
raise e
|
raise e
|
||||||
end
|
end
|
||||||
|
|
||||||
{:class =>object.class.to_s.underscore.pluralize, :html => v, :post_id => obj_id(object)}.to_json
|
{:class =>object.class.to_s.underscore.pluralize, :html => v, :post_id => obj_id(object)}.to_json
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,6 @@ module WebSocket
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.push_to_clients(html)
|
def self.push_to_clients(html)
|
||||||
puts html
|
|
||||||
@channel.push(html)
|
@channel.push(html)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -37,7 +36,7 @@ module WebSocket
|
||||||
|
|
||||||
|
|
||||||
def self.subscribe
|
def self.subscribe
|
||||||
@channel.subscribe{ |msg| puts @ws.inspect; puts "ehllo" ; @ws.send msg }
|
@channel.subscribe{ |msg| @ws.send msg }
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,18 @@
|
||||||
require File.dirname(__FILE__) + '/../spec_helper'
|
require File.dirname(__FILE__) + '/../spec_helper'
|
||||||
|
|
||||||
#require 'em-spec/rspec'
|
describe 'SocketController' do
|
||||||
describe SocketController do
|
render_views
|
||||||
render_views
|
|
||||||
|
|
||||||
before do
|
before do
|
||||||
@user = Factory.create(:user)
|
@user = Factory.create(:user)
|
||||||
WebSocket.unstub!(:push_to_clients)
|
SocketController.unstub!(:new)
|
||||||
WebSocket.unstub!(:unsubscribe)
|
|
||||||
WebSocket.unstub!(:subscribe)
|
|
||||||
#EventMachine::WebSocket.stub!(:start)
|
#EventMachine::WebSocket.stub!(:start)
|
||||||
@controller = SocketController.new
|
@controller = SocketController.new
|
||||||
|
stub_socket_controller
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should unstub the websocket' do
|
it 'should unstub the websocket' do
|
||||||
WebSocket.initialize_channel
|
WebSocket.initialize_channel
|
||||||
WebSocket.push_to_clients("what").should_not == "stub"
|
@controller.class.should == SocketController
|
||||||
WebSocket.unsubscribe(1).should_not == "stub"
|
|
||||||
WebSocket.subscribe.should_not == "stub"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should add a new subscriber to the websocket channel' do
|
it 'should add a new subscriber to the websocket channel' do
|
||||||
|
|
@ -30,16 +25,16 @@ describe SocketController do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should actionhash posts' do
|
it 'should actionhash posts' do
|
||||||
hash = @controller.action_hash(@message)
|
json = @controller.action_hash(@message)
|
||||||
hash[:html].include?(@message.message).should be_true
|
json.include?(@message.message).should be_true
|
||||||
hash[:class].include?('status_message').should be_true
|
json.include?('status_message').should be_true
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should actionhash retractions' do
|
it 'should actionhash retractions' do
|
||||||
retraction = Retraction.for @message
|
retraction = Retraction.for @message
|
||||||
hash = @controller.action_hash(retraction)
|
json = @controller.action_hash(retraction)
|
||||||
hash[:class].include?('retraction').should be_true
|
json.include?('retraction').should be_true
|
||||||
hash[:html].should be_nil
|
json.include?("html\":null").should be_true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ describe MessageHandler do
|
||||||
@handler = MessageHandler.new
|
@handler = MessageHandler.new
|
||||||
@message_body = "I want to pump you up"
|
@message_body = "I want to pump you up"
|
||||||
@message_urls = ["http://www.google.com/", "http://yahoo.com/", "http://foo.com/"]
|
@message_urls = ["http://www.google.com/", "http://yahoo.com/", "http://foo.com/"]
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'GET messages' do
|
describe 'GET messages' do
|
||||||
|
|
|
||||||
|
|
@ -24,15 +24,7 @@ RSpec.configure do |config|
|
||||||
|
|
||||||
config.before(:each) do
|
config.before(:each) do
|
||||||
DatabaseCleaner.start
|
DatabaseCleaner.start
|
||||||
SocketController.stub!(:incoming).and_return(true)
|
stub_socket_controller
|
||||||
SocketController.stub!(:new_subscriber).and_return(true)
|
|
||||||
SocketController.stub!(:outgoing).and_return(true)
|
|
||||||
SocketController.stub!(:delete_subscriber).and_return(true)
|
|
||||||
|
|
||||||
|
|
||||||
WebSocket.stub!(:push_to_clients).and_return("stub")
|
|
||||||
WebSocket.stub!(:unsubscribe).and_return("stub")
|
|
||||||
WebSocket.stub!(:subscribe).and_return("stub")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
config.after(:each) do
|
config.after(:each) do
|
||||||
|
|
@ -40,3 +32,11 @@ RSpec.configure do |config|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
def stub_socket_controller
|
||||||
|
mock_socket_controller = mock('socket mock')
|
||||||
|
mock_socket_controller.stub!(:incoming).and_return(true)
|
||||||
|
mock_socket_controller.stub!(:new_subscriber).and_return(true)
|
||||||
|
mock_socket_controller.stub!(:outgoing).and_return(true)
|
||||||
|
mock_socket_controller.stub!(:delete_subscriber).and_return(true)
|
||||||
|
SocketController.stub!(:new).and_return(mock_socket_controller)
|
||||||
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue