websocket-rails 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. data/CHANGELOG.md +41 -0
  2. data/README.md +1 -1
  3. data/lib/generators/websocket_rails/install/templates/events.rb +15 -5
  4. data/lib/rails/tasks/websocket_rails.tasks +8 -10
  5. data/lib/spec_helpers/matchers/route_matchers.rb +2 -1
  6. data/lib/spec_helpers/spec_helper_event.rb +4 -0
  7. data/lib/websocket-rails.rb +50 -73
  8. data/lib/websocket_rails/base_controller.rb +9 -7
  9. data/lib/websocket_rails/channel.rb +8 -1
  10. data/lib/websocket_rails/channel_manager.rb +6 -0
  11. data/lib/websocket_rails/configuration.rb +112 -0
  12. data/lib/websocket_rails/connection_adapters.rb +13 -4
  13. data/lib/websocket_rails/connection_manager.rb +3 -2
  14. data/lib/websocket_rails/controller_factory.rb +70 -0
  15. data/lib/websocket_rails/data_store.rb +128 -62
  16. data/lib/websocket_rails/dispatcher.rb +17 -16
  17. data/lib/websocket_rails/event.rb +12 -2
  18. data/lib/websocket_rails/event_map.rb +6 -41
  19. data/lib/websocket_rails/internal_events.rb +0 -7
  20. data/lib/websocket_rails/logging.rb +103 -14
  21. data/lib/websocket_rails/synchronization.rb +6 -8
  22. data/lib/websocket_rails/version.rb +1 -1
  23. data/spec/dummy/app/controllers/chat_controller.rb +6 -14
  24. data/spec/dummy/log/test.log +0 -750
  25. data/spec/integration/connection_manager_spec.rb +8 -1
  26. data/spec/spec_helper.rb +3 -16
  27. data/spec/spec_helpers/matchers/route_matchers_spec.rb +2 -11
  28. data/spec/spec_helpers/matchers/trigger_matchers_spec.rb +2 -12
  29. data/spec/unit/channel_manager_spec.rb +8 -0
  30. data/spec/unit/channel_spec.rb +16 -2
  31. data/spec/unit/connection_adapters_spec.rb +32 -11
  32. data/spec/unit/controller_factory_spec.rb +63 -0
  33. data/spec/unit/data_store_spec.rb +91 -24
  34. data/spec/unit/dispatcher_spec.rb +6 -11
  35. data/spec/unit/event_map_spec.rb +17 -27
  36. data/spec/unit/event_spec.rb +14 -0
  37. data/spec/unit/logging_spec.rb +122 -17
  38. metadata +11 -6
@@ -24,7 +24,7 @@ module WebsocketRails
24
24
 
25
25
 
26
26
  def define_test_events
27
- WebsocketRails.route_block = nil
27
+ WebsocketRails.config.route_block = nil
28
28
  WebsocketRails::EventMap.describe do
29
29
  subscribe :client_connected, :to => ChatController, :with_method => :new_user
30
30
 
@@ -38,32 +38,24 @@ module WebsocketRails
38
38
 
39
39
  end
40
40
  end
41
-
41
+
42
42
  let(:dispatcher) { double('dispatcher').as_null_object }
43
43
  subject { EventMap.new(dispatcher) }
44
44
  before { define_test_events }
45
45
 
46
46
  context "EventMap.describe" do
47
47
  it "should store the event route block in the global configuration" do
48
- WebsocketRails.route_block.should be_present
48
+ WebsocketRails.config.route_block.should be_present
49
49
  end
50
50
  end
51
-
51
+
52
52
  context "Events in the global namespace" do
53
-
53
+
54
54
  it "should store the event in the correct namespace" do
55
55
  subject.namespace.actions[:client_connected].should be_present
56
56
  subject.namespace.name.should == :global
57
57
  end
58
-
59
- it "should store the instantiated controller in the classes hash" do
60
- subject.namespace.controllers[ChatController].class.should == ChatController
61
- end
62
-
63
- it "should set the dispatcher on the instantiated controller" do
64
- subject.namespace.controllers[ChatController].instance_variable_get(:@_dispatcher).should == dispatcher
65
- end
66
-
58
+
67
59
  it "should store the class constant and method name in the events hash" do
68
60
  subject.namespace.actions[:client_connected].should == [[ChatController,:new_user]]
69
61
  end
@@ -73,25 +65,21 @@ module WebsocketRails
73
65
  context "Events in a child namespace" do
74
66
 
75
67
  before { @namespace = subject.namespace }
76
-
68
+
77
69
  it "should store the event in the correct namespaces" do
78
70
  @namespace.namespaces[:product].actions[:update].should be_present
79
71
  @namespace.namespaces[:complex_product].actions[:simplify].should be_present
80
72
  end
81
-
73
+
82
74
  end
83
75
 
84
76
  context "#routes_for" do
85
-
86
- let(:event) { double('event') }
87
-
88
77
  context "with events in the global namespace" do
