startback 0.12.0 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/startback/context/middleware.rb +5 -9
- data/lib/startback/event/agent.rb +4 -2
- data/lib/startback/event/bus/bunny/async.rb +2 -5
- data/lib/startback/event/engine.rb +5 -0
- data/lib/startback/event/ext/context.rb +5 -0
- data/lib/startback/event/ext/operation.rb +13 -0
- data/lib/startback/event.rb +5 -7
- data/lib/startback/version.rb +1 -1
- data/spec/spec_helper.rb +5 -0
- data/spec/unit/context/test_middleware.rb +6 -8
- data/spec/unit/test_event.rb +8 -18
- data/spec/unit/web/test_catch_all.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '085ac55f8fc68a8b8806570359e68ae9cba48a0c0cef83645bfb9f7133fb38ce'
|
4
|
+
data.tar.gz: 231685b5e7c8c53a3cb086bbdc8ef6728132a942a47b59e6ac5c1ca6560b2725
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c12830f86d5e0a067b801ebc752c894d7abec27c9745bb8eb307eee010a049275b49f3bfa1f55a1b7bf168c9fc3222b7039f6dc243f96c5268d369b625eabb3
|
7
|
+
data.tar.gz: dedb51337c356d9208ea727155c978dd6e937c2d8016189c17c606820975e81d1ede4e004a22878e8060dd58cd84280a4119f1a1996dae5c26bbdfc660009029
|
@@ -18,7 +18,7 @@ module Startback
|
|
18
18
|
#
|
19
19
|
# # Use a user defined context class
|
20
20
|
# Rack::Builder.new do
|
21
|
-
# use Startback::Context::Middleware,
|
21
|
+
# use Startback::Context::Middleware, MyContextClass.new
|
22
22
|
#
|
23
23
|
# run ->(env){
|
24
24
|
# ctx = env[Startback::Context::Middleware::RACK_ENV_KEY]
|
@@ -31,18 +31,14 @@ module Startback
|
|
31
31
|
|
32
32
|
RACK_ENV_KEY = 'SAMBACK_CONTEXT'
|
33
33
|
|
34
|
-
|
35
|
-
context_class: Context
|
36
|
-
}
|
37
|
-
|
38
|
-
def initialize(app, options = {})
|
34
|
+
def initialize(app, context = Context.new)
|
39
35
|
@app = app
|
40
|
-
@
|
36
|
+
@context = context
|
41
37
|
end
|
42
|
-
attr_reader :
|
38
|
+
attr_reader :context
|
43
39
|
|
44
40
|
def call(env)
|
45
|
-
env[RACK_ENV_KEY] ||=
|
41
|
+
env[RACK_ENV_KEY] ||= context.dup.tap{|c|
|
46
42
|
c.original_rack_env = env.dup
|
47
43
|
}
|
48
44
|
@app.call(env)
|
@@ -38,7 +38,8 @@ module Startback
|
|
38
38
|
#
|
39
39
|
# See Bus#listen
|
40
40
|
def async(exchange, queue)
|
41
|
-
bus.async.listen(exchange, queue) do |
|
41
|
+
bus.async.listen(exchange, queue) do |event_data|
|
42
|
+
event = engine.factor_event(event_data)
|
42
43
|
dup.call(event)
|
43
44
|
end
|
44
45
|
end
|
@@ -47,7 +48,8 @@ module Startback
|
|
47
48
|
#
|
48
49
|
# See Bus#listen
|
49
50
|
def sync(exchange, queue)
|
50
|
-
bus.listen(exchange, queue) do |
|
51
|
+
bus.listen(exchange, queue) do |event_data|
|
52
|
+
event = engine.factor_event(event_data)
|
51
53
|
dup.call(event)
|
52
54
|
end
|
53
55
|
end
|
@@ -123,11 +123,8 @@ module Startback
|
|
123
123
|
queue = channel.queue((processor || "main").to_s, queue_options)
|
124
124
|
queue.bind(fanout)
|
125
125
|
queue.subscribe do |delivery_info, properties, body|
|
126
|
-
|
127
|
-
|
128
|
-
end
|
129
|
-
stop_errors(self, "listen", event.context) do
|
130
|
-
(listener || bl).call(event)
|
126
|
+
stop_errors(self, "listen") do
|
127
|
+
(listener || bl).call(body)
|
131
128
|
end
|
132
129
|
end
|
133
130
|
end
|
@@ -36,6 +36,7 @@ module Startback
|
|
36
36
|
def initialize(options = {}, context = Context.new)
|
37
37
|
@options = DEFAULT_OPTIONS.merge(options)
|
38
38
|
@context = context
|
39
|
+
@context.engine = self
|
39
40
|
end
|
40
41
|
attr_reader :options, :context
|
41
42
|
|
@@ -84,6 +85,10 @@ module Startback
|
|
84
85
|
.each { |klass| klass.new(self) }
|
85
86
|
end
|
86
87
|
|
88
|
+
def factor_event(event_data)
|
89
|
+
Event.json(event_data, context.dup)
|
90
|
+
end
|
91
|
+
|
87
92
|
class Runner
|
88
93
|
|
89
94
|
DEFAULT_SERVER_ENGINE_OPTIONS = {
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Startback
|
2
|
+
class Operation
|
3
|
+
|
4
|
+
def self.emits(type, &bl)
|
5
|
+
after_call do
|
6
|
+
event_data = instance_exec(&bl)
|
7
|
+
event = type.new(type.to_s, event_data, context)
|
8
|
+
context.engine.bus.emit(event)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end # class Operation
|
13
|
+
end # module Startback
|
data/lib/startback/event.rb
CHANGED
@@ -20,14 +20,10 @@ module Startback
|
|
20
20
|
end
|
21
21
|
attr_reader :context, :type, :data
|
22
22
|
|
23
|
-
def self.json(src,
|
23
|
+
def self.json(src, context)
|
24
24
|
parsed = JSON.parse(src)
|
25
|
-
|
26
|
-
|
27
|
-
elsif world[:context_factory]
|
28
|
-
world[:context_factory].call(parsed)
|
29
|
-
end
|
30
|
-
Event.new(parsed['type'], parsed['data'], context)
|
25
|
+
klass = Kernel.const_get(parsed['type'])
|
26
|
+
klass.new(parsed['type'], parsed['data'], context)
|
31
27
|
end
|
32
28
|
|
33
29
|
def to_json(*args, &bl)
|
@@ -41,6 +37,8 @@ module Startback
|
|
41
37
|
|
42
38
|
end # class Event
|
43
39
|
end # module Startback
|
40
|
+
require_relative 'event/ext/context'
|
41
|
+
require_relative 'event/ext/operation'
|
44
42
|
require_relative 'event/agent'
|
45
43
|
require_relative 'event/bus'
|
46
44
|
require_relative 'event/engine'
|
data/lib/startback/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -10,18 +10,18 @@ module Startback
|
|
10
10
|
include Rack::Test::Methods
|
11
11
|
|
12
12
|
def app
|
13
|
-
|
13
|
+
build_args = self.build_args
|
14
14
|
Rack::Builder.new do
|
15
|
-
use Middleware,
|
15
|
+
use Middleware, *build_args
|
16
16
|
run ->(env){
|
17
|
-
ctx = env[Startback::Context::Middleware::RACK_ENV_KEY]
|
17
|
+
ctx = env[Startback::Context::Middleware::RACK_ENV_KEY]
|
18
18
|
[200, {}, ctx.class.to_s]
|
19
19
|
}
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
|
-
context 'when used without
|
24
|
-
let(:
|
23
|
+
context 'when used without context' do
|
24
|
+
let(:build_args){ [] }
|
25
25
|
|
26
26
|
it 'sets the default context class' do
|
27
27
|
get '/'
|
@@ -31,9 +31,7 @@ module Startback
|
|
31
31
|
end
|
32
32
|
|
33
33
|
context 'when specifying the context class' do
|
34
|
-
let(:
|
35
|
-
context_class: MyContextSubClass
|
36
|
-
}}
|
34
|
+
let(:build_args){ [MyContextSubClass.new] }
|
37
35
|
|
38
36
|
it 'sets the default context class' do
|
39
37
|
get '/'
|
data/spec/unit/test_event.rb
CHANGED
@@ -4,7 +4,7 @@ module Startback
|
|
4
4
|
describe Event do
|
5
5
|
|
6
6
|
subject{
|
7
|
-
Event.new("
|
7
|
+
Event.new("User::Changed", { "foo" => "bar" })
|
8
8
|
}
|
9
9
|
|
10
10
|
it 'presents an ostruct on top of its data' do
|
@@ -15,7 +15,7 @@ module Startback
|
|
15
15
|
|
16
16
|
JSON_SRC = <<-JSON.gsub(/\s+/, "")
|
17
17
|
{
|
18
|
-
"type": "
|
18
|
+
"type": "User::Changed",
|
19
19
|
"data": {
|
20
20
|
"foo": "bar"
|
21
21
|
}
|
@@ -27,10 +27,10 @@ module Startback
|
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'has a to_json that dumps the context if any' do
|
30
|
-
evt = Event.new("
|
30
|
+
evt = Event.new("User::Changed", { "foo" => "bar" }, { "baz": "context" })
|
31
31
|
expect(evt.to_json).to eql(<<-JSON.gsub(/\s+/, ""))
|
32
32
|
{
|
33
|
-
"type": "
|
33
|
+
"type": "User::Changed",
|
34
34
|
"data": {
|
35
35
|
"foo": "bar"
|
36
36
|
},
|
@@ -43,26 +43,16 @@ module Startback
|
|
43
43
|
|
44
44
|
|
45
45
|
it 'has a json class method that works as expected' do
|
46
|
-
evt = Event.json(JSON_SRC)
|
46
|
+
evt = Event.json(JSON_SRC, nil)
|
47
47
|
expect(evt).to be_a(Event)
|
48
|
-
expect(evt.type).to eql("
|
48
|
+
expect(evt.type).to eql("User::Changed")
|
49
49
|
expect(evt.data).to eql(subject.data)
|
50
50
|
end
|
51
51
|
|
52
|
-
it 'accepts an explicit context
|
53
|
-
evt = Event.json(JSON_SRC,
|
52
|
+
it 'accepts an explicit context as second argument' do
|
53
|
+
evt = Event.json(JSON_SRC, 12)
|
54
54
|
expect(evt.context).to eql(12)
|
55
55
|
end
|
56
|
-
|
57
|
-
it 'accepts an context factory in the world' do
|
58
|
-
cf = ->(arg) {
|
59
|
-
expect(arg).to eql(JSON.parse(JSON_SRC))
|
60
|
-
12
|
61
|
-
}
|
62
|
-
evt = Event.json(JSON_SRC, context_factory: cf)
|
63
|
-
expect(evt.context).to eql(12)
|
64
|
-
end
|
65
|
-
|
66
56
|
end
|
67
57
|
|
68
58
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: startback
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.12.
|
4
|
+
version: 0.12.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bernard Lambeau
|
@@ -400,6 +400,8 @@ files:
|
|
400
400
|
- lib/startback/event/bus/memory/async.rb
|
401
401
|
- lib/startback/event/bus/memory/sync.rb
|
402
402
|
- lib/startback/event/engine.rb
|
403
|
+
- lib/startback/event/ext/context.rb
|
404
|
+
- lib/startback/event/ext/operation.rb
|
403
405
|
- lib/startback/ext.rb
|
404
406
|
- lib/startback/ext/date_time.rb
|
405
407
|
- lib/startback/ext/time.rb
|