From 6293f9f5855f1363693e82d0d91c119e2cf9f56c Mon Sep 17 00:00:00 2001
From: maxwell <maxwell@joindiaspora.com>
Date: Thu, 8 Jul 2010 10:14:17 -0700
Subject: [PATCH] socket is now a controller

---
 app/controllers/dashboard_controller.rb    |  1 -
 app/controllers/socket_controller.rb       |  7 +-----
 app/helpers/socket_helper.rb               |  9 +++-----
 config/initializers/socket.rb              |  3 +--
 spec/controllers/socket_controller_spec.rb | 27 +++++++++-------------
 spec/lib/message_handler_spec.rb           |  1 +
 spec/spec_helper.rb                        | 18 +++++++--------
 7 files changed, 26 insertions(+), 40 deletions(-)

diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb
index ce821988d5..e0aa18da09 100644
--- a/app/controllers/dashboard_controller.rb
+++ b/app/controllers/dashboard_controller.rb
@@ -5,7 +5,6 @@ class DashboardController < ApplicationController
 
   def index
     @posts = Post.paginate :page => params[:page], :order => 'created_at DESC'
-    puts session.inspect
   end
 
 
diff --git a/app/controllers/socket_controller.rb b/app/controllers/socket_controller.rb
index c3c2df8f94..6046697b73 100644
--- a/app/controllers/socket_controller.rb
+++ b/app/controllers/socket_controller.rb
@@ -19,13 +19,8 @@ class SocketController < ApplicationController
   end
   
   def outgoing(object)
-    begin 
-    @_request = ActionDispatch::Request.new(:socket => true)
+    @_request = ActionDispatch::Request.new({})
     WebSocket.push_to_clients(action_hash(object))
-    rescue Exception => e
-      puts e.inspect
-      raise e
-    end
   end
   
   def delete_subscriber(sid)
diff --git a/app/helpers/socket_helper.rb b/app/helpers/socket_helper.rb
index fbdd37f7a5..36124a60da 100644
--- a/app/helpers/socket_helper.rb
+++ b/app/helpers/socket_helper.rb
@@ -1,26 +1,23 @@
 module SocketHelper
  include ApplicationHelper 
-  def obj_id(object)
+  
+ def obj_id(object)
     (object.is_a? Post) ? object.id : object.post_id
   end
   
   def url_options
-    {:host => "", :only_path => true}
+    {:host => ""}
   end
 
   def action_hash(object)
-      
     begin
       v = render_to_string(:partial => type_partial(object), :locals => {:post => object}) unless object.is_a? Retraction
-
-
     rescue Exception => e
       puts "web socket view rendering failed for some reason." + 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)}.to_json
   end
 
diff --git a/config/initializers/socket.rb b/config/initializers/socket.rb
index badb33c6f4..d1fcfc3159 100644
--- a/config/initializers/socket.rb
+++ b/config/initializers/socket.rb
@@ -27,7 +27,6 @@ module WebSocket
   end
   
   def self.push_to_clients(html)
-    puts html
     @channel.push(html)
   end
   
@@ -37,7 +36,7 @@ module WebSocket
   
   
   def self.subscribe
-    @channel.subscribe{ |msg|  puts @ws.inspect; puts "ehllo" ; @ws.send msg }
+    @channel.subscribe{ |msg| @ws.send msg }
   end
   
 end
diff --git a/spec/controllers/socket_controller_spec.rb b/spec/controllers/socket_controller_spec.rb
index 8f3f47e763..5f5a8b70f3 100644
--- a/spec/controllers/socket_controller_spec.rb
+++ b/spec/controllers/socket_controller_spec.rb
@@ -1,23 +1,18 @@
 require File.dirname(__FILE__) + '/../spec_helper'
 
-#require 'em-spec/rspec'
-describe SocketController do
-  render_views
-  
+describe 'SocketController' do
+  render_views  
   before do
     @user = Factory.create(:user)
-    WebSocket.unstub!(:push_to_clients)
-    WebSocket.unstub!(:unsubscribe)
-    WebSocket.unstub!(:subscribe)
+    SocketController.unstub!(:new)
     #EventMachine::WebSocket.stub!(:start)
     @controller = SocketController.new
+    stub_socket_controller
   end
 
   it 'should unstub the websocket' do
       WebSocket.initialize_channel
-      WebSocket.push_to_clients("what").should_not == "stub"
-      WebSocket.unsubscribe(1).should_not == "stub"
-      WebSocket.subscribe.should_not == "stub"
+      @controller.class.should == SocketController
   end
   
   it 'should add a new subscriber to the websocket channel' do
@@ -30,16 +25,16 @@ describe SocketController do
     end
 
     it 'should actionhash posts' do
-      hash = @controller.action_hash(@message)
-      hash[:html].include?(@message.message).should be_true
-      hash[:class].include?('status_message').should be_true
+      json = @controller.action_hash(@message)
+      json.include?(@message.message).should be_true
+      json.include?('status_message').should be_true
     end
 
     it 'should actionhash retractions' do
       retraction = Retraction.for @message
-      hash = @controller.action_hash(retraction)
-      hash[:class].include?('retraction').should be_true
-      hash[:html].should be_nil
+      json = @controller.action_hash(retraction)
+      json.include?('retraction').should be_true
+      json.include?("html\":null").should be_true
     end
   end
 end
diff --git a/spec/lib/message_handler_spec.rb b/spec/lib/message_handler_spec.rb
index 3ec0a6e2c1..553135b967 100644
--- a/spec/lib/message_handler_spec.rb
+++ b/spec/lib/message_handler_spec.rb
@@ -5,6 +5,7 @@ describe MessageHandler do
     @handler = MessageHandler.new
     @message_body = "I want to pump you up" 
     @message_urls = ["http://www.google.com/", "http://yahoo.com/", "http://foo.com/"]
+
   end
 
   describe 'GET messages' do
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 268305b320..a4d470af2e 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -24,15 +24,7 @@ RSpec.configure do |config|
 
   config.before(:each) do
     DatabaseCleaner.start
-    SocketController.stub!(:incoming).and_return(true)
-    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")
+    stub_socket_controller 
   end
 
   config.after(:each) do
@@ -40,3 +32,11 @@ RSpec.configure do |config|
   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
-- 
GitLab