websocket-rails 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. data/CHANGELOG.md +34 -0
  2. data/Gemfile +1 -0
  3. data/README.md +34 -34
  4. data/assets/javascripts/http_dispatcher.js +6 -5
  5. data/assets/javascripts/websocket_dispatcher.js +7 -6
  6. data/lib/websocket-rails.rb +22 -2
  7. data/lib/websocket_rails/base_controller.rb +24 -8
  8. data/lib/websocket_rails/connection_adapters.rb +43 -16
  9. data/lib/websocket_rails/connection_adapters/http.rb +14 -10
  10. data/lib/websocket_rails/connection_adapters/web_socket.rb +13 -12
  11. data/lib/websocket_rails/connection_manager.rb +19 -47
  12. data/lib/websocket_rails/dispatcher.rb +39 -33
  13. data/lib/websocket_rails/event.rb +71 -0
  14. data/lib/websocket_rails/event_map.rb +120 -0
  15. data/lib/websocket_rails/version.rb +1 -1
  16. data/spec/dummy/config/initializers/events.rb +2 -2
  17. data/spec/dummy/{public/stylesheets/.gitkeep → log/development.log} +0 -0
  18. data/spec/dummy/log/production.log +0 -0
  19. data/spec/dummy/log/server.log +0 -0
  20. data/spec/dummy/log/test.log +454 -0
  21. data/spec/integration/connection_manager_spec.rb +29 -13
  22. data/spec/spec_helper.rb +3 -3
  23. data/spec/support/helper_methods.rb +29 -0
  24. data/spec/support/mock_web_socket.rb +6 -2
  25. data/spec/unit/connection_adapters/http_spec.rb +1 -3
  26. data/spec/unit/connection_adapters/web_socket_spec.rb +1 -11
  27. data/spec/unit/connection_adapters_spec.rb +51 -19
  28. data/spec/unit/connection_manager_spec.rb +22 -46
  29. data/spec/unit/dispatcher_spec.rb +56 -25
  30. data/spec/unit/event_map_spec.rb +96 -0
  31. data/spec/unit/event_spec.rb +99 -0
  32. metadata +23 -25
  33. data/.gitignore +0 -11
  34. data/.rspec +0 -2
  35. data/.travis.yml +0 -3
  36. data/Gemfile.lock +0 -144
  37. data/Guardfile +0 -9
  38. data/lib/websocket_rails/events.rb +0 -53
  39. data/spec/unit/events_spec.rb +0 -70
  40. data/websocket-rails.gemspec +0 -26
