pubsubstub 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38d41c098792d6b1b14c0a9dddab5dc0bf01eea0
4
- data.tar.gz: 5bdef86b12597f01c1e88e812c5dd2bb111f221c
3
+ metadata.gz: dfd7d496f0e2ce3d0a48c9bc7a88a96b53d14150
4
+ data.tar.gz: c4849cbfb57dc5f9415edd74beb2737592d35921
5
5
  SHA512:
6
- metadata.gz: b03ff4a5d49874b7b411bb11544cf55aefcbd01f8f0a756eddb881a87417db0c68efd0ee784fa6f482cdf795556df83092e4ca2141f44b9365a066816d3abf67
7
- data.tar.gz: 668ed2d0fc55ce0500da60ffefe9c28067ffaf5c631d9bef95ed0461ca9696770cef4c63f21a08e54f166ec073e3788f93506c523756c6601171c0b9db6eaed9
6
+ metadata.gz: b6a1b5013dbaef4a34fc7486f9f8095818d599a1083d0361c9e57a9b51698d92048eff0d524d924b6a9826e401583ba572a04c477ba2919c57812d34d02c1212
7
+ data.tar.gz: 818686cfa16835e0a430c830b4af65fc61820217bc08da80cd9b3128f69f2fa4d8e6828424dcc7b9aa2f2305f34b833744e83cf8e75e193a11587579ee1cecb4
data/.travis.yml CHANGED
@@ -2,8 +2,8 @@ language: ruby
2
2
  rvm:
3
3
  - 2.2.2
4
4
  gemfile:
5
- - gemfiles/Gemfile.sinatra-1.x
6
- - gemfiles/Gemfile.sinatra-2.x
5
+ - gemfiles/Gemfile.rack-1.x
6
+ - gemfiles/Gemfile.rack-2.x
7
7
  services:
8
8
  - redis-server
9
9
 
data/Gemfile CHANGED
@@ -3,11 +3,10 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in pubsubstub.gemspec
4
4
  gemspec
5
5
 
6
- gem 'sinatra', github: 'sinatra/sinatra', ref: 'a5da6fa82c46436f59ae482d07c1752ab908c852'
7
6
  gem "rspec", "3.1.0"
8
7
  gem "pry-byebug"
9
8
  gem "puma", "~> 3.4"
10
9
  gem "thin", "~> 1.6"
11
10
  gem "rack-test"
12
11
  gem "timecop"
13
- gem "rake", "~> 10.0"
12
+ gem "rake", "~> 10.0"
data/example/config.ru CHANGED
@@ -1,4 +1,3 @@
1
- p File.expand_path('../lib', __dir__)
2
1
  $LOAD_PATH.unshift(File.expand_path('../lib', __dir__))
3
2
  require 'pubsubstub'
4
3
 
@@ -1,6 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'sinatra', '< 2'
3
+ gem 'rack', '< 2'
4
4
  gem 'redis', '~> 3.0'
5
5
  gem "rspec", "3.1.0"
6
6
  gem "pry-byebug"
@@ -3,7 +3,7 @@ GEM
3
3
  specs:
4
4
  byebug (9.0.5)
5
5
  coderay (1.1.1)
6
- daemons (1.2.3)
6
+ daemons (1.2.4)
7
7
  diff-lcs (1.2.5)
8
8
  eventmachine (1.2.0.1)
9
9
  method_source (0.8.2)
@@ -14,10 +14,8 @@ GEM
14
14
  pry-byebug (3.4.0)
15
15
  byebug (~> 9.0)
16
16
  pry (~> 0.10)
17
- puma (3.5.0)
17
+ puma (3.6.0)
18
18
  rack (1.6.4)
19
- rack-protection (1.5.3)
20
- rack
21
19
  rack-test (0.6.3)
22
20
  rack (>= 1.0)
23
21
  rake (10.5.0)
@@ -34,16 +32,11 @@ GEM
34
32
  rspec-mocks (3.1.3)
35
33
  rspec-support (~> 3.1.0)
