sse-rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +69 -0
- data/Rakefile +10 -0
- data/lib/rails/sse.rb +22 -0
- data/lib/rails/sse/channel.rb +25 -0
- data/lib/rails/sse/version.rb +5 -0
- data/spec/rails/channel_spec.rb +44 -0
- data/spec/rails/sse_spec.rb +51 -0
- data/spec/support/fake_controller.rb +9 -0
- data/spec/support/fake_response.rb +8 -0
- data/sse-rails.gemspec +24 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7ed37b1a0278ed017ea493c828438d7ed27f53b6
|
4
|
+
data.tar.gz: 7489f61464b12e05449b6f8879c29994b2cb64cf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cccb177bee6b02003472d8a4444cf8c0e8ca97b8e876008366f4b5b171e97fcbb0216bfe868330649267727584e5685ebf8fbfcdd1e95aa285f8c88670a3bc00
|
7
|
+
data.tar.gz: 3537098921904f85c1f47a616b6751a56b7b07d27bc10d760cae5e290b462898355f71ea8c9ab0e4c5ea8a770682ef62507d6d06ad0e90bbb8938f636ae997c5
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Antonio Scandurra
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# sse-rails
|
2
|
+
|
3
|
+
sse-rails is a simple wrapper around ActionController::Live to hide all the complexity of streaming.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'sse-rails'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
```bash
|
22
|
+
$ gem install sse-rails
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Add these lines to a controller:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'rails/sse'
|
31
|
+
|
32
|
+
include ActionController::Live
|
33
|
+
include Rails::SSE
|
34
|
+
```
|
35
|
+
|
36
|
+
And then use it in one of your actions:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
def listen
|
40
|
+
stream do |channel|
|
41
|
+
channel.post(event: 'refresh')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
You can also use `Rails::SSE::Channel#ping!` to see if connection is still open. This is useful when you are in a loop like this:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
def listen
|
50
|
+
stream do |channel|
|
51
|
+
loop do
|
52
|
+
channel.send('something') if condition
|
53
|
+
|
54
|
+
channel.ping!
|
55
|
+
sleep 1
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
Without pinging you would know that the connection was lost only when the condition becomes true.
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
1. Fork it
|
66
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
67
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
68
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
69
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/rails/sse.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require "rails/sse/version"
|
2
|
+
require "rails/sse/channel"
|
3
|
+
|
4
|
+
module Rails
|
5
|
+
module SSE
|
6
|
+
def channel
|
7
|
+
Channel.new(response.stream)
|
8
|
+
end
|
9
|
+
|
10
|
+
def stream
|
11
|
+
response.headers['Content-Type'] = 'text/event-stream'
|
12
|
+
|
13
|
+
begin
|
14
|
+
yield(channel) if block_given?
|
15
|
+
rescue IOError
|
16
|
+
ensure
|
17
|
+
response.stream.close unless response.stream.closed?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Rails
|
2
|
+
module SSE
|
3
|
+
class Channel
|
4
|
+
attr_reader :stream
|
5
|
+
|
6
|
+
def initialize(stream)
|
7
|
+
@stream = stream
|
8
|
+
end
|
9
|
+
|
10
|
+
def post(data, options = {})
|
11
|
+
raise ArgumentError unless data
|
12
|
+
|
13
|
+
options.each do |key, value|
|
14
|
+
@stream.write("#{key}: #{value}\n")
|
15
|
+
end
|
16
|
+
|
17
|
+
@stream.write("data: #{JSON.dump(data)}\n\n")
|
18
|
+
end
|
19
|
+
|
20
|
+
def ping!
|
21
|
+
post(:ping)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'rails/sse/channel'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
describe Rails::SSE::Channel do
|
6
|
+
before(:each) do
|
7
|
+
@input, @output = IO.pipe
|
8
|
+
@channel = Rails::SSE::Channel.new(@output)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'encodes data in json' do
|
12
|
+
data = { 'test' => 1 }
|
13
|
+
|
14
|
+
@channel.post(data)
|
15
|
+
response = @input.gets.match(/data: (.+)/)
|
16
|
+
|
17
|
+
JSON.parse(response[1]).must_equal(data)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'encodes options in kv format' do
|
21
|
+
options = { event: 'refresh', id: 'test' }
|
22
|
+
@channel.post({}, options)
|
23
|
+
|
24
|
+
@input.gets.must_match(/event: refresh/)
|
25
|
+
@input.gets.must_match(/id: test/)
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'pings client for keep–alive' do
|
29
|
+
@channel.ping!
|
30
|
+
|
31
|
+
@input.gets.must_match(/data: (.+)/)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'closes the message with two LF' do
|
35
|
+
@channel.post({})
|
36
|
+
|
37
|
+
@input.gets.wont_be_nil
|
38
|
+
@input.gets.wont_be_nil
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'raises an error when data is nil' do
|
42
|
+
lambda { @channel.post(nil) }.must_raise(ArgumentError)
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'support/fake_controller'
|
3
|
+
require 'rails/sse'
|
4
|
+
|
5
|
+
describe Rails::SSE do
|
6
|
+
before(:each) do
|
7
|
+
@controller = FakeController.new
|
8
|
+
@controller.extend(Rails::SSE)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'sets content-type to text/event-stream' do
|
12
|
+
@controller.stream
|
13
|
+
|
14
|
+
content_type = @controller.response.headers['Content-Type']
|
15
|
+
content_type.must_equal('text/event-stream')
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'closes event stream before ending communication' do
|
19
|
+
@controller.stream
|
20
|
+
|
21
|
+
@controller.response.stream.closed?.must_equal(true)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'closes event stream when errors are raised' do
|
25
|
+
@controller.stream do |channel|
|
26
|
+
raise IOError
|
27
|
+
end
|
28
|
+
|
29
|
+
@controller.response.stream.closed?.must_equal(true)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'streams messages to the channel' do
|
33
|
+
channel = MiniTest::Mock.new
|
34
|
+
channel.expect(:ping!, nil)
|
35
|
+
channel.expect(:send, nil, ['p1', 'p2'])
|
36
|
+
|
37
|
+
@controller.stub(:channel, channel) do
|
38
|
+
@controller.stream do |channel|
|
39
|
+
channel.ping!
|
40
|
+
channel.send('p1', 'p2')
|
41
|
+
end
|
42
|
+
|
43
|
+
channel.verify
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'has a default channel' do
|
48
|
+
@controller.channel.must_be_instance_of(Rails::SSE::Channel)
|
49
|
+
@controller.channel.stream.must_equal @controller.response.stream
|
50
|
+
end
|
51
|
+
end
|
data/sse-rails.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails/sse/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sse-rails"
|
8
|
+
spec.version = Rails::SSE::VERSION
|
9
|
+
spec.authors = ["Antonio Scandurra"]
|
10
|
+
spec.email = ["as-cii@outlook.com"]
|
11
|
+
spec.description = %q{SSE for rails, made easy.}
|
12
|
+
spec.summary = spec.description
|
13
|
+
spec.homepage = "https://github.com/as-cii/sse-rails"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "minitest"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sse-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Antonio Scandurra
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: SSE for rails, made easy.
|
56
|
+
email:
|
57
|
+
- as-cii@outlook.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/rails/sse.rb
|
68
|
+
- lib/rails/sse/channel.rb
|
69
|
+
- lib/rails/sse/version.rb
|
70
|
+
- spec/rails/channel_spec.rb
|
71
|
+
- spec/rails/sse_spec.rb
|
72
|
+
- spec/support/fake_controller.rb
|
73
|
+
- spec/support/fake_response.rb
|
74
|
+
- sse-rails.gemspec
|
75
|
+
homepage: https://github.com/as-cii/sse-rails
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.2.0
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: SSE for rails, made easy.
|
99
|
+
test_files:
|
100
|
+
- spec/rails/channel_spec.rb
|
101
|
+
- spec/rails/sse_spec.rb
|
102
|
+
- spec/support/fake_controller.rb
|
103
|
+
- spec/support/fake_response.rb
|