servus 1.3.0 → 1.3.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/servus/config.rb +30 -0
- data/lib/servus/event.rb +4 -0
- data/lib/servus/events/emitter.rb +6 -2
- data/lib/servus/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1ed0c555ab8879d5f5e32ecfed8109214468bbd3f9a397a4d6b1a79df51cc2e8
|
|
4
|
+
data.tar.gz: e518983727d4fad53fe08ef7863692cfa06c002b2a4a9248b38b8a25c3f2371d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: ddece449340240ced0d0df59d9c1eb9ed79e5e03ffd8ffd6cb5a402dd8d206d13489ef9da9d7e7b49dc98d2b2abff5cb78f235ccfdda6d085abfa96f36b4fb14
|
|
7
|
+
data.tar.gz: ffc7bd55d38b027005eb89187b4987776e546a9bb85d33c2566848704d1f787fb2e985700b921a57add2356ce67e9f877985fdae7d86fc52767055c2ecdb05b9
|
data/lib/servus/config.rb
CHANGED
|
@@ -90,6 +90,36 @@ module Servus
|
|
|
90
90
|
# end
|
|
91
91
|
attr_accessor :logger
|
|
92
92
|
|
|
93
|
+
# What to add to every event payload, beside what each event is about.
|
|
94
|
+
#
|
|
95
|
+
# A callable given the payload as built, the event's name and the service
|
|
96
|
+
# emitting it (+nil+ for {Servus::Event.emit}), returning the payload to
|
|
97
|
+
# validate and dispatch. Applied on both emission paths, before the payload
|
|
98
|
+
# is validated, so an Event's schema can require what it adds. Use it for
|
|
99
|
+
# what every event should say the same way, such as who caused it, rather
|
|
100
|
+
# than having each service build it into its own payloads.
|
|
101
|
+
#
|
|
102
|
+
# @return [#call, nil] the enricher, or nil to emit payloads as built
|
|
103
|
+
#
|
|
104
|
+
# @example Say who caused every event
|
|
105
|
+
# config.event_payload_enricher = lambda do |payload, event_name:, service:|
|
|
106
|
+
# payload.merge(meta: { actor: Current.user&.id })
|
|
107
|
+
# end
|
|
108
|
+
attr_accessor :event_payload_enricher
|
|
109
|
+
|
|
110
|
+
# The payload an event is emitted with: as built, or as the configured
|
|
111
|
+
# {#event_payload_enricher} returns it.
|
|
112
|
+
#
|
|
113
|
+
# @param payload [Hash] the payload as built
|
|
114
|
+
# @param event_name [Symbol] the event being emitted
|
|
115
|
+
# @param service [Servus::Base, nil] the service emitting it, if one is
|
|
116
|
+
# @return [Hash] the payload to validate and dispatch
|
|
117
|
+
def enrich_event_payload(payload, event_name:, service: nil)
|
|
118
|
+
return payload unless event_payload_enricher
|
|
119
|
+
|
|
120
|
+
event_payload_enricher.call(payload, event_name:, service:)
|
|
121
|
+
end
|
|
122
|
+
|
|
93
123
|
# The ordered list of routers that resolve invocations for events.
|
|
94
124
|
#
|
|
95
125
|
# The Bus iterates routers in order, collects invocations, deduplicates
|
data/lib/servus/event.rb
CHANGED
|
@@ -202,6 +202,9 @@ module Servus
|
|
|
202
202
|
#
|
|
203
203
|
# Provides a type-safe, discoverable way to emit events from anywhere in
|
|
204
204
|
# the application (controllers, jobs, rake tasks) without creating a service.
|
|
205
|
+
# The payload is given to the configured
|
|
206
|
+
# {Servus::Config#event_payload_enricher}, with no service, before it is
|
|
207
|
+
# validated.
|
|
205
208
|
#
|
|
206
209
|
# @param payload [Hash] the event payload
|
|
207
210
|
# @return [void]
|
|
@@ -226,6 +229,7 @@ module Servus
|
|
|
226
229
|
def emit(payload)
|
|
227
230
|
raise 'No event configured. Call event_name :name first.' unless @event_name
|
|
228
231
|
|
|
232
|
+
payload = Servus.config.enrich_event_payload(payload, event_name: @event_name)
|
|
229
233
|
Servus::Support::Validator.validate_event_payload!(self, payload)
|
|
230
234
|
|
|
231
235
|
Servus::Events::Bus.emit(@event_name, payload)
|
|
@@ -146,7 +146,9 @@ module Servus
|
|
|
146
146
|
end
|
|
147
147
|
end
|
|
148
148
|
|
|
149
|
-
# Emits events for a specific trigger with the given result.
|
|
149
|
+
# Emits events for a specific trigger with the given result. Each payload
|
|
150
|
+
# is built, then given to the configured
|
|
151
|
+
# {Servus::Config#event_payload_enricher}, then validated and dispatched.
|
|
150
152
|
#
|
|
151
153
|
# @param trigger [Symbol] the trigger type (:success, :failure, :error!)
|
|
152
154
|
# @param result [Servus::Support::Response] the service result
|
|
@@ -156,7 +158,9 @@ module Servus
|
|
|
156
158
|
self.class.emissions_for(trigger).each do |emission|
|
|
157
159
|
next unless emission_condition_met?(emission, result)
|
|
158
160
|
|
|
159
|
-
payload = build_event_payload(emission, result)
|
|
161
|
+
payload = Servus.config.enrich_event_payload(build_event_payload(emission, result),
|
|
162
|
+
event_name: emission[:event_name],
|
|
163
|
+
service: self)
|
|
160
164
|
validate_event_payload!(emission[:event_name], payload)
|
|
161
165
|
Servus::Events::Bus.emit(emission[:event_name], payload)
|
|
162
166
|
end
|
data/lib/servus/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: servus
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.3.
|
|
4
|
+
version: 1.3.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sebastian Scholl
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-04 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|