36
34
  rspec-support (3.1.2)
37
- sinatra (1.4.7)
38
- rack (~> 1.5)
39
- rack-protection (~> 1.4)
40
- tilt (>= 1.3, < 3)
41
35
  slop (3.6.0)
42
36
  thin (1.7.0)
43
37
  daemons (~> 1.0, >= 1.0.9)
44
38
  eventmachine (~> 1.0, >= 1.0.4)
45
39
  rack (>= 1, < 3)
46
- tilt (2.0.5)
47
40
  timecop (0.8.1)
48
41
 
49
42
  PLATFORMS
@@ -52,11 +45,11 @@ PLATFORMS
52
45
  DEPENDENCIES
53
46
  pry-byebug
54
47
  puma (~> 3.4)
48
+ rack (< 2)
55
49
  rack-test
56
50
  rake (~> 10.0)
57
51
  redis (~> 3.0)
58
52
  rspec (= 3.1.0)
59
- sinatra (< 2)
60
53
  thin (~> 1.6)
61
54
  timecop
62
55
 
@@ -1,6 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'sinatra', github: 'sinatra/sinatra', ref: 'a5da6fa82c46436f59ae482d07c1752ab908c852'
3
+ gem 'rack', '~> 2.0'
4
4
  gem 'redis', '~> 3.0'
5
5
  gem "rspec", "3.1.0"
6
6
  gem "pry-byebug"
@@ -1,25 +1,12 @@
1
- GIT
2
- remote: git://github.com/sinatra/sinatra.git
3
- revision: a5da6fa82c46436f59ae482d07c1752ab908c852
4
- ref: a5da6fa82c46436f59ae482d07c1752ab908c852
5
- specs:
6
- sinatra (2.0.0.pre.alpha)
7
- mustermann (~> 0.4)
8
- rack (~> 2.0)
9
- rack-protection (~> 1.5)
10
- tilt (~> 2.0)
11
-
12
1
  GEM
13
2
  remote: https://rubygems.org/
14
3
  specs:
15
4
  byebug (9.0.5)
16
5
  coderay (1.1.1)
17
- daemons (1.2.3)
6
+ daemons (1.2.4)
18
7
  diff-lcs (1.2.5)
19
8
  eventmachine (1.2.0.1)
20
9
  method_source (0.8.2)
21
- mustermann (0.4.0)
22
- tool (~> 0.2)
23
10
  pry (0.10.4)
24
11
  coderay (~> 1.1.0)
25
12
  method_source (~> 0.8.1)
@@ -27,10 +14,8 @@ GEM
27
14
  pry-byebug (3.4.0)
28
15
  byebug (~> 9.0)
29
16
  pry (~> 0.10)
30
- puma (3.5.0)
17
+ puma (3.6.0)
31
18
  rack (2.0.1)
32
- rack-protection (1.5.3)
33
- rack
34
19
  rack-test (0.6.3)
35
20
  rack (>= 1.0)
36
21
  rake (10.5.0)
@@ -52,9 +37,7 @@ GEM
52
37
  daemons (~> 1.0, >= 1.0.9)
53
38
  eventmachine (~> 1.0, >= 1.0.4)
54
39
  rack (>= 1, < 3)
55
- tilt (2.0.5)
56
40
  timecop (0.8.1)
57
- tool (0.2.3)
58
41
 
59
42
  PLATFORMS
60
43
  ruby
@@ -62,11 +45,11 @@ PLATFORMS
62
45
  DEPENDENCIES
63
46
  pry-byebug
64
47
  puma (~> 3.4)
48
+ rack (~> 2.0)
65
49
  rack-test
66
50
  rake (~> 10.0)
67
51
  redis (~> 3.0)
68
52
  rspec (= 3.1.0)
69
- sinatra!
70
53
  thin (~> 1.6)
71
54
  timecop
72
55
 
@@ -1,6 +1,22 @@
1
1
  module Pubsubstub
