Class: Isimud::Event
Overview
A structured message format useful for processing events. Note that each message has a routing key automatically constructed based on four properties: type.eventful_type.eventful_id.action Any blank or nil properties are omitted from the routing key. For convenience, you may construct an event using an eventful object, which sets the eventful_type and eventful_id
Constant Summary collapse
- DEFAULT_TYPE =
:model
Instance Attribute Summary collapse
-
#action ⇒ Object
Returns the value of attribute action.
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#eventful_id ⇒ Object
Returns the value of attribute eventful_id.
-
#eventful_type ⇒ Object
Returns the value of attribute eventful_type.
- #exchange ⇒ Object
-
#occurred_at ⇒ Object
Returns the value of attribute occurred_at.
-
#parameters ⇒ Object
Returns the value of attribute parameters.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#type ⇒ Object
Returns the value of attribute type.
-
#user_id ⇒ Object
Returns the value of attribute user_id.
Class Method Summary collapse
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Hash
Return hash of data to be serialized to JSON.
-
#initialize(*args) ⇒ Event
constructor
Initialize a new Event.
-
#message_id ⇒ Object
Message ID, which is generated from the exchange, routing_key, user_id, and timestamp.
- #publish ⇒ Object
- #routing_key ⇒ Object
- #serialize ⇒ Object
Methods included from Logging
Constructor Details
#new(user, eventful, attributes) ⇒ Event #new(attributes) ⇒ Event
Initialize a new Event.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/isimud/event.rb', line 35 def initialize(*args) = args..with_indifferent_access self.type = .delete(:type).try(:to_sym) || DEFAULT_TYPE self.exchange = .delete(:exchange) self.action = .delete(:action).try(:to_sym) self.user_id = .delete(:user_id) self.occurred_at = if (occurred = .delete(:occurred_at)) occurred.kind_of?(String) ? Time.parse(occurred) : occurred else Time.now.utc end @timestamp = Time.now eventful_object = .delete(:eventful) if args.length > 0 self.parameters = if (user = args.shift) self.user_id = user.id end eventful_object ||= args.shift end if eventful_object self.eventful_type = eventful_object.class.base_class.name self.eventful_id = eventful_object.id else self.eventful_type = .delete(:eventful_type) self.eventful_id = .delete(:eventful_id) end self.attributes = .delete(:attributes) self.parameters = .delete(:parameters) || end |
Instance Attribute Details
#action ⇒ Object
Returns the value of attribute action.
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def action @action end |
#attributes ⇒ Object
Returns the value of attribute attributes.
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def attributes @attributes end |
#eventful_id ⇒ Object
Returns the value of attribute eventful_id.
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def eventful_id @eventful_id end |
#eventful_type ⇒ Object
Returns the value of attribute eventful_type.
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def eventful_type @eventful_type end |
#exchange ⇒ Object
70 71 72 |
# File 'lib/isimud/event.rb', line 70 def exchange @exchange || Isimud.events_exchange end |
#occurred_at ⇒ Object
Returns the value of attribute occurred_at.
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def occurred_at @occurred_at end |
#parameters ⇒ Object
Returns the value of attribute parameters.
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def parameters @parameters end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
12 13 14 |
# File 'lib/isimud/event.rb', line 12 def @timestamp end |
#type ⇒ Object
Returns the value of attribute type.
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def type @type end |
#user_id ⇒ Object
Returns the value of attribute user_id.
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def user_id @user_id end |
Class Method Details
.dispatch ⇒ Object
117 118 119 |
# File 'lib/isimud/event.rb', line 117 def publish(*args) Event.new(*args).publish end |
Instance Method Details
#as_json(options = {}) ⇒ Hash
Return hash of data to be serialized to JSON
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/isimud/event.rb', line 87 def as_json( = {}) session_id = parameters.delete(:session_id) || Thread.current[:keas_session_id] data = {type: type, action: action, user_id: user_id, occurred_at: occurred_at, eventful_type: eventful_type, eventful_id: eventful_id, session_id: session_id} unless [:omit_parameters] data[:parameters] = parameters data[:attributes] = attributes end data end |
#message_id ⇒ Object
Message ID, which is generated from the exchange, routing_key, user_id, and timestamp. This is practically guaranteed to be unique across all publishers.
80 81 82 |
# File 'lib/isimud/event.rb', line 80 def [exchange, routing_key, user_id, .to_i, .nsec].join(':') end |
#publish ⇒ Object
120 121 122 123 124 |
# File 'lib/isimud/event.rb', line 120 def publish data = self.serialize log "Event#publish: exchange #{exchange} message_id=#{}" Isimud.client.publish(exchange, routing_key, data, message_id: ) end |
#routing_key ⇒ Object
74 75 76 |
# File 'lib/isimud/event.rb', line 74 def routing_key [type.to_s, eventful_type, eventful_id, action].compact.join('.') end |
#serialize ⇒ Object
104 105 106 |
# File 'lib/isimud/event.rb', line 104 def serialize self.to_json end |