sarb 0.1.0 → 0.1.1

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.
data/.gitignore CHANGED
@@ -4,3 +4,12 @@
4
4
  # Version Manager files
5
5
  .rvmrc
6
6
 
7
+ # RSpec
8
+ .rspec
9
+
10
+ # Bundler
11
+ Gemfile.lock
12
+
13
+ # Gem
14
+ sarb-*.gem
15
+
data/README.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  Framework for em-websocket that uses actions and triggers for real-time communication with your app.
4
4
 
5
+ ## Install
6
+
7
+ For latest rubygems version:
8
+
9
+ ```ruby
10
+ gem "sarb"
11
+ ```
12
+
13
+ For latest github commit:
14
+
15
+ ```ruby
16
+ gem "sarb", :git => "https://github.com/craigjackson/sarb.git"
17
+ ```
18
+
5
19
  ## Example
6
20
 
7
21
  ```ruby
@@ -11,7 +11,7 @@ module Sarb
11
11
  :connection_open => [method(:connection_open)],
12
12
  :connection_close => [method(:connection_close)]
13
13
  }
14
- @connections = []
14
+ @connections = Set.new
15
15
  end
16
16
 
17
17
  def action(name, &block)
@@ -35,6 +35,10 @@ module Sarb
35
35
  @connections.delete(args[:connection])
36
36
  end
37
37
 
38
+ def message_all(message, exceptions=Set.new)
39
+ @connections.each { |c| c.message(message) unless exceptions.include?(c) }
40
+ end
41
+
38
42
  def run(options = {})
39
43
  EventMachine::WebSocket.start(DEFAULTS.merge(options), &method(:new_connection))
40
44
  end
@@ -26,6 +26,7 @@ module Sarb
26
26
  ws.onopen &connection.method(:onopen)
27
27
  ws.onmessage &connection.method(:onmessage)
28
28
  ws.onclose &connection.method(:onclose)
29
+ connection
29
30
  end
30
31
  end
31
32
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "sarb"
3
- s.version = "0.1.0"
3
+ s.version = "0.1.1"
4
4
  s.summary = "Socket Action Ruby"
5
5
  s.description = "Framework for em-websocket that uses actions and triggers for real-time communication with your app."
6
6
  s.authors = ["Craig Jackson"]
@@ -12,7 +12,7 @@ describe Sarb::Application do
12
12
  :connection_open => [@app.method(:connection_open)],
13
13
  :connection_close => [@app.method(:connection_close)]
14
14
  }
15
- @app.instance_variable_get(:@connections).should == []
15
+ @app.instance_variable_get(:@connections).should == Set.new
16
16
  end
17
17
  end
18
18
 
@@ -63,9 +63,9 @@ describe Sarb::Application do
63
63
 
64
64
  context "new_connection" do
65
65
  it "should pass along the websocket to the Connection and set onmessage event" do
66
- @connection = Sarb::Connection.new(@app, "ws")
67
- Sarb::Connection.should_receive(:setup).with(@app, "ws").and_return(@connection)
68
- @app.new_connection("ws").should === @connection
66
+ connection = Sarb::Connection.new(@app, "ws")
67
+ Sarb::Connection.should_receive(:setup).with(@app, "ws").and_return(connection)
68
+ @app.new_connection("ws").should === connection
69
69
  end
70
70
  end
71
71
 
@@ -73,7 +73,7 @@ describe Sarb::Application do
73
73
  it "should add to @connections" do
74
74
  connection = "connection"
75
75
  @app.connection_open({:connection => connection})
76
- @app.instance_variable_get(:@connections).should == [connection]
76
+ @app.instance_variable_get(:@connections).should == Set.new([connection])
77
77
  end
78
78
  end
79
79
 
@@ -81,35 +81,58 @@ describe Sarb::Application do
81
81
  it "should remove from @connections" do
82
82
  connection1 = "connection1"
83
83
  connection2 = "connection2"
84
- @app.instance_variable_set(:@connections, [connection1, connection2])
84
+ @app.instance_variable_set(:@connections, Set.new([connection1, connection2]))
85
85
  @app.connection_close({:connection => connection1})
86
- @app.instance_variable_get(:@connections).should == [connection2]
86
+ @app.instance_variable_get(:@connections).should == Set.new([connection2])
87
+ end
88
+ end
89
+
90
+ context "message_all" do
91
+ it "should send message to all connections" do
92
+ connection1 = Sarb::Connection.new(@app, "ws")
93
+ connection2 = Sarb::Connection.new(@app, "ws")
94
+ connection1.should_receive(:message).with("message")
95
+ connection2.should_receive(:message).with("message")
96
+ @app.instance_variable_set(:@connections, Set.new([connection1, connection2]))
97
+ @app.message_all "message"
98
+ end
99
+
100
+ it "should allow exceptions to message_all" do
101
+ connection1 = Sarb::Connection.new(@app, "ws")
102
+ connection2 = Sarb::Connection.new(@app, "ws")
103
+ connection1.should_receive(:message).with("message")
104
+ connection2.should_not_receive(:message)
105
+ @app.instance_variable_set(:@connections, Set.new([connection1, connection2]))
106
+ @app.message_all "message", Set.new([connection2])
87
107
  end
88
108
  end
89
109
 
90
110
  context "run" do
91
111
  it "should start an EventMachine::WebSocket with defaults" do
92
112
  method = Proc.new {}
93
- @app.stub(:method => method)
94
- # TODO: Not really sure how to test &method is passed. "with" does not actually test &method.
95
- EventMachine::WebSocket.should_receive(:start).with(Sarb::Application::DEFAULTS, &method)
113
+ method_expected = false
114
+ @app.stub(:method).with(:new_connection).and_return(method)
115
+ EventMachine::WebSocket.should_receive(:start){|&block| method_expected = (block == method)}.with(Sarb::Application::DEFAULTS)
96
116
  @app.run
117
+ method_expected.should be_true
97
118
  end
98
119
 
99
120
  it "should start an EventMachine::WebSocket with given host and default port" do
100
121
  method = Proc.new {}
101
- @app.stub(:method => method)
102
- # TODO: Not really sure how to test &method is passed. "with" does not actually test &method.
103
- EventMachine::WebSocket.should_receive(:start).with(:host => "testhost", :port => Sarb::Application::DEFAULTS[:port], &method)
122
+ method_expected = false
123
+ @app.stub(:method).with(:new_connection).and_return(method)
124
+ EventMachine::WebSocket.should_receive(:start){|&block| method_expected = (block == method)}.with(:host => "testhost", :port => Sarb::Application::DEFAULTS[:port])
104
125
  @app.run :host => "testhost"
126
+ method_expected.should be_true
105
127
  end
106
128
 
107
129
  it "should start an EventMachine::WebSocket with default host and given port" do
108
130
  method = Proc.new {}
109
- @app.stub(:method => method)
110
- # TODO: Not really sure how to test &method is passed. "with" does not actually test &method.
111
- EventMachine::WebSocket.should_receive(:start).with(:host => Sarb::Application::DEFAULTS[:host], :port => "testport", &method)
131
+ method_expected = false
132
+ @app.stub(:method).with(:new_connection).and_return(method)
133
+ EventMachine::WebSocket.should_receive(:start){|&block| method_expected = (block == method)}.with(:host => Sarb::Application::DEFAULTS[:host], :port => "testport")
112
134
  @app.run :port => "testport"
135
+ method_expected.should be_true
113
136
  end
114
137
  end
115
138
 
@@ -51,11 +51,26 @@ describe Sarb::Connection do
51
51
  context "setup" do
52
52
  it "should create new Connection and add 'on' events" do
53
53
  Sarb::Connection.should_receive(:new).with(@app, @ws).and_return(@connection)
54
- # TODO: Putting the intended blocks in the 'with' method on the following stubs causes an error.
55
- @ws.should_receive(:onopen)
56
- @ws.should_receive(:onmessage)
57
- @ws.should_receive(:onclose)
58
- Sarb::Connection.setup(@app, @ws)
54
+
55
+ onopen_expected = false
56
+ onopen_method = Proc.new{ :onopen }
57
+ @connection.should_receive(:method).with(:onopen).and_return(onopen_method)
58
+ @ws.should_receive(:onopen) { |&block| onopen_expected = (block == onopen_method) }
59
+
60
+ onmessage_expected = false
61
+ onmessage_method = Proc.new{ :onmessage }
62
+ @connection.should_receive(:method).with(:onmessage).and_return(onmessage_method)
63
+ @ws.should_receive(:onmessage) { |&block| onmessage_expected = (block == onmessage_method) }
64
+
65
+ onclose_expected = false
66
+ onclose_method = Proc.new{ :onclose }
67
+ @connection.should_receive(:method).with(:onclose).and_return(onclose_method)
68
+ @ws.should_receive(:onclose) { |&block| onclose_expected = (block == onclose_method) }
69
+
70
+ Sarb::Connection.setup(@app, @ws).should be_equal(@connection)
71
+ onopen_expected.should be_true
72
+ onmessage_expected.should be_true
73
+ onclose_expected.should be_true
59
74
  end
60
75
  end
61
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sarb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-10 00:00:00.000000000 Z
12
+ date: 2013-04-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: em-websocket
16
- requirement: &7758080 !ruby/object:Gem::Requirement
16
+ requirement: &5988340 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 0.5.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *7758080
24
+ version_requirements: *5988340
25
25
  description: Framework for em-websocket that uses actions and triggers for real-time
26
26
  communication with your app.
27
27
  email: tapocol@gmail.com
@@ -30,9 +30,7 @@ extensions: []
30
30
  extra_rdoc_files: []
31
31
  files:
32
32
  - .gitignore
33
- - .rspec
34
33
  - Gemfile
35
- - Gemfile.lock
36
34
  - README.md
37
35
  - Rakefile
38
36
  - lib/sarb.rb
data/.rspec DELETED
@@ -1,2 +0,0 @@
1
- --color
2
-
@@ -1,24 +0,0 @@
1
- GEM
2
- remote: https://rubygems.org/
3
- specs:
4
- diff-lcs (1.2.2)
5
- em-websocket (0.5.0)
6
- eventmachine (>= 0.12.9)
7
- http_parser.rb (~> 0.5.3)
8
- eventmachine (1.0.3)
9
- http_parser.rb (0.5.3)
10
- rspec (2.13.0)
11
- rspec-core (~> 2.13.0)
12
- rspec-expectations (~> 2.13.0)
13
- rspec-mocks (~> 2.13.0)
14
- rspec-core (2.13.1)
15
- rspec-expectations (2.13.0)
16
- diff-lcs (>= 1.1.3, < 2.0)
17
- rspec-mocks (2.13.1)
18
-
19
- PLATFORMS
20
- ruby
21
-
22
- DEPENDENCIES
23
- em-websocket
24
- rspec