Class: Happn::Event
- Inherits:
-
Object
- Object
- Happn::Event
- Defined in:
- lib/happn/event.rb
Overview
A single CREPE event, as it was consumed from the exchange.
An event is built from the parsed payload of a message. That payload carries
two entries: meta, describing the event itself, and data, describing what
happened. A projector handler receives an instance of this class.
Key conversion
Every key of the payload, at every depth, is converted into an underscored Symbol when the event is built. A payload emitted in camel case is therefore read in snake case, and always through symbols:
# {"meta" => {…}, "data" => {"requestMetadata" => {"controllerName" => "countries"}}}
event.data[:request_metadata][:controller_name] # => "countries"
event.data["request_metadata"] # => nil
Dashes become underscores, :: becomes /, and the capitals of an acronym
are kept together: "HTTPResponseCode" is read as :http_response_code.
Changes
An entity change carries a changes entry, mapping an attribute to the pair
of values it went through:
event.changes # => { name: ["France", "Belgium"] }
Events of another shape carry no such entry at all, a request for instance.
#changes then returns nil, and the five methods reading through it raise
a NoMethodError: guard them with #changes when a handler may be reached by
events of several shapes.
Instance Attribute Summary collapse
-
#data ⇒ Hash
readonly
The whole
dataentry of the payload, keys converted.
Instance Method Summary collapse
-
#add_change(name, value) ⇒ Array
Records a change on an attribute.
-
#associations ⇒ Hash?
The entities the event relates to.
-
#change_after(attribute_name) ⇒ Object?
The value an attribute was changed to.
-
#change_before(attribute_name) ⇒ Object?
The value an attribute was changed from.
-
#changes ⇒ Hash{Symbol => Array}?
The attributes the event changed, each mapped to its before and after values.
-
#changes=(new_changes) ⇒ Hash{Symbol => Array}
Replaces the whole set of changes.
-
#delete_change(attribute_name) ⇒ Array?
Drops the change recorded on an attribute.
-
#emitter ⇒ String?
The application the event comes from, matched by the
emitterof a query. -
#has_change?(attribute_name) ⇒ Boolean
Whether an attribute changed.
-
#id ⇒ String?
The identifier the emitter gave the event.
-
#initialize(args) ⇒ Event
constructor
Builds an event from a parsed payload.
-
#kind ⇒ String?
The category of the event, matched by the
kindof a query. -
#name ⇒ String?
What the event says happened, matched by the
nameof a query. -
#request_metadata ⇒ Hash?
The metadata the emitter attached to the request behind the event.
-
#status ⇒ String?
The state of the event, matched by the
statusof a query. -
#timestamp ⇒ DateTime?
When the event was emitted.
-
#user_metadata ⇒ Hash?
The metadata the emitter attached to the user behind the event.
Constructor Details
#initialize(args) ⇒ Event
Builds an event from a parsed payload.
55 56 57 58 |
# File 'lib/happn/event.rb', line 55 def initialize(args) @meta = deep_underscore_keys(args.fetch("meta")) @data = deep_underscore_keys(args.fetch("data")) end |
Instance Attribute Details
#data ⇒ Hash (readonly)
The whole data entry of the payload, keys converted.
This is the hash the event holds, not a copy: #changes= and #add_change write into it, and so does anything the caller does to it.
48 49 50 |
# File 'lib/happn/event.rb', line 48 def data @data end |
Instance Method Details
#add_change(name, value) ⇒ Array
Records a change on an attribute.
The "before" value is always nil: the method describes a value that was
set, not a transition. An empty String is normalized into nil, so that a
blank emitted value and an absent one are recorded alike.
109 110 111 112 |
# File 'lib/happn/event.rb', line 109 def add_change(name, value) new_value = value == "" ? nil : value changes[name.to_sym] = [nil, new_value] end |
#associations ⇒ Hash?
The entities the event relates to.
117 118 119 |
# File 'lib/happn/event.rb', line 117 def associations @data[:associations] end |
#change_after(attribute_name) ⇒ Object?
The value an attribute was changed to.
178 179 180 |
# File 'lib/happn/event.rb', line 178 def change_after(attribute_name) changes[attribute_name.to_sym]&.last end |
#change_before(attribute_name) ⇒ Object?
The value an attribute was changed from.
187 188 189 |
# File 'lib/happn/event.rb', line 187 def change_before(attribute_name) changes[attribute_name.to_sym]&.first end |
#changes ⇒ Hash{Symbol => Array}?
The attributes the event changed, each mapped to its before and after values.
80 81 82 |
# File 'lib/happn/event.rb', line 80 def changes @data[:changes] end |
#changes=(new_changes) ⇒ Hash{Symbol => Array}
Replaces the whole set of changes.
Keys are taken as they are given: unlike the ones read from the payload, they go through no conversion.
91 92 93 |
# File 'lib/happn/event.rb', line 91 def changes=(new_changes) @data[:changes] = new_changes end |
#delete_change(attribute_name) ⇒ Array?
Drops the change recorded on an attribute.
206 207 208 |
# File 'lib/happn/event.rb', line 206 def delete_change(attribute_name) changes.delete(attribute_name.to_sym) end |
#emitter ⇒ String?
The application the event comes from, matched by the emitter of a query.
169 170 171 |
# File 'lib/happn/event.rb', line 169 def emitter @meta[:emitter] end |
#has_change?(attribute_name) ⇒ Boolean
Whether an attribute changed.
196 197 198 |
# File 'lib/happn/event.rb', line 196 def has_change?(attribute_name) !changes[attribute_name.to_sym].nil? end |
#id ⇒ String?
The identifier the emitter gave the event.
141 142 143 |
# File 'lib/happn/event.rb', line 141 def id @meta[:id] end |
#kind ⇒ String?
The category of the event, matched by the kind of a query.
162 163 164 |
# File 'lib/happn/event.rb', line 162 def kind @meta[:kind] end |
#name ⇒ String?
What the event says happened, matched by the name of a query.
148 149 150 |
# File 'lib/happn/event.rb', line 148 def name @meta[:name] end |
#request_metadata ⇒ Hash?
The metadata the emitter attached to the request behind the event.
70 71 72 |
# File 'lib/happn/event.rb', line 70 def @data[:request_metadata] end |
#status ⇒ String?
The state of the event, matched by the status of a query.
155 156 157 |
# File 'lib/happn/event.rb', line 155 def status @meta[:status] end |
#timestamp ⇒ DateTime?
When the event was emitted.
The raw value is parsed on every call, and its offset is kept as it was emitted rather than being normalized.
129 130 131 132 133 134 135 136 |
# File 'lib/happn/event.rb', line 129 def = @meta[:timestamp] if .nil? || .to_s.strip.empty? nil else DateTime.parse() end end |
#user_metadata ⇒ Hash?
The metadata the emitter attached to the user behind the event.
63 64 65 |
# File 'lib/happn/event.rb', line 63 def @data[:user_metadata] end |