Module: Messaging::Message

Extended by:
ActiveSupport::Concern
Defined in:
lib/messaging/message.rb,
lib/messaging/message/from_json.rb

Defined Under Namespace

Modules: ClassMethods Classes: FromJson

Instance Method Summary collapse

Instance Method Details

#as_json(*_args) ⇒ Object



74
75
76
# File 'lib/messaging/message.rb', line 74

def as_json(*_args)
  attributes_as_json.merge(stream_position: stream_position)
end

#attributes_as_json(*_args) ⇒ Object

We do our own conversion for datetimes as by default they will only be stored with microseconds, which makes us loose precision when we serialize to json and back to message objects



70
71
72
# File 'lib/messaging/message.rb', line 70

def attributes_as_json(*_args)
  attributes.transform_values { |v| v.respond_to?(:nsec) ? v.iso8601(9) : v }
end

#key_attributeObject



82
83
84
# File 'lib/messaging/message.rb', line 82

def key_attribute
  self.class.key_attribute
end

#message_keyObject



78
79
80
# File 'lib/messaging/message.rb', line 78

def message_key
  attributes[key_attribute&.to_sym]
end

#message_typeObject



95
96
97
# File 'lib/messaging/message.rb', line 95

def message_type
  self.class.to_s
end

#publishObject



99
100
101
# File 'lib/messaging/message.rb', line 99

def publish
  Messaging::Publish.(message: self)
end

#stream_nameObject



90
91
92
93
# File 'lib/messaging/message.rb', line 90

def stream_name
  # define stream_name in your message class to override
  nil
end

#topicObject



86
87
88
# File 'lib/messaging/message.rb', line 86

def topic
  self.class.topic
end