event_router 0.2.0 → 0.3.0

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: 79dbcbada6f98ca97196267161c48f526c84e4003b9b17f08d802204e9f46772
4
- data.tar.gz: 7b6bbaeed68e1264329c5ef2e616fcccdab0792b95dfc180af466e1159829838
3
+ metadata.gz: 566c3d0916d6d0df641724d151596657765068ff3a97ae2c4778ca51b29d0378
4
+ data.tar.gz: 93eb6ac9f1554a8ee6bca437f81fca0cbe952b82678029bbcabc8cd6d145cbad
5
5
  SHA512:
6
- metadata.gz: 5ce10b55197712cf503960918fb7c6936ed334441a882eb374cbb50ca746d1b6d7a5287d8f4b33465e557aba62c85ef80103037dc286ad30277fe0a0d91a5769
7
- data.tar.gz: 5bddbd23394618c8518b0fb5c0dd50c8af1c645ac2e849f2e3006119594b52e31aa9f1ccb787b32846f10ff32fa48e46c33c10b6760222f36de7618d0c623d97
6
+ metadata.gz: 92129422792e53f5e89c0ea41c6bd7bc3cc3619ef949a510961687bef819a0a5f6723f64f70ecd775ebcd8d1e1f659132696143346a80ddb2110a655f590dedb
7
+ data.tar.gz: 9282ef8e6b1ed5b59dc1b66f81a192c6722eabe827aca1e668c923a3a43851ab8badd3079ddcbec19cf00b9b90aaff0a510f2ebaadf75237a7b46ce0c4554931
data/Gemfile.lock CHANGED
@@ -1,28 +1,28 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- event_router (0.1.0)
4
+ event_router (0.3.0)
5
5
  activesupport
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (6.0.3.2)
10
+ activesupport (6.1.1)
11
11
  concurrent-ruby (~> 1.0, >= 1.0.2)