2
- class Application < Sinatra::Base
3
- use PublishAction
4
- use StreamAction
2
+ class Application
3
+ def initialize(*)
4
+ @publish = PublishAction.new
5
+ @stream = StreamAction.new
6
+ end
7
+
8
+ def self.call(env)
9
+ @instance ||= new
10
+ @instance.call(env)
11
+ end
12
+
13
+ def call(env)
14
+ request = Rack::Request.new(env)
15
+ if request.get?
16
+ @stream.call(env)
17
+ else
18
+ @publish.call(env)
19
+ end
20
+ end
5
21
  end
6
22
  end
@@ -1,10 +1,14 @@
1
1
  module Pubsubstub
2
- class PublishAction < Pubsubstub::Action
3
- post '/' do
4
- (params[:channels] || [:default]).each do |channel_name|
5
- Pubsubstub.publish(channel_name, params[:data], name: params[:event])
2
+ class PublishAction
3
+ def initialize(*)
4
+ end
5
+
6
+ def call(env)
7
+ request = Rack::Request.new(env)
8
+ channels = (request.params['channels'] || [:default]).each do |channel_name|
9
+ Pubsubstub.publish(channel_name, request.params['data'], name: request.params['event'])
6
10
  end
7
- ""
11
+ [200, {}, ['']]
8
12
  end
9
13
  end
10
14
  end
@@ -0,0 +1,25 @@
1
+ class Stream
2
+ def initialize(&callback)
3
+ @callback = callback
4
+ @closed = false
5
+ end
6
+
7
+ def close
8
+ @closed = true
9
+ end
10
+
11
+ def each(&front)
12
+ @front = front
13
+ @callback.call(self)
14
+ close
15
+ end
16
+
17
+ def <<(data)
18
+ @front.call(data.to_s)
19
+ self
20
+ end
21
+
22
+ def closed?
23
+ @closed
24
+ end
25
+ end
@@ -1,44 +1,39 @@
1
1
  module Pubsubstub
2
- class StreamAction < Pubsubstub::Action
2
+ class StreamAction
3
+ HEADERS = {
4
+ 'Content-Type' => 'text/event-stream',
5
+ 'Cache-Control' => 'no-cache',
6
+ 'X-Accel-Buffering' => 'no',
7
+ 'Connection' => 'keep-alive',
8
+ }.freeze
3
9
  include Logging
4
10
 
5
11
  def initialize(*)
6
- super
7
12
  @subscriptions = Set.new
8
13
  @mutex = Mutex.new
9
14
  end
10
15
 
11
- get '/', provides: 'text/event-stream' do
12
- status(200)
13
- headers({
14
- 'Cache-Control' => 'no-cache',
15
- 'X-Accel-Buffering' => 'no',
16
- 'Connection' => 'keep-alive',
17
- })
16
+ def call(env)
17
+ spawn_helper_threads
18
+ last_event_id = env['HTTP_LAST_EVENT_ID']
19
+ request = Rack::Request.new(env)
20
+ channels = (request.params['channels'] || [:default]).map(&Channel.method(:new))
18
21
 
19
- if event_machine?
20
- send_scrollback
22
+ stream = if event_machine?
23
+ send_scrollback(channels, last_event_id)
21
24
  else
22
- subscribe_connection
25
+ subscribe_connection(channels, last_event_id)
23
26
  end
24
- end
25
-
26
- def call(*)
27
- spawn_helper_threads
28
- super
27
+ [200, HEADERS, stream]
29
28
  end
30
29
 
31
30
  private
32
31
 
33
- def last_event_id
34
- request.env['HTTP_LAST_EVENT_ID']
35
- end
36
-
37
- def send_scrollback
32
+ def send_scrollback(channels, last_event_id)
38
33
  scrollback_events = []
39
34
  scrollback_events = channels.flat_map { |c| c.scrollback(since: last_event_id) } if last_event_id
40
35
  scrollback_events = [Pubsubstub.heartbeat_event] if scrollback_events.empty?
41
- stream do |connection|
36
+ Stream.new do |connection|
42
37
  scrollback_events.each do |event|