89
- it "should yield the controller and action name for each route defined for an event" do
90
- event.stub(:name).and_return(:client_connected)
91
- event.stub(:namespace).and_return([:global])
78
+ it "should yield the controller class and action name for each route defined for an event" do
79
+ event = HelperMethods::MockEvent.new(:client_connected, [:global])
92
80
 
93
- subject.routes_for(event) do |controller,method|
94
- controller.class.should == ChatController
81
+ subject.routes_for(event) do |klass, method|
82
+ klass.should == ChatController
95
83
  method.should == :new_user
96
84
  end
97
85
  end
@@ -101,8 +89,9 @@ module WebsocketRails
101
89
  it "should yield the controller and action name for each route defined with a hash for an event" do
102
90
  ProductController.any_instance.should_receive(:action_executed)
103
91
  event = HelperMethods::MockEvent.new :update, [:global,:product]
104
-
105
- subject.routes_for(event) do |controller,method|
92
+
93
+ subject.routes_for(event) do |klass, method|
94
+ controller = klass.new
106
95
  controller.action_executed
107
96
  controller.class.should == ProductController
108
97
  method.should == :update_product
@@ -114,7 +103,8 @@ module WebsocketRails
114
103
  ComplexProductController.any_instance.should_receive(:action_executed)
115
104
  event = HelperMethods::MockEvent.new :simplify, [:global,:complex_product]
116
105
 
117
- subject.routes_for(event) do |controller,method|
106
+ subject.routes_for(event) do |klass, method|
107
+ controller = klass.new
118
108
  controller.action_executed
119
109
  controller.class.should == ComplexProductController
120
110
  method.should == :simplify
@@ -96,6 +96,20 @@ module WebsocketRails
96
96
  end
97
97
  end
98
98
 
99
+ describe "#is_invalid?" do
100
+ it "returns true if the event name is :invalid_event" do
101
+ event = Event.new(:invalid_event)
102
+ event.is_invalid?.should be_true
103
+ end
104
+ end
105
+
106
+ describe "#is_internal?" do
107
+ it "returns true if the event is namespaced under websocket_rails" do
108
+ event = Event.new(:internal_event, :namespace => :websocket_rails)
109
+ event.is_internal?.should be_true
110
+ end
111
+ end
112
+
99
113
  describe "#serialize" do
100
114
  context "messages in the global namespace" do
101
115
  it "should not add the global namespace to the event name" do
@@ -1,38 +1,143 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module WebsocketRails
4
- class ClassWithLogging
5
- include Logging
6
- end
4
+ describe Logging do
5
+
6
+ class LoggedClass
7
+ include Logging
8
+ end
9
+
10
+ let(:io) { StringIO.new }
11
+ let(:object) { LoggedClass.new }
12
+
13
+ before do
14
+ WebsocketRails.config.logger = Logger.new(io)
15
+ end
16
+
17
+ describe "#info" do
18
+ it "logs the message" do
19
+ object.info "info logged"
20
+ io.string.should include("info logged")
21
+ end
22
+ end
23
+
24
+ describe "#debug" do
25
+ it "logs the message" do
26
+ object.debug "debug logged"
27
+ io.string.should include("debug logged")
28
+ end
29
+ end
30
+
31
+ describe "log_exception" do
32
+ let(:exception) { Exception.new('kaputt!').tap { |e| e.set_backtrace(['line 1', 'line 2']) } }
33
+
34
+ it "logs the exception message" do
35
+ object.log_exception(exception)
36
+ io.string.should include('kaputt!')
37
+ end
38
+
39
+ it "logs the backtrace" do
40
+ object.log_exception(exception)
41
+ io.string.should include("line 1")
42
+ io.string.should include("line 2")
43
+ end
44
+ end
45
+
46
+ context "logging an event" do
47
+ before do
48
+ data = {
49
+ namespace: :logger,
50
+ data: {message: "hello"},
51
+ connection: double('connection')
52
+ }
53
+ @event = Event.new(:logged_event, data)
54
+ end
55
+
56
+ describe "#log_event_start" do
57
+ it "logs the event information" do
58
+ object.log_event_start(@event)
59
+ io.string.should include("Started Event:")
60
+ io.string.should include("logger.logged_event")
61
+ end
62
+ end
63
+
64
+ describe "#log_event_end" do
65
+ it "logs the total time the event took to process" do
66
+ time = 12
67
+ object.log_event_end(@event, time)
68
+ io.string.should include("Event #{@event.encoded_name} Finished in #{time.to_f.to_d.to_s} seconds")
69
+ end
70
+ end
7
71
 
8
- describe ClassWithLogging do
72
+ describe "#log_event" do
73
+ it "logs the start of the event" do
74
+ object.should_receive(:log_event_start).with(@event)
75
+ object.log_event(@event) { true }
76
+ end
9
77
 
