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

DEFAULT_TYPE =
:model

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Logging

#log, #logger

Constructor Details

- (Event) new(user, eventful, attributes) - (Event) new(attributes)

Initialize a new Event.

Overloads:

  • - (Event) new(user, eventful, attributes)

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

  • - (Event) new(attributes)

    @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)



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)
  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

  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

- (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

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



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(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

- (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