Skip to content

Commit

Permalink
Commit initial repository
Browse files Browse the repository at this point in the history
  • Loading branch information
havenwood committed Oct 18, 2024
0 parents commit f374312
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/Gemfile.lock
/pkg/*
/roda-ssse-*.gem
9 changes: 9 additions & 0 deletions Gemfile
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'
18 changes: 18 additions & 0 deletions LICENSE
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.
36 changes: 36 additions & 0 deletions README.md
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
```
14 changes: 14 additions & 0 deletions Rakefile
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
34 changes: 34 additions & 0 deletions lib/roda/plugins/sse.rb
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
15 changes: 15 additions & 0 deletions roda-sse.gemspec
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
56 changes: 56 additions & 0 deletions spec/roda-sse_spec.rb
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

0 comments on commit f374312

Please sign in to comment.