12
- i18n (>= 0.7, < 2)
13
- minitest (~> 5.1)
14
- tzinfo (~> 1.1)
15
- zeitwerk (~> 2.2, >= 2.2.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
15
+ zeitwerk (~> 2.3)
16
16
  ast (2.4.1)
17
17
  byebug (11.1.3)
18
18
  coderay (1.1.3)
19
- concurrent-ruby (1.1.7)
19
+ concurrent-ruby (1.1.8)
20
20
  connection_pool (2.2.3)
21
21
  diff-lcs (1.3)
22
- i18n (1.8.5)
22
+ i18n (1.8.7)
23
23
  concurrent-ruby (~> 1.0)
24
24
  method_source (1.0.0)
25
- minitest (5.14.1)
25
+ minitest (5.14.3)
26
26
  oj (3.10.14)
27
27
  parallel (1.19.2)
28
28
  parser (2.7.1.4)
@@ -67,11 +67,10 @@ GEM
67
67
  connection_pool (>= 2.2.2)
68
68
  rack (~> 2.0)
69
69
  redis (>= 4.2.0)
70
- thread_safe (0.3.6)
71
- tzinfo (1.2.7)
72
- thread_safe (~> 0.1)
70
+ tzinfo (2.0.4)
71
+ concurrent-ruby (~> 1.0)
73
72
  unicode-display_width (1.7.0)
74
- zeitwerk (2.4.0)
73
+ zeitwerk (2.4.2)
75
74
 
76
75
  PLATFORMS
77
76
  ruby
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # EventRouter
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/event_router.svg)](https://badge.fury.io/rb/event_router)
3
4
  ![Specs](https://github.com/ahmad-elassuty/event_router/workflows/Specs/badge.svg)
4
5
 
5
6
  EventRouter helps you organise your application domain events in a simple and intuitive way.
@@ -2,8 +2,11 @@
2
2
 
3
3
  module Examples
4
4
  class OrderPlaced < EventRouter::Event
5
+ event_options queue: :orders, retry: false
6
+
5
7
  deliver_to :notifications,
6
- handler: Examples::Notifications
8
+ handler: Examples::Notifications,
9
+ queue: :notifications
7
10
 
8
11
  deliver_to :event_store,
9
12
  handler: Examples::EventStore::OrderPlaced,
@@ -1,3 +1,5 @@
1
1
  :concurrency: 1
2
2
  :queues:
3
3
  - [event_router, 1]
4
+ - [notifications, 2]
5
+ - [orders, 2]
@@ -6,12 +6,14 @@ module EventRouter
6
6
  module Sidekiq
7
7
  module_function
8
8
 
9
+ extend EventRouter::Helpers::Event
10
+
9
11
  def process_event(event, serialized_event: nil)
10
12
  serialized_event ||= EventRouter.serialize(event)
11
13
 
12
- options = EventRouter::DeliveryAdapters::Sidekiq.options
14
+ yield_destinations(event) do |destination, serialized_payload|
15
+ options = destination_options(destination, EventRouter::DeliveryAdapters::Sidekiq)
13
16
 
14
- Helpers::Deliver.yield_destinations(event) do |destination, serialized_payload|
15
17
  Workers::SidekiqDestinationDeliveryWorker
16
18
  .set(queue: options[:queue], retry: options[:retry])
17
19
  .perform_async(destination.name, serialized_event, serialized_payload)
@@ -2,11 +2,10 @@
2
2
 
3
3
  require 'sidekiq'
4
4
 
5
- require_relative '../serializer'
5
+ require 'event_router/serializer'
6
+ require 'event_router/helpers/event'
6
7
 
7
- require_relative 'helpers/deliver'
8
8
  require_relative 'helpers/sidekiq'
9
-
10
9
  require_relative 'workers/sidekiq_destination_delivery_worker'
11
10
  require_relative 'workers/sidekiq_event_delivery_worker'
12
11
 
@@ -29,7 +28,8 @@ module EventRouter
29
28
  end
30
29
 
31
30
  def deliver_async(event)
32
- serialized_event = EventRouter.serialize(event)
31
+ serialized_event = EventRouter.serialize(event)
32
+ options = EventRouter::Helpers::Event.event_options(event, self)
33
33
 
34
34
  Workers::SidekiqEventDeliveryWorker
35
35
  .set(queue: options[:queue], retry: options[:retry])
@@ -4,7 +4,8 @@ module EventRouter
4
4
  class Destination
5
5
  # Attributes
6
6
  attr_reader :name, :handler, :handler_method,
7
- :prefetch_payload, :payload_method
7
+ :prefetch_payload, :payload_method,
8
+ :options
8
9
 
9
10
  # Constants
10
11
  DEFAULT_ATTRIBUTES = {
@@ -22,9 +23,10 @@ module EventRouter
22
23
 
23
24
  @name = name
24
25
  @handler = handler
25
- @handler_method = opts[:handler_method]
26
- @prefetch_payload = opts[:prefetch_payload]
27
- @payload_method = opts[:payload_method] || "#{name}_payload"
26
+ @handler_method = opts.delete(:handler_method)
27
+ @prefetch_payload = opts.delete(:prefetch_payload)
28
+ @payload_method = opts.delete(:payload_method) || "#{name}_payload"
29
+ @options = opts
28
30
  end
29
31
 
30
32
  def process(event, payload)
@@ -12,6 +12,7 @@ module EventRouter
12
12
  attr_accessor :correlation_id
13
13
 
14
14
  class_attribute :destinations, default: {}, instance_writer: false
15
+ class_attribute :options, instance_writer: false
15
16
 
16
17
  def initialize(uid: SecureRandom.uuid, correlation_id: SecureRandom.uuid, created_at: Time.now, **payload)
17
18
  @uid = uid
@@ -45,6 +46,10 @@ module EventRouter
45
46
  destinations[name] = EventRouter::Destination.new(name, **opts)
46
47
  end
47
48
 
49
+ def event_options(opts)
50
+ self.options = opts
51
+ end
52
+
48
53
  def publish(**attrs)
49
54
  EventRouter.publish(new(**attrs))
50
55
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EventRouter
4
+ module Helpers
5
+ module Event
6
+ module_function
7
+
8
+ def yield_destinations(event)
9
+ event.destinations.each do |_name, destination|
10
+ if destination.prefetch_payload?
11
+ payload = destination.extra_payload(event)
12
+ serialized_payload = EventRouter.serialize(payload)
13
+ end
14
+
15
+ yield destination, serialized_payload if block_given?
16
+ end
17
+ end
18
+
19
+ def event_options(event, adapter)
20
+ return adapter.options unless event.options?
21
+
22
+ adapter.options.merge(event.options)
23
+ end
24
+
25
+ def destination_options(destination, adapter)
26
+ adapter.options.merge(destination.options)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EventRouter
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: event_router
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ahmad Elassuty
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-31 00:00:00.000000000 Z
11
+ date: 2021-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -143,7 +143,6 @@ files:
143
143
  - lib/event_router.rb
144
144
  - lib/event_router/configuration.rb
145
145
  - lib/event_router/delivery_adapters/base.rb
146
- - lib/event_router/delivery_adapters/helpers/deliver.rb
147
146
  - lib/event_router/delivery_adapters/helpers/sidekiq.rb
148
147
  - lib/event_router/delivery_adapters/sidekiq.rb
149
148
  - lib/event_router/delivery_adapters/sync.rb
@@ -154,6 +153,7 @@ files:
154
153
  - lib/event_router/errors/required_option_error.rb
155
154
  - lib/event_router/errors/unsupported_option_error.rb
156
155
  - lib/event_router/event.rb
156
+ - lib/event_router/helpers/event.rb
157
157
  - lib/event_router/publisher.rb
158
158
  - lib/event_router/serializer.rb
159
159
  - lib/event_router/serializers/base.rb
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module EventRouter
4
- module DeliveryAdapters
5
- module Helpers
6
- module Deliver
7
- module_function
8
-
9
- def yield_destinations(event)
10
- event.destinations.each do |_name, destination|
11
- if destination.prefetch_payload?
12
- payload = destination.extra_payload(event)
13
- serialized_payload = EventRouter.serialize(payload)
14
- end
15
-
16
- yield destination, serialized_payload
17
- end
18
- end
19
- end
20
- end
21
- end
22
- end