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
- DEFAULT_TYPE =
:model
Instance Attribute Summary (collapse)
-
- (Object) action
Returns the value of attribute action.
-
- (Object) attributes
Returns the value of attribute attributes.
-
- (Object) eventful_id
Returns the value of attribute eventful_id.
-
- (Object) eventful_type
Returns the value of attribute eventful_type.
- - (Object) exchange
-
- (Object) occurred_at
Returns the value of attribute occurred_at.
-
- (Object) parameters
Returns the value of attribute parameters.
-
- (Object) type
Returns the value of attribute type.
-
- (Object) user_id
Returns the value of attribute user_id.
Class Method Summary (collapse)
Instance Method Summary (collapse)
-
- (Hash) as_json(options = {})
Return hash of data to be serialized to JSON.
-
- (Event) initialize(*args)
constructor
Initialize a new Event.
- - (Object) publish
- - (Object) routing_key
- - (Object) serialize
Methods included from Logging
Constructor Details
- (Event) new(user, eventful, attributes) - (Event) new(attributes)
Initialize a new Event.
34 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 |
# File 'lib/isimud/event.rb', line 34 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 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
- (Object) action
Returns the value of attribute action
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def action @action end |
- (Object) attributes
Returns the value of attribute attributes
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def attributes @attributes end |
- (Object) eventful_id
Returns the value of attribute eventful_id
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def eventful_id @eventful_id end |
- (Object) eventful_type
Returns the value of attribute eventful_type
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def eventful_type @eventful_type end |
- (Object) exchange
68 69 70 |
# File 'lib/isimud/event.rb', line 68 def exchange @exchange || Isimud.events_exchange end |
- (Object) occurred_at
Returns the value of attribute occurred_at
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def occurred_at @occurred_at end |
- (Object) parameters
Returns the value of attribute parameters
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def parameters @parameters end |
- (Object) type
Returns the value of attribute type
11 12 13 |
# File 'lib/isimud/event.rb', line 11 def type @type end |
- (Object) user_id
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
+ (Object) dispatch
109 110 111 |
# File 'lib/isimud/event.rb', line 109 def publish(*args) Event.new(*args).publish end |
+ (Object) parse(data)
101 102 103 |
# File 'lib/isimud/event.rb', line 101 def parse(data) Event.new(JSON.parse(data)) end |
+ (Object) publish(*args)
105 106 107 |
# File 'lib/isimud/event.rb', line 105 def publish(*args) Event.new(*args).publish end |
Instance Method Details
- (Hash) as_json(options = {})
Return hash of data to be serialized to JSON
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/isimud/event.rb', line 79 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 |
- (Object) publish
112 113 114 115 116 |
# File 'lib/isimud/event.rb', line 112 def publish data = self.serialize log "Event#publish: #{self.inspect}" Isimud.client.publish(exchange, routing_key, data) end |
- (Object) routing_key
72 73 74 |
# File 'lib/isimud/event.rb', line 72 def routing_key [type.to_s, eventful_type, eventful_id, action].compact.join('.') end |
- (Object) serialize
96 97 98 |
# File 'lib/isimud/event.rb', line 96 def serialize self.to_json end |