roda-sse 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 81bbd096f5155da3f8593b8e3d37771daedd543a9cf4816d5bab36f3192bb203
4
+ data.tar.gz: 88366dcfba9fb10fcf5a810c540c3ae5b77c9f52989cdea5c03ea1b0a9aa9641
5
+ SHA512:
6
+ metadata.gz: 8375dd84ecfefe94dafb591d5edfb1e22a3413aa0f574b7c7a965b1adcf7ef62cd70397fb133a8f0e2468183edae44f82208742d357f45a43a69e53d9f352e6c
7
+ data.tar.gz: 9bf26ed27a75bb355f1468baa668e0c3b569b9313d1468b60ac9aa6bbe7a2c142592011bacb7f931232e18a2bb90c932011b375a6b73e7d654a942ae0bb57210
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ gem 'minitest', '~> 5.2'
8
+ gem 'minitest-proveit', '~> 1.0'
9
+ gem 'rack-test', '~> 2.1'
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) Shannon Skipper
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
+ THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # roda-sse
2
+
3
+ The roda-sse Roda plugin adds SSE headers and provides Rack 3 streaming for you to send your own events.
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ gem install roda-sse
9
+ ```
10
+
11
+ ## Source Code
12
+
13
+ Source code is available on GitHub at
14
+ https://github.com/havenwood/roda-sse
15
+
16
+ ## Usage
17
+
18
+ roda-sse is a Roda plugin, so you need to load it into your Roda
19
+ application similar to other plugins:
20
+
21
+ ```ruby
22
+ class App < Roda
23
+ plugin :sse
24
+ end
25
+ ```
26
+
27
+ In your routing block, you can then use `r.sse` to stream with the correct headers.
28
+
29
+ ```ruby
30
+ r.sse do |stream|
31
+ stream << "data: hello\n\n"
32
+ stream << "data: world\n\n"
33
+ ensure
34
+ stream.close
35
+ end
36
+ ```
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/clean'
5
+ require 'rake/testtask'
6
+
7
+ CLEAN.include %w[pkg/roda-sse-*.gem].freeze
8
+
9
+ task default: :test
10
+
11
+ Rake::TestTask.new do |test|
12
+ test.pattern = 'spec/**/*_spec.rb'
13
+ test.warning = false
14
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Roda
4
+ module RodaPlugins
5
+ # Example:
6
+ #
7
+ # plugin :sse
8
+ #
9
+ # route do |r|
10
+ # r.root do
11
+ # # GET /
12
+ # r.sse do |stream|
13
+ # stream.write "data: hola\n\n"
14
+ # ensure
15
+ # stream.close
16
+ # end
17
+ # end
18
+ # end
19
+ module SSE
20
+ module RequestMethods
21
+ def sse(&block)
22
+ response['Content-Type'] = 'text/event-stream'
23
+ response['Cache-Control'] = 'no-cache'
24
+
25
+ always do
26
+ halt response.finish_with_body(block)
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ register_plugin(:sse, SSE)
33
+ end
34
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'minitest/autorun'
4
+ require 'minitest/pride'
5
+ require 'minitest/proveit'
6
+ require 'rack/test'
7
+ require 'roda'
8
+
9
+ class App < Roda
10
+ plugin :sse
11
+
12
+ route do |r|
13
+ r.root do
14
+ r.sse do |stream|
15
+ stream.write "data: hola\n\n"
16
+ ensure
17
+ stream.close
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ def app = App.freeze.app
24
+
25
+ describe 'roda-sse plugin' do
26
+ include Rack::Test::Methods
27
+
28
+ prove_it!
29
+
30
+ it 'responds 200 OK' do
31
+ get '/'
32
+
33
+ assert last_response.ok?
34
+ end
35
+
36
+ it 'has SSE headers' do
37
+ get '/'
38
+
39
+ headers = {'content-type' => 'text/event-stream', 'cache-control' => 'no-cache'}
40
+ assert_equal headers, last_response.headers
41
+ end
42
+
43
+ it 'streams the body' do
44
+ get '/'
45
+
46
+ stream = Minitest::Mock.new
47
+ stream.expect(:write, nil, ["data: hola\n\n"])
48
+ stream.expect(:close, nil)
49
+ response_body = last_response.instance_variable_get(:@body)
50
+ assert_instance_of Proc, response_body
51
+
52
+ response_body.call(stream)
53
+
54
+ stream.verify
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: roda-sse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shannon Skipper
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2024-10-18 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: roda
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '3.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '3.0'
26
+ description: The roda-sse gem integrates simple SSE streaming into the roda web toolkit.
27
+ email: shannonskipper@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - Gemfile
33
+ - LICENSE
34
+ - README.md
35
+ - Rakefile
36
+ - lib/roda/plugins/sse.rb
37
+ - spec/roda-sse_spec.rb
38
+ homepage: https://github.com/havenwood/roda-sse
39
+ licenses:
40
+ - MIT
41
+ metadata:
42
+ rubygems_mfa_required: 'true'
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.6.0.dev
58
+ specification_version: 4
59
+ summary: SSE integration for Roda
60
+ test_files: []