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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ed37b1a0278ed017ea493c828438d7ed27f53b6
4
- data.tar.gz: 7489f61464b12e05449b6f8879c29994b2cb64cf
3
+ metadata.gz: 02f8509d08fef3b64bf2d7117b2fabdd1f4e74e3
4
+ data.tar.gz: d34f8b852f182936ee146ef45ac32b8125e90c09
5
5
  SHA512:
6
- metadata.gz: cccb177bee6b02003472d8a4444cf8c0e8ca97b8e876008366f4b5b171e97fcbb0216bfe868330649267727584e5685ebf8fbfcdd1e95aa285f8c88670a3bc00
7
- data.tar.gz: 3537098921904f85c1f47a616b6751a56b7b07d27bc10d760cae5e290b462898355f71ea8c9ab0e4c5ea8a770682ef62507d6d06ad0e90bbb8938f636ae997c5
6
+ metadata.gz: 53b69b8dd19d9be7168fb0e6250e8f94b16aa7beacbcb97ccee12ab34f400fc176d4557c2b20d41ca177bb3e3097fa0c6458a1447ecbc487c2944b16dd362fb4
7
+ data.tar.gz: 88126a2486705c030064369c567dbf2f84e77d9f48b88f5cb48a98da135c39ecdba97367db6db8d2ad7580401932abb5ed17d151f21e3fa7ef3f7bff4ebc661b
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # sse-rails
1
+ # sse-rails [![Build Status](https://travis-ci.org/as-cii/sse-rails.png?branch=master)](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.send('something') if condition
52
+ channel.post('anything') if condition
53
53
 
54
54
  channel.ping!
55
55
  sleep 1
@@ -8,18 +8,28 @@ module Rails
8
8
  end
9
9
 
10
10
  def post(data, options = {})
11
- raise ArgumentError unless data
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\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
@@ -1,5 +1,5 @@
1
1
  module Rails
2
2
  module SSE
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -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, @output = IO.pipe
8
- @channel = Rails::SSE::Channel.new(@output)
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.gets.match(/data: (.+)/)
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.gets.must_match(/event: refresh/)
25
- @input.gets.must_match(/id: test/)
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.gets.must_match(/data: (.+)/)
33
+ @input.string.must_match(/data: (.+)\n/)
32
34
  end
33
35
 
34
- it 'closes the message with two LF' do
35
- @channel.post({})
36
+ it 'closes a data-only message with a blank line' do
37
+ @channel.post({ test: 'data' })
36
38
 
37
- @input.gets.wont_be_nil
38
- @input.gets.wont_be_nil
39
+ assert_last_line_blank
39
40
  end
40
41
 
41
- it 'raises an error when data is nil' do
42
- lambda { @channel.post(nil) }.must_raise(ArgumentError)
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
@@ -0,0 +1,5 @@
1
+ def assert_last_line_blank
2
+ lines = @input.string.lines
3
+ lines[lines.count - 2].wont_equal("\n")
4
+ lines.last.must_equal("\n")
5
+ 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.1
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
- - ".gitignore"
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.2.0
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: