Class: NulogyMessageBusConsumer::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/nulogy_message_bus_consumer/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Message

Returns a new instance of Message.



15
16
17
# File 'lib/nulogy_message_bus_consumer/message.rb', line 15

def initialize(attrs = {})
  attrs.each { |key, value| instance_variable_set("@#{key}", value) }
end

Instance Attribute Details

#company_uuidObject (readonly)

Returns the value of attribute company_uuid.



10
11
12
# File 'lib/nulogy_message_bus_consumer/message.rb', line 10

def company_uuid
  @company_uuid
end

#created_atObject (readonly)

Returns the value of attribute created_at.



13
14
15
# File 'lib/nulogy_message_bus_consumer/message.rb', line 13

def created_at
  @created_at
end

#event_dataObject (readonly)

Returns the value of attribute event_data.



3
4
5
# File 'lib/nulogy_message_bus_consumer/message.rb', line 3

def event_data
  @event_data
end

#event_data_unparsedObject (readonly)

Returns the value of attribute event_data_unparsed.



4
5
6
# File 'lib/nulogy_message_bus_consumer/message.rb', line 4

def event_data_unparsed
  @event_data_unparsed
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/nulogy_message_bus_consumer/message.rb', line 5

def id
  @id
end

#keyObject (readonly)

Returns the value of attribute key.



6
7
8
# File 'lib/nulogy_message_bus_consumer/message.rb', line 6

def key
  @key
end

#offsetObject (readonly)

Returns the value of attribute offset.



7
8
9
# File 'lib/nulogy_message_bus_consumer/message.rb', line 7

def offset
  @offset
end

#partitionObject (readonly)

Returns the value of attribute partition.



8
9
10
# File 'lib/nulogy_message_bus_consumer/message.rb', line 8

def partition
  @partition
end

#subscription_idObject (readonly)

Returns the value of attribute subscription_id.



9
10
11
# File 'lib/nulogy_message_bus_consumer/message.rb', line 9

def subscription_id
  @subscription_id
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



11
12
13
# File 'lib/nulogy_message_bus_consumer/message.rb', line 11

def timestamp
  @timestamp
end

#topicObject (readonly)

Returns the value of attribute topic.



12
13
14
# File 'lib/nulogy_message_bus_consumer/message.rb', line 12

def topic
  @topic
end

Class Method Details

.from_kafka(kafka_message) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nulogy_message_bus_consumer/message.rb', line 19

def self.from_kafka(kafka_message)
  envelope_data = JSON.parse(kafka_message.payload, symbolize_names: true)
  event_data =
    begin
      JSON.parse(envelope_data[:event_json], symbolize_names: true)
    rescue
      {}
    end

  new(
    event_data: event_data,
    event_data_unparsed: envelope_data[:event_json],
    id: envelope_data[:id],
    key: kafka_message.key,
    offset: kafka_message.offset,
    partition: kafka_message.partition,
    subscription_id: envelope_data[:subscription_id] || envelope_data[:public_subscription_id],
    company_uuid: envelope_data[:company_uuid] || envelope_data[:tenant_id],
    timestamp: kafka_message.timestamp,
    topic: kafka_message.topic,
    created_at: envelope_data[:created_at]
  )
end

Instance Method Details

#to_hObject



43
44
45
46
47
48
# File 'lib/nulogy_message_bus_consumer/message.rb', line 43

def to_h
  instance_variables.each_with_object({}) do |instance_variable, hash|
    attribute_name = instance_variable.to_s.delete("@")
    hash[attribute_name.to_sym] = public_send(attribute_name)
  end
end