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, context: nil) ⇒ Event

Returns a new instance of Event.



13
14
15
16
17
18
19
20
# File 'lib/pgbus/event.rb', line 13

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

Instance Attribute Details

#contextObject (readonly)

context is the publisher's persisted ActiveSupport::CurrentAttributes (issue #431) in their stored form — { "Current" => serialized attrs } — or nil. Handlers normally just read Current (it is restored around handle); the raw form is here for tests and explicit access.



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

def context
  @context
end

#event_idObject (readonly)

context is the publisher's persisted ActiveSupport::CurrentAttributes (issue #431) in their stored form — { "Current" => serialized attrs } — or nil. Handlers normally just read Current (it is restored around handle); the raw form is here for tests and explicit access.



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

def event_id
  @event_id
end

#headersObject (readonly)

context is the publisher's persisted ActiveSupport::CurrentAttributes (issue #431) in their stored form — { "Current" => serialized attrs } — or nil. Handlers normally just read Current (it is restored around handle); the raw form is here for tests and explicit access.



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

def headers
  @headers
end

#payloadObject (readonly)

context is the publisher's persisted ActiveSupport::CurrentAttributes (issue #431) in their stored form — { "Current" => serialized attrs } — or nil. Handlers normally just read Current (it is restored around handle); the raw form is here for tests and explicit access.



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

def payload
  @payload
end

#published_atObject (readonly)

context is the publisher's persisted ActiveSupport::CurrentAttributes (issue #431) in their stored form — { "Current" => serialized attrs } — or nil. Handlers normally just read Current (it is restored around handle); the raw form is here for tests and explicit access.



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

def published_at
  @published_at
end

#routing_keyObject (readonly)

context is the publisher's persisted ActiveSupport::CurrentAttributes (issue #431) in their stored form — { "Current" => serialized attrs } — or nil. Handlers normally just read Current (it is restored around handle); the raw form is here for tests and explicit access.



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

def routing_key
  @routing_key
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
# File 'lib/pgbus/event.rb', line 22

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

#to_hObject



26
27
28
29
30
31
32
33
34
# File 'lib/pgbus/event.rb', line 26

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