Class: Debezium::Message

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

Overview

Message represents a Debezium message, containing information about the ‘before` and `after` states of a record.

This class parses a Debezium event message (JSON) and provides methods to determine the type of operation and access the changes between the ‘before` and `after` states in case of an update operation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ Message

Initializes a new Message instance by parsing the given Debezium JSON message.

Parameters:

  • json (String)

    The Debezium JSON message to parse.



18
19
20
21
22
# File 'lib/debezium/message.rb', line 18

def initialize(json)
  @json    = JSON.parse(json)
  @payload = @json['payload']
  @op      = parse_op
end

Instance Attribute Details

#opSymbol (readonly)

Returns The operation type (‘:create`, `:update`, `:delete`, or `:unknown`).

Returns:

  • (Symbol)

    The operation type (‘:create`, `:update`, `:delete`, or `:unknown`).



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

def op
  @op
end

Instance Method Details

#changesChange

Returns the changes between the ‘before` and `after` states.

Returns:

  • (Change)

    Returns a ‘Change` object.



48
49
50
# File 'lib/debezium/message.rb', line 48

def changes
  @changes ||= Change.new(@payload['before'], @payload['after'])
end

#create?Boolean

Checks if the operation is a “create” operation.

Returns:

  • (Boolean)

    ‘true` if the operation is a create, otherwise `false`.



27
28
29
# File 'lib/debezium/message.rb', line 27

def create?
  @op == :create
end

#delete?Boolean

Checks if the operation is a “delete” operation.

Returns:

  • (Boolean)

    ‘true` if the operation is a delete, otherwise `false`.



41
42
43
# File 'lib/debezium/message.rb', line 41

def delete?
  @op == :delete
end

#to_hHash

Returns The parsed JSON of the event.

Returns:

  • (Hash)

    The parsed JSON of the event.



53
54
55
# File 'lib/debezium/message.rb', line 53

def to_h
  @json
end

#update?Boolean

Checks if the operation is an “update” operation.

Returns:

  • (Boolean)

    ‘true` if the operation is an update, otherwise `false`.



34
35
36
# File 'lib/debezium/message.rb', line 34

def update?
  @op == :update
end