Class: EventBus::Event

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, body, schema_version = 1.0) ⇒ Event

Returns a new instance of Event.



5
6
7
8
9
10
11
# File 'lib/event_bus/event.rb', line 5

def initialize(name, body, schema_version = 1.0)
  @name = name
  @body = JSON.parse(body) rescue body
  @schema_version = @body['headers']['schemaVersion'] rescue schema_version
  generate_headers if has_name?
  build_payload if @body && @body.has_key?('headers')
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/event_bus/event.rb', line 3

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



3
4
5
# File 'lib/event_bus/event.rb', line 3

def headers
  @headers
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/event_bus/event.rb', line 3

def name
  @name
end

#schema_versionObject (readonly)

Returns the value of attribute schema_version.



3
4
5
# File 'lib/event_bus/event.rb', line 3

def schema_version
  @schema_version
end

Instance Method Details

#has_body?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/event_bus/event.rb', line 20

def has_body?
  body && !body.empty?
end

#has_name?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/event_bus/event.rb', line 24

def has_name?
  name && !name.empty?
end

#payloadObject



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

def payload
  {
    headers: headers,
    body: body
  }.to_json
end