43
38
  connection << event.to_message
44
39
  end
@@ -49,12 +44,8 @@ module Pubsubstub
49
44
  defined?(EventMachine) && EventMachine.reactor_running?
50
45
  end
51
46
 
52
- def channels
53
- (params[:channels] || [:default]).map(&Channel.method(:new))
54
- end
55
-
56
- def subscribe_connection
57
- stream do |connection|
47
+ def subscribe_connection(channels, last_event_id)
48
+ Stream.new do |connection|
58
49
  subscription = register(channels, connection)
59
50
  begin
60
51
  subscription.stream(last_event_id)
@@ -1,3 +1,3 @@
1
1
  module Pubsubstub
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/pubsubstub.rb CHANGED
@@ -3,7 +3,7 @@ require "mutex_m"
3
3
  require "json"
4
4
  require "set"
5
5
 
6
- require "sinatra"
6
+ require "rack/request"
7
7
  require "redis"
8
8
  require "pubsubstub/version"
9
9
  require "pubsubstub/logging"
@@ -11,7 +11,7 @@ require "pubsubstub/channel"
11
11
  require "pubsubstub/subscriber"
12
12
  require "pubsubstub/subscription"
13
13
  require "pubsubstub/event"
14
- require "pubsubstub/action"
14
+ require "pubsubstub/stream"
15
15
  require "pubsubstub/stream_action"
16
16
  require "pubsubstub/publish_action"
17
17
  require "pubsubstub/application"
data/pubsubstub.gemspec CHANGED
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_dependency 'sinatra', '>= 1.4.7', '< 3'
22
- spec.add_dependency 'redis', '~> 3.0'
21
+ spec.add_dependency 'rack'
22
+ spec.add_dependency 'redis', "~> 3.0"
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.5"
25
25
  end
metadata CHANGED
@@ -1,35 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pubsubstub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guillaume Malette
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-19 00:00:00.000000000 Z
11
+ date: 2016-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: sinatra
14
+ name: rack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 1.4.7
20
- - - "<"
21
- - !ruby/object:Gem::Version
22
- version: '3'
19
+ version: '0'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - ">="
28
25
  - !ruby/object:Gem::Version
29
- version: 1.4.7
30
- - - "<"
31
- - !ruby/object:Gem::Version
32
- version: '3'
26
+ version: '0'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: redis
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -80,17 +74,17 @@ files:
80
74
  - example/Gemfile
81
75
  - example/config.ru
82
76
  - example/puma_config.rb
83
- - gemfiles/Gemfile.sinatra-1.x
84
- - gemfiles/Gemfile.sinatra-1.x.lock
85
- - gemfiles/Gemfile.sinatra-2.x
86
- - gemfiles/Gemfile.sinatra-2.x.lock
77
+ - gemfiles/Gemfile.rack-1.x
78
+ - gemfiles/Gemfile.rack-1.x.lock
79
+ - gemfiles/Gemfile.rack-2.x
80
+ - gemfiles/Gemfile.rack-2.x.lock
87
81
  - lib/pubsubstub.rb
88
- - lib/pubsubstub/action.rb
89
82
  - lib/pubsubstub/application.rb
90
83
  - lib/pubsubstub/channel.rb
91
84
  - lib/pubsubstub/event.rb
92
85
  - lib/pubsubstub/logging.rb
93
86
  - lib/pubsubstub/publish_action.rb
87
+ - lib/pubsubstub/stream.rb
94
88
  - lib/pubsubstub/stream_action.rb
95
89
  - lib/pubsubstub/subscriber.rb
96
90
  - lib/pubsubstub/subscription.rb
@@ -1,13 +0,0 @@
1
- module Pubsubstub
2
- class Action < Sinatra::Base
3
- configure :production, :development do
4
- enable :logging
5
- end
6
-
7
- configure :test do
8
- set :dump_errors, false
9
- set :raise_errors, true
10
- set :show_exceptions, false
11
- end
12
- end
13
- end