data/Guardfile DELETED
@@ -1,9 +0,0 @@
1
- # A sample Guardfile
2
- # More info at https://github.com/guard/guard#readme
3
-
4
- guard 'rspec', :version => 2 do
5
- watch(%r{^spec/.+_spec\.rb$})
6
- watch(%r{^lib/websocket_rails/(.+)\.rb$}) { |m| "spec/unit/#{m[1]}_spec.rb" }
7
- watch('spec/spec_helper.rb') { "spec" }
8
- end
9
-
@@ -1,53 +0,0 @@
1
- module WebsocketRails
2
- # Provides a DSL for mapping client events to controller actions. A single event can be mapped to any
3
- # number of controllers and actions. You can define your event routes by creating an +events.rb+ file in
4
- # your application's +initializers+ directory. The DSL currently consists of a single method, {#subscribe},
5
- # which takes a symbolized event name as the first argument, and a Hash with the controller and method
6
- # name as the second argument.
7
- #
8
- # == Example events.rb file
9
- # # located in config/initializers/events.rb
10
- # WebsocketRails::Events.describe_events do
11
- # subscribe :client_connected, to: ChatController, with_method: :client_connected
12
- # subscribe :new_user, to: ChatController, with_method: :new_user
13
- # end
14
- class Events
15
-
16
- def self.describe_events(&block)
17
- WebsocketRails.route_block = block
18
- end
19
-
20
- attr_reader :classes, :events
21
-
22
- def initialize(dispatcher)
23
- @dispatcher = dispatcher
24
- evaluate( WebsocketRails.route_block ) if WebsocketRails.route_block
25
- end
26
-
27
- def routes_for(event,&block)
28
- @events[event].each do |klass,method|
29
- controller = @classes[klass]
30
- block.call( controller, method )
31
- end
32
- end
33
-
34
- def subscribe(event_name,options)
35
- klass = options[:to] || raise("Must specify a class for to: option in event route")
36
- method = options[:with_method] || raise("Must specify a method for with_method: option in event route")
37
- controller = klass.new
38
- if @classes[klass].nil?
39
- @classes[klass] = controller
40
- controller.instance_variable_set(:@_dispatcher,@dispatcher)
41
- controller.send :initialize_session if controller.respond_to?(:initialize_session)
42
- end
43
- @events[event_name] << [klass,method]
44
- end
45
-
46
- def evaluate(block)
47
- @events = Hash.new {|h,k| h[k] = Array.new}
48
- @classes = Hash.new
49
- instance_eval &block
50
- end
51
-
52
- end
53
- end
@@ -1,70 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module WebsocketRails
4
- describe Events do
5
-
6
- def define_test_events
7
- WebsocketRails.route_block = nil
8
- WebsocketRails::Events.describe_events do
9
- subscribe :client_connected, to: Object, with_method: :object_id
10
- end
11
- end
12
-
13
- let(:dispatcher) { double('dispatcher').as_null_object }
14
- subject { Events.new(dispatcher) }
15
-
16
- context "Events.describe_events" do
17
- it "should store the event route block in the global configuration" do
18
- define_test_events
19
- WebsocketRails.route_block.should be_present
20
- end
21
- end
22
-
23
- context "#evaluate" do
24
- it "should evaluate the route DSL" do
25
- evaluated = double()
26
- evaluated.should_receive(:done)
27
- block = Proc.new { evaluated.done }
28
- subject.evaluate( block )
29
- end
30
-
31
- it "should initialize empty hashes for classes and events" do
32
- subject.evaluate Proc.new {}
33
- subject.classes.should == {}
34
- subject.events.should == {}
35
- end
36
- end
37
-
38
- context "#subscribe" do
39
- before(:each) { define_test_events }
40
-
41
- it "should store the event in the events hash" do
42
- subject.events.has_key?(:client_connected).should be_true
43
- end
44
-
45
- it "should store the instantiated controller in the classes hash" do
46
- subject.classes[Object].class.should == Object
47
- end
48
-
49
- it "should set the dispatcher on the instantiated controller" do
50
- subject.classes[Object].instance_variable_get(:@_dispatcher).should == dispatcher
51
- end
52
-
53
- it "should store the class constant and method name in the events hash" do
54
- subject.events[:client_connected].should == [[Object,:object_id]]
55
- end
56
- end
57
-
58
- context "#routes_for" do
59
- before(:each) { define_test_events }
60
-
61
- it "should yield the class constant and method symbol for each route defined for an event" do
62
- subject.routes_for(:client_connected) do |controller,method|
63
- controller.class.should == Object
64
- method.should == :object_id
65
- end
66
- end
67
- end
68
-
69
- end
70
- end
@@ -1,26 +0,0 @@
1
- $:.push File.expand_path("../lib", __FILE__)
2
- require "websocket_rails/version"
3
-
4
- Gem::Specification.new do |s|
5
- s.name = "websocket-rails"
6
- s.summary = "Plug and play websocket support for ruby on rails. Includes event router for mapping javascript events to controller actions."
7
- s.description = "Seamless Ruby on Rails websocket integration."
8
- s.homepage = "http://danknox.github.com/websocket-rails/"
9
- s.files = Dir["{lib,config}/**/*"] + ["MIT-LICENSE", "Rakefile", "Gemfile", "README.rdoc"]
10
- s.version = WebsocketRails::VERSION
11
- s.platform = Gem::Platform::RUBY
12
- s.authors = [ "Dan Knox", "Kyle Whalen", "Three Dot Loft LLC" ]
13
- s.email = [ "dknox@threedotloft.com" ]
14
-
15
- s.files = `git ls-files`.split("\n")
16
- s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
17
- s.require_path = 'lib'
18
-
19
- s.add_dependency "rack"
20
- s.add_dependency "faye-websocket"
21
- s.add_dependency "thin"
22
- s.add_development_dependency "rake"
23
- s.add_development_dependency "rails"
24
- s.add_development_dependency "sqlite3"
25
- s.add_development_dependency "rspec-rails"
26
- end