Class: Flu::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/flu-rails/event.rb

Constant Summary collapse

ROUTING_KEY_MAX_LENGTH =
255

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid, emitter, kind, name, data) ⇒ Event

Returns a new instance of Event.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/flu-rails/event.rb', line 10

def initialize(uuid, emitter, kind, name, data)
  raise ArgumentError, "uuid must not be nil"              if uuid.nil?
  raise ArgumentError, "emitter must not be nil nor empty" if emitter.nil? || emitter.length == 0
  raise ArgumentError, "kind must not be nil nor empty"    if kind.nil?    || kind.length == 0
  raise ArgumentError, "name must not be nil nor empty"    if name.nil?    || name.length == 0

  @meta = {
    id:        uuid,
    name:      name,
    emitter:   emitter,
    timestamp: Time.now.utc,
    kind:      kind,
    status:    :new
  }
  @data = data || {}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/flu-rails/event.rb', line 8

def data
  @data
end

Instance Method Details

#emitterObject



53
54
55
# File 'lib/flu-rails/event.rb', line 53

def emitter
  @meta[:emitter]
end

#idObject



41
42
43
# File 'lib/flu-rails/event.rb', line 41

def id
  @meta[:id]
end

#kindObject



61
62
63
# File 'lib/flu-rails/event.rb', line 61

def kind
  @meta[:kind]
end

#mark_as_replayedObject



49
50
51
# File 'lib/flu-rails/event.rb', line 49

def mark_as_replayed
  @meta[:status] = :replayed
end

#nameObject



65
66
67
# File 'lib/flu-rails/event.rb', line 65

def name
  @meta[:name]
end

#statusObject



69
70
71
# File 'lib/flu-rails/event.rb', line 69

def status
  @meta[:status]
end

#timestampObject



57
58
59
# File 'lib/flu-rails/event.rb', line 57

def timestamp
  @meta[:timestamp]
end

#timestamp=(new_timestamp) ⇒ Object



45
46
47
# File 'lib/flu-rails/event.rb', line 45

def timestamp=(new_timestamp)
  @meta[:timestamp] = new_timestamp
end

#to_json(options = nil) ⇒ Object



34
35
36
37
38
39
# File 'lib/flu-rails/event.rb', line 34

def to_json(options=nil)
  JSON.dump({
    meta: @meta,
    data: map_complex_object(@data)
  })
end

#to_routing_keyObject



29
30
31
32
# File 'lib/flu-rails/event.rb', line 29

def to_routing_key
  prefix = "#{@meta[:status]}.#{@meta[:emitter]}.#{@meta[:kind]}."
  "#{prefix}#{routing_key_name(prefix.length)}"
end