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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 67ffd7abceaf1b4e048cc8f02f668b682ea81f62fc7de5228a9f0d12abab1df8
4
- data.tar.gz: 1eb075bdec30a569726bc3f2a74c1ce427aef8ff0c3cbcee401a61c034e71ca6
3
+ metadata.gz: '085ac55f8fc68a8b8806570359e68ae9cba48a0c0cef83645bfb9f7133fb38ce'
4
+ data.tar.gz: 231685b5e7c8c53a3cb086bbdc8ef6728132a942a47b59e6ac5c1ca6560b2725
5
5
  SHA512:
6
- metadata.gz: 8cb4d6f1302a34cab267c836c1e0d81c54267607de7f24d350183ff05a79af7cc216a6f34bc96c0e1204f71cca1de744fde448590e99bd96a1ab45c15c8ceddf
7
- data.tar.gz: fecf573e39eb04317d756414349d94092f24a07210629a55347191ef74ec94fcc8079f8a4f38b6c58bf94be5c59fbd79a90a168a4925819ce771269ccae6e3da
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, context_class: MyContextClass
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
- DEFAULT_OPTIONS = {
35
- context_class: Context
36
- }
37
-
38
- def initialize(app, options = {})
34
+ def initialize(app, context = Context.new)
39
35
  @app = app
40
- @options = DEFAULT_OPTIONS.merge(options || {})
36
+ @context = context
41
37
  end
42
- attr_reader :options
38
+ attr_reader :context
43
39
 
44
40
  def call(env)
45
- env[RACK_ENV_KEY] ||= options[:context_class].h({}).tap{|c|
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 |event|
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 |event|
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
- event = stop_errors(self, "listen") do
127
- factor_event(body)
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,5 @@
1
+ module Startback
2
+ class Context
3
+ attr_accessor :engine
4
+ end # class Context
5
+ end # module Startback
@@ -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
@@ -20,14 +20,10 @@ module Startback
20
20
  end
21
21
  attr_reader :context, :type, :data
22
22
 
23
- def self.json(src, world = {})
23
+ def self.json(src, context)
24
24
  parsed = JSON.parse(src)
25
- context = if world[:context]
26
- world[:context]
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'
@@ -2,7 +2,7 @@ module Startback
2
2
  module Version
3
3
  MAJOR = 0
4
4
  MINOR = 12
5
- TINY = 0
5
+ TINY = 1
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
data/spec/spec_helper.rb CHANGED
@@ -10,3 +10,8 @@ end
10
10
  RSpec.configure do |c|
11
11
  c.include SpecHelpers
12
12
  end
13
+
14
+ class User
15
+ class Changed < Startback::Event
16
+ end
17
+ end
@@ -10,18 +10,18 @@ module Startback
10
10
  include Rack::Test::Methods
11
11
 
12
12
  def app
13
- opts = middleware_options
13
+ build_args = self.build_args
14
14
  Rack::Builder.new do
15
- use Middleware, opts
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 option' do
24
- let(:middleware_options){ nil }
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(:middleware_options){{
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 '/'
@@ -4,7 +4,7 @@ module Startback
4
4
  describe Event do
5
5
 
6
6
  subject{
7
- Event.new("user_changed", { "foo" => "bar" })
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": "user_changed",
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("user_changed", { "foo" => "bar" }, { "baz": "context" })
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": "user_changed",
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("user_changed")
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 in the world' do
53
- evt = Event.json(JSON_SRC, context: 12)
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
@@ -53,7 +53,7 @@ module Startback
53
53
 
54
54
  def app
55
55
  Rack::Builder.new do
56
- use Context::Middleware, context_class: MyContextWithErrorHandler
56
+ use Context::Middleware, MyContextWithErrorHandler.new
57
57
  use CatchAll
58
58
  run ->(env){ raise AnError, "Hello error" }
59
59
  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.0
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