-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f374312
Showing
8 changed files
with
185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/Gemfile.lock | ||
/pkg/* | ||
/roda-ssse-*.gem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
source 'https://rubygems.org' | ||
|
||
gemspec | ||
|
||
gem 'minitest', '~> 5.2' | ||
gem 'minitest-proveit', '~> 1.0' | ||
gem 'rack-test', '~> 2.1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Copyright (c) Shannon Skipper | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to | ||
deal in the Software without restriction, including without limitation the | ||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
sell copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# roda-sse | ||
|
||
The roda-sse Roda plugin adds SSE headers and provides Rack 3 streaming for you to send your own events. | ||
|
||
## Installation | ||
|
||
```sh | ||
gem install roda-sse | ||
``` | ||
|
||
## Source Code | ||
|
||
Source code is available on GitHub at | ||
https://github.com/havenwood/roda-sse | ||
|
||
## Usage | ||
|
||
roda-sse is a Roda plugin, so you need to load it into your Roda | ||
application similar to other plugins: | ||
|
||
```ruby | ||
class App < Roda | ||
plugin :sse | ||
end | ||
``` | ||
|
||
In your routing block, you can then use `r.sse` to stream with the correct headers. | ||
|
||
```ruby | ||
r.sse do |stream| | ||
stream << "data: hello\n\n" | ||
stream << "data: world\n\n" | ||
ensure | ||
stream.close | ||
end | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'bundler/gem_tasks' | ||
require 'rake/clean' | ||
require 'rake/testtask' | ||
|
||
CLEAN.include %w[pkg/roda-sse-*.gem].freeze | ||
|
||
task default: :test | ||
|
||
Rake::TestTask.new do |test| | ||
test.pattern = 'spec/**/*_spec.rb' | ||
test.warning = false | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
class Roda | ||
module RodaPlugins | ||
# Example: | ||
# | ||
# plugin :sse | ||
# | ||
# route do |r| | ||
# r.root do | ||
# # GET / | ||
# r.sse do |stream| | ||
# stream.write "data: hola\n\n" | ||
# ensure | ||
# stream.close | ||
# end | ||
# end | ||
# end | ||
module SSE | ||
module RequestMethods | ||
def sse(&block) | ||
response['Content-Type'] = 'text/event-stream' | ||
response['Cache-Control'] = 'no-cache' | ||
|
||
always do | ||
halt response.finish_with_body(block) | ||
end | ||
end | ||
end | ||
end | ||
|
||
register_plugin(:sse, SSE) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
Gem::Specification.new do |s| | ||
s.name = 'roda-sse' | ||
s.version = '0.1.0' | ||
s.license = 'MIT' | ||
s.summary = 'SSE integration for Roda' | ||
s.description = 'The roda-sse gem integrates simple SSE streaming into the roda web toolkit.' | ||
s.author = 'Shannon Skipper' | ||
s.email = '[email protected]' | ||
s.homepage = 'https://github.com/havenwood/roda-sse' | ||
s.files = %w[Gemfile LICENSE Rakefile README.md] + Dir['{spec,lib}/**/*.rb'] | ||
s.add_dependency('roda', '~> 3.0') | ||
s.metadata['rubygems_mfa_required'] = 'true' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'minitest/autorun' | ||
require 'minitest/pride' | ||
require 'minitest/proveit' | ||
require 'rack/test' | ||
require 'roda' | ||
|
||
class App < Roda | ||
plugin :sse | ||
|
||
route do |r| | ||
r.root do | ||
r.sse do |stream| | ||
stream.write "data: hola\n\n" | ||
ensure | ||
stream.close | ||
end | ||
end | ||
end | ||
end | ||
|
||
def app = App.freeze.app | ||
|
||
describe 'roda-sse plugin' do | ||
include Rack::Test::Methods | ||
|
||
prove_it! | ||
|
||
it 'responds 200 OK' do | ||
get '/' | ||
|
||
assert last_response.ok? | ||
end | ||
|
||
it 'has SSE headers' do | ||
get '/' | ||
|
||
headers = {'content-type' => 'text/event-stream', 'cache-control' => 'no-cache'} | ||
assert_equal headers, last_response.headers | ||
end | ||
|
||
it 'streams the body' do | ||
get '/' | ||
|
||
stream = Minitest::Mock.new | ||
stream.expect(:write, nil, ["data: hola\n\n"]) | ||
stream.expect(:close, nil) | ||
response_body = last_response.instance_variable_get(:@body) | ||
assert_instance_of Proc, response_body | ||
|
||
response_body.call(stream) | ||
|
||
stream.verify | ||
end | ||
end |