Class: Pgbus::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/pgbus/event.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_id:, payload:, published_at: nil, routing_key: nil, headers: nil) ⇒ Event

Returns a new instance of Event.



9
10
11
12
13
14
15
# File 'lib/pgbus/event.rb', line 9

def initialize(event_id:, payload:, published_at: nil, routing_key: nil, headers: nil)
  @event_id = event_id
  @payload = payload
  @published_at = published_at || Time.now.utc
  @routing_key = routing_key
  @headers = headers || {}
end

Instance Attribute Details

#event_idObject (readonly)

Returns the value of attribute event_id.



7
8
9
# File 'lib/pgbus/event.rb', line 7

def event_id
  @event_id
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/pgbus/event.rb', line 7

def headers
  @headers
end

#payloadObject (readonly)

Returns the value of attribute payload.



7
8
9
# File 'lib/pgbus/event.rb', line 7

def payload
  @payload
end

#published_atObject (readonly)

Returns the value of attribute published_at.



7
8
9
# File 'lib/pgbus/event.rb', line 7

def published_at
  @published_at
end

#routing_keyObject (readonly)

Returns the value of attribute routing_key.



7
8
9
# File 'lib/pgbus/event.rb', line 7

def routing_key
  @routing_key
end

Instance Method Details

#[](key) ⇒ Object



17
18
19
# File 'lib/pgbus/event.rb', line 17

def [](key)
  payload.is_a?(Hash) ? payload[key.to_s] : nil
end

#to_hObject



21
22
23
24
25
26
27
28
29
# File 'lib/pgbus/event.rb', line 21

def to_h
  {
    "event_id" => event_id,
    "payload" => payload,
    "published_at" => published_at.iso8601(6),
    "routing_key" => routing_key,
    "headers" => headers
  }
end