10
- describe "#log" do
11
- context "when log_level = :warn" do
78
+ it "logs the end of the event" do
79
+ Time.stub(:now).and_return(1)
80
+ object.should_receive(:log_event_end).with(@event, 0)
81
+ object.log_event(@event) { true }
82
+ end
83
+
84
+ it "executes the block" do
85
+ executed = false
86
+ object.log_event(@event) { executed = true }
87
+ executed.should == true
88
+ end
89
+
90
+ it "logs any exceptions" do
91
+ exception = Exception.new("ouch")
92
+ object.should_receive(:log_exception).with(exception)
93
+ expect {
94
+ object.log_event(@event) { raise exception }
95
+ }.to raise_exception(exception)
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "#log_event?" do
101
+ context "with an internal event" do
12
102
  before do
13
- WebsocketRails.setup do |config|
14
- config.log_level = :warn
103
+ @event = Event.new(:internal, :namespace => :websocket_rails)
104
+ end
105
+
106
+ context "when WebsocketRails.config.log_internal_events? is false" do
107
+ it "returns false" do
108
+ WebsocketRails.config.log_internal_events = false
109
+ object.log_event?(@event).should == false
15
110
  end
16
111
  end
17
112
 
18
- it "should not print to the console" do
19
- subject.should_not_receive(:puts).with("test message")
20
- subject.log "test message"
113
+ context "when WebsocketRails.config.log_internal_events? is true" do
114
+ it "returns true" do
115
+ WebsocketRails.config.log_internal_events = true
116
+ object.log_event?(@event).should == true
117
+ end
21
118
  end
22
119
  end
23
120
 
24
- context "log_level = :debug" do
121
+ context "with an external event" do
25
122
  before do
26
- WebsocketRails.setup do |config|
27
- config.log_level = :debug
123
+ @event = Event.new(:external)
124
+ end
125
+
126
+ context "when WebsocketRails.config.log_internal_events? is false" do
127
+ it "returns true" do
128
+ WebsocketRails.config.log_internal_events = false
129
+ object.log_event?(@event).should == true
28
130
  end
29
131
  end
30
132
 
31
- it "should print to the console if log_level is :debug" do
32
- subject.should_receive(:puts).with("test message")
33
- subject.log "test message"
133
+ context "when WebsocketRails.config.log_internal_events? is true" do
134
+ it "returns true" do
135
+ WebsocketRails.config.log_internal_events = true
136
+ object.log_event?(@event).should == true
137
+ end
34
138
  end
35
139
  end
36
140
  end
141
+
37
142
  end
38
143
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: websocket-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2013-02-06 00:00:00.000000000 Z
14
+ date: 2013-02-27 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
@@ -200,10 +200,12 @@ files:
200
200
  - lib/websocket_rails/base_controller.rb
201
201
  - lib/websocket_rails/channel.rb
202
202
  - lib/websocket_rails/channel_manager.rb
203
+ - lib/websocket_rails/configuration.rb
203
204
  - lib/websocket_rails/connection_adapters/http.rb
204
205
  - lib/websocket_rails/connection_adapters/web_socket.rb
205
206
  - lib/websocket_rails/connection_adapters.rb
206
207
  - lib/websocket_rails/connection_manager.rb
208
+ - lib/websocket_rails/controller_factory.rb
207
209
  - lib/websocket_rails/data_store.rb
208
210
  - lib/websocket_rails/dispatcher.rb
209
211
  - lib/websocket_rails/engine.rb
@@ -282,6 +284,7 @@ files:
282
284
  - spec/unit/connection_adapters/web_socket_spec.rb
283
285
  - spec/unit/connection_adapters_spec.rb
284
286
  - spec/unit/connection_manager_spec.rb
287
+ - spec/unit/controller_factory_spec.rb
285
288
  - spec/unit/data_store_spec.rb
286
289
  - spec/unit/dispatcher_spec.rb
287
290
  - spec/unit/event_map_spec.rb
@@ -297,7 +300,9 @@ files:
297
300
  - CHANGELOG.md
298
301
  homepage: http://danknox.github.com/websocket-rails/
299
302
  licenses: []
300
- post_install_message:
303
+ post_install_message: ! " Welcome to WebsocketRails v0.4.0!\n\n There have been
304
+ a few significant changes in the public\n API, so if you are upgrading please be
305
+ sure to read the\n CHANGELOG located at:\n\n http://github.com/DanKnox/websocket-rails/CHANGELOG.md\n"
301
306
  rdoc_options: []
302
307
  require_paths:
303
308
  - lib
@@ -309,7 +314,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
309
314
  version: '0'
310
315
  segments:
311
316
  - 0
312
- hash: 3143824869554570755
317
+ hash: 500969557461387914
313
318
  required_rubygems_version: !ruby/object:Gem::Requirement
314
319
  none: false
315
320
  requirements:
@@ -318,10 +323,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
318
323
  version: '0'
319
324
  segments:
320
325
  - 0
321
- hash: 3143824869554570755
326
+ hash: 500969557461387914
322
327
  requirements: []
323
328
  rubyforge_project: websocket-rails
324
- rubygems_version: 1.8.24
329
+ rubygems_version: 1.8.23
325
330
  signing_key:
326
331
  specification_version: 3
327
332
  summary: Plug and play websocket support for ruby on rails. Includes event router