Class: Isimud::Event

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/isimud/event.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log, #logger

Constructor Details

#new(user, eventful, attributes) ⇒ Event #new(attributes) ⇒ Event

Initialize a new Event.

Overloads:

  • #new(user, eventful, attributes) ⇒ Event

    @param user user associated by the event @param eventful object associated with event @param parameters optional additional attributes

  • #new(attributes) ⇒ Event

    @param attributes event attributes

    Options Hash (attributes):

    • :user_id (Integer)

      ID of User associated with event

    • :eventful_type (String)

      type of object associated with event

    • :eventful_id (Integer)

      id of object associated with event

    • :exchange (String) — default: Isimud.events_exchange

      exchange for publishing event

    • :eventful (#id)

      object associated with event. This sets :eventful_type and :eventful_id based on the class and ID of the object.

    • :type (String, Symbol) — default: :model

      event type

    • :action (String, Symbol)

      event action

    • :occurred_at (Time) — default: Time.now

      date and time event occurred

    • :attributes (Hash)

      event attributes

    • :parameters (Hash)

      additional parameters (deprecated)



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)
  options = args.extract_options!.with_indifferent_access

  self.type        = options.delete(:type).try(:to_sym) || DEFAULT_TYPE
  self.exchange    = options.delete(:exchange)
  self.action      = options.delete(:action).try(:to_sym)
  self.user_id     = options.delete(:user_id)
  self.occurred_at = if (occurred = options.delete(:occurred_at))
                       occurred.kind_of?(String) ? Time.parse(occurred) : occurred
                     else
                       Time.now.utc
                     end
  @timestamp       = Time.now

  eventful_object = options.delete(:eventful)

  if args.length > 0
    self.parameters = options
    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 = options.delete(:eventful_type)
    self.eventful_id   = options.delete(:eventful_id)
  end
  self.attributes = options.delete(:attributes)
  self.parameters = options.delete(:parameters) || options
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



11
12
13
# File 'lib/isimud/event.rb', line 11

def action
  @action
end

#attributesObject

Returns the value of attribute attributes.



11
12
13
# File 'lib/isimud/event.rb', line 11

def attributes
  @attributes
end

#eventful_idObject

Returns the value of attribute eventful_id.



11
12
13
# File 'lib/isimud/event.rb', line 11

def eventful_id
  @eventful_id
end

#eventful_typeObject

Returns the value of attribute eventful_type.



11
12
13
# File 'lib/isimud/event.rb', line 11

def eventful_type
  @eventful_type
end

#exchangeObject



70
71
72
# File 'lib/isimud/event.rb', line 70

def exchange
  @exchange || Isimud.events_exchange
end

#occurred_atObject

Returns the value of attribute occurred_at.



11
12
13
# File 'lib/isimud/event.rb', line 11

def occurred_at
  @occurred_at
end

#parametersObject

Returns the value of attribute parameters.



11
12
13
# File 'lib/isimud/event.rb', line 11

def parameters
  @parameters
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



12
13
14
# File 'lib/isimud/event.rb', line 12

def timestamp
  @timestamp
end

#typeObject

Returns the value of attribute type.



11
12
13
# File 'lib/isimud/event.rb', line 11

def type
  @type
end

#user_idObject

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

.dispatchObject



117
118
119
# File 'lib/isimud/event.rb', line 117

def publish(*args)
  Event.new(*args).publish
end

.parse(data) ⇒ Object



109
110
111
# File 'lib/isimud/event.rb', line 109

def parse(data)
  Event.new(JSON.parse(data))
end

.publish(*args) ⇒ Object



113
114
115
# File 'lib/isimud/event.rb', line 113

def publish(*args)
  Event.new(*args).publish
end

Instance Method Details

#as_json(options = {}) ⇒ Hash

Return hash of data to be serialized to JSON

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :omit_parameters (Boolean)

    when set, do not include attributes or parameters in data

Returns:

  • (Hash)

    data to serialize



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(options = {})
  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 options[:omit_parameters]
    data[:parameters] = parameters
    data[:attributes] = attributes
  end
  data
end

#message_idObject

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 message_id
  [exchange, routing_key, user_id, timestamp.to_i, timestamp.nsec].join(':')
end

#publishObject



120
121
122
123
124
# File 'lib/isimud/event.rb', line 120

def publish
  data = self.serialize
  log "Event#publish: exchange #{exchange} message_id=#{message_id}"
  Isimud.client.publish(exchange, routing_key, data, message_id: message_id)
end

#routing_keyObject



74
75
76
# File 'lib/isimud/event.rb', line 74

def routing_key
  [type.to_s, eventful_type, eventful_id, action].compact.join('.')
end

#serializeObject



104
105
106
# File 'lib/isimud/event.rb', line 104

def serialize
  self.to_json
end