sse-rails 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/README.md +3 -3
- data/lib/rails/sse/channel.rb +12 -2
- data/lib/rails/sse/version.rb +1 -1
- data/spec/rails/channel_spec.rb +27 -12
- data/spec/support/matchers.rb +5 -0
- metadata +15 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 02f8509d08fef3b64bf2d7117b2fabdd1f4e74e3
|
4
|
+
data.tar.gz: d34f8b852f182936ee146ef45ac32b8125e90c09
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53b69b8dd19d9be7168fb0e6250e8f94b16aa7beacbcb97ccee12ab34f400fc176d4557c2b20d41ca177bb3e3097fa0c6458a1447ecbc487c2944b16dd362fb4
|
7
|
+
data.tar.gz: 88126a2486705c030064369c567dbf2f84e77d9f48b88f5cb48a98da135c39ecdba97367db6db8d2ad7580401932abb5ed17d151f21e3fa7ef3f7bff4ebc661b
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# sse-rails
|
1
|
+
# sse-rails [](https://travis-ci.org/as-cii/sse-rails)
|
2
2
|
|
3
3
|
sse-rails is a simple wrapper around ActionController::Live to hide all the complexity of streaming.
|
4
4
|
|
@@ -38,7 +38,7 @@ And then use it in one of your actions:
|
|
38
38
|
```ruby
|
39
39
|
def listen
|
40
40
|
stream do |channel|
|
41
|
-
channel.post(event: 'refresh')
|
41
|
+
channel.post({ username: 'jim' }, event: 'refresh')
|
42
42
|
end
|
43
43
|
end
|
44
44
|
```
|
@@ -49,7 +49,7 @@ You can also use `Rails::SSE::Channel#ping!` to see if connection is still open.
|
|
49
49
|
def listen
|
50
50
|
stream do |channel|
|
51
51
|
loop do
|
52
|
-
channel.
|
52
|
+
channel.post('anything') if condition
|
53
53
|
|
54
54
|
channel.ping!
|
55
55
|
sleep 1
|
data/lib/rails/sse/channel.rb
CHANGED
@@ -8,18 +8,28 @@ module Rails
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def post(data, options = {})
|
11
|
-
raise ArgumentError
|
11
|
+
raise ArgumentError if empty_arg?(data) && empty_arg?(options)
|
12
12
|
|
13
13
|
options.each do |key, value|
|
14
14
|
@stream.write("#{key}: #{value}\n")
|
15
15
|
end
|
16
16
|
|
17
|
-
@stream.write("data: #{JSON.dump(data)}\n
|
17
|
+
@stream.write("data: #{JSON.dump(data)}\n") unless empty_arg?(data)
|
18
|
+
end_message
|
18
19
|
end
|
19
20
|
|
20
21
|
def ping!
|
21
22
|
post(:ping)
|
22
23
|
end
|
24
|
+
|
25
|
+
private
|
26
|
+
def end_message
|
27
|
+
@stream.write("\n")
|
28
|
+
end
|
29
|
+
|
30
|
+
def empty_arg?(argument)
|
31
|
+
!argument || argument.empty?
|
32
|
+
end
|
23
33
|
end
|
24
34
|
end
|
25
35
|
end
|
data/lib/rails/sse/version.rb
CHANGED
data/spec/rails/channel_spec.rb
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
require 'minitest/autorun'
|
2
2
|
require 'rails/sse/channel'
|
3
3
|
require 'json'
|
4
|
+
require 'stringio'
|
5
|
+
require 'support/matchers'
|
4
6
|
|
5
7
|
describe Rails::SSE::Channel do
|
6
8
|
before(:each) do
|
7
|
-
@input
|
8
|
-
@channel = Rails::SSE::Channel.new(@
|
9
|
+
@input = StringIO.new
|
10
|
+
@channel = Rails::SSE::Channel.new(@input)
|
9
11
|
end
|
10
12
|
|
11
13
|
it 'encodes data in json' do
|
12
14
|
data = { 'test' => 1 }
|
13
15
|
|
14
16
|
@channel.post(data)
|
15
|
-
response = @input.
|
17
|
+
response = @input.string.match(/data: (.+)\n/)
|
16
18
|
|
17
19
|
JSON.parse(response[1]).must_equal(data)
|
18
20
|
end
|
@@ -21,24 +23,37 @@ describe Rails::SSE::Channel do
|
|
21
23
|
options = { event: 'refresh', id: 'test' }
|
22
24
|
@channel.post({}, options)
|
23
25
|
|
24
|
-
@input.
|
25
|
-
@input.
|
26
|
+
@input.string.must_match(/event: refresh\n/)
|
27
|
+
@input.string.must_match(/id: test\n/)
|
26
28
|
end
|
27
29
|
|
28
30
|
it 'pings client for keep–alive' do
|
29
31
|
@channel.ping!
|
30
32
|
|
31
|
-
@input.
|
33
|
+
@input.string.must_match(/data: (.+)\n/)
|
32
34
|
end
|
33
35
|
|
34
|
-
it 'closes
|
35
|
-
@channel.post({})
|
36
|
+
it 'closes a data-only message with a blank line' do
|
37
|
+
@channel.post({ test: 'data' })
|
36
38
|
|
37
|
-
|
38
|
-
@input.gets.wont_be_nil
|
39
|
+
assert_last_line_blank
|
39
40
|
end
|
40
41
|
|
41
|
-
it '
|
42
|
-
|
42
|
+
it 'closes a options-only message with a blank line' do
|
43
|
+
@channel.post(nil, event: 'test')
|
44
|
+
|
45
|
+
assert_last_line_blank
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'does not include data option when data is empty' do
|
49
|
+
@channel.post(nil, event: 'test')
|
50
|
+
|
51
|
+
@input.string.wont_match(/data: .*/)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'raises an error when data and options are both nil or empty' do
|
55
|
+
lambda { @channel.post(nil, nil) }.must_raise(ArgumentError)
|
56
|
+
lambda { @channel.post({}, nil) }.must_raise(ArgumentError)
|
57
|
+
lambda { @channel.post(nil, {}) }.must_raise(ArgumentError)
|
43
58
|
end
|
44
59
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sse-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Antonio Scandurra
|
@@ -14,42 +14,42 @@ dependencies:
|
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.3'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.3'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
description: SSE for rails, made easy.
|
@@ -59,7 +59,8 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
-
|
62
|
+
- .gitignore
|
63
|
+
- .travis.yml
|
63
64
|
- Gemfile
|
64
65
|
- LICENSE.txt
|
65
66
|
- README.md
|
@@ -71,6 +72,7 @@ files:
|
|
71
72
|
- spec/rails/sse_spec.rb
|
72
73
|
- spec/support/fake_controller.rb
|
73
74
|
- spec/support/fake_response.rb
|
75
|
+
- spec/support/matchers.rb
|
74
76
|
- sse-rails.gemspec
|
75
77
|
homepage: https://github.com/as-cii/sse-rails
|
76
78
|
licenses:
|
@@ -82,17 +84,17 @@ require_paths:
|
|
82
84
|
- lib
|
83
85
|
required_ruby_version: !ruby/object:Gem::Requirement
|
84
86
|
requirements:
|
85
|
-
- -
|
87
|
+
- - '>='
|
86
88
|
- !ruby/object:Gem::Version
|
87
89
|
version: '0'
|
88
90
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
91
|
requirements:
|
90
|
-
- -
|
92
|
+
- - '>='
|
91
93
|
- !ruby/object:Gem::Version
|
92
94
|
version: '0'
|
93
95
|
requirements: []
|
94
96
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.
|
97
|
+
rubygems_version: 2.0.3
|
96
98
|
signing_key:
|
97
99
|
specification_version: 4
|
98
100
|
summary: SSE for rails, made easy.
|
@@ -101,3 +103,5 @@ test_files:
|
|
101
103
|
- spec/rails/sse_spec.rb
|
102
104
|
- spec/support/fake_controller.rb
|
103
105
|
- spec/support/fake_response.rb
|
106
|
+
- spec/support/matchers.rb
|
107
|
+
has_rdoc:
|