debezium 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/debezium/change.rb +116 -0
- data/lib/debezium/message.rb +84 -0
- data/lib/debezium/version.rb +5 -0
- data/lib/debezium.rb +12 -0
- metadata +47 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9f3398eb5ff6b79c049747db3b65c73bdea7f6bf96d1b5f6ac358930e1fda59a
|
4
|
+
data.tar.gz: 77738af1561bd3beddc86f57c3df3fa1535b6f5320b6c7c4229b3cfac40a1393
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b73496223f22bffa65709c384a997a1838c83e27d9d8c19705018b06ef06d74d9a377077055ea9d2b4513bf9b33a45beffee100b64c5219df3fbe57cf1ba70e3
|
7
|
+
data.tar.gz: 01c728a6af11de6f26f0140af59e6dd598eef6503d0b679c953dac57797818ac580a82fa132d9bf87e5aefa4630376473e335f7894111c0b33a8bca88633d3fb
|
@@ -0,0 +1,116 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Debezium
|
4
|
+
# Represents the differences between two hashes, categorizing them as additions, removals, or modifications.
|
5
|
+
class Change
|
6
|
+
# Initializes a Change object and computes the differences between two hashes.
|
7
|
+
#
|
8
|
+
# @param old [Hash] The original hash.
|
9
|
+
# @param new [Hash] The modified hash.
|
10
|
+
def initialize(old, new)
|
11
|
+
@additions = {}
|
12
|
+
@removals = {}
|
13
|
+
@modifications = {}
|
14
|
+
|
15
|
+
hash_diff(old, new)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Checks if the provided key was added.
|
19
|
+
#
|
20
|
+
# @param key [Object] The key to check.
|
21
|
+
# @return [Boolean] True if the key was added, otherwise false.
|
22
|
+
def added?(key)
|
23
|
+
@additions.include?(key)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Checks if the provided key was removed.
|
27
|
+
#
|
28
|
+
# @param key [Object] The key to check.
|
29
|
+
# @return [Boolean] True if the key was removed, otherwise false.
|
30
|
+
def removed?(key)
|
31
|
+
@removals.include?(key)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Checks if the provided key was modified.
|
35
|
+
#
|
36
|
+
# @param key [Object] The key to check.
|
37
|
+
# @return [Boolean] True if the key was modified, otherwise false.
|
38
|
+
def modified?(key)
|
39
|
+
@modifications.include?(key)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns the list of added key-value pairs.
|
43
|
+
#
|
44
|
+
# @return [Array<Array>] An array of key-value pairs that were added.
|
45
|
+
def additions
|
46
|
+
@additions.to_a
|
47
|
+
end
|
48
|
+
|
49
|
+
# Returns the list of removed key-value pairs.
|
50
|
+
#
|
51
|
+
# @return [Array<Array>] An array of key-value pairs that were removed.
|
52
|
+
def removals
|
53
|
+
@removals.to_a
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the list of modified key-value pairs.
|
57
|
+
#
|
58
|
+
# @return [Array<Array>] An array of key-value pairs showing new and old values.
|
59
|
+
def modifications
|
60
|
+
@modifications.to_a
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
# Computes the differences between two hashes and categorizes them into additions, removals, and modifications.
|
66
|
+
#
|
67
|
+
# @param old_hash [Hash] The original hash.
|
68
|
+
# @param new_hash [Hash] The modified hash.
|
69
|
+
# @return [void]
|
70
|
+
def hash_diff(old_hash, new_hash)
|
71
|
+
hash_additions(old_hash, new_hash)
|
72
|
+
hash_removals(old_hash, new_hash)
|
73
|
+
hash_modifications(old_hash, new_hash)
|
74
|
+
end
|
75
|
+
|
76
|
+
# Computes hash additions between two hashes.
|
77
|
+
#
|
78
|
+
# @param old_hash [Hash] The original hash.
|
79
|
+
# @param new_hash [Hash] The modified hash.
|
80
|
+
# @return [void]
|
81
|
+
def hash_additions(old_hash, new_hash)
|
82
|
+
new_hash.each do |key, value|
|
83
|
+
next if old_hash.key?(key)
|
84
|
+
|
85
|
+
@additions[key] = value
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# Computes hash removals between two hashes.
|
90
|
+
#
|
91
|
+
# @param old_hash [Hash] The original hash.
|
92
|
+
# @param new_hash [Hash] The modified hash.
|
93
|
+
# @return [void]
|
94
|
+
def hash_removals(old_hash, new_hash)
|
95
|
+
old_hash.each do |key, value|
|
96
|
+
next if new_hash.key?(key)
|
97
|
+
|
98
|
+
@removals[key] = value
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Computes hash modifications between two hashes.
|
103
|
+
#
|
104
|
+
# @param old_hash [Hash] The original hash.
|
105
|
+
# @param new_hash [Hash] The modified hash.
|
106
|
+
# @return [void]
|
107
|
+
def hash_modifications(old_hash, new_hash)
|
108
|
+
old_hash.each do |key, value|
|
109
|
+
next unless new_hash.key?(key)
|
110
|
+
next if new_hash[key] == value
|
111
|
+
|
112
|
+
@modifications[key] = [new_hash[key], value]
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Debezium
|
4
|
+
# Message represents a Debezium message, containing information about the `before` and `after`
|
5
|
+
# states of a record.
|
6
|
+
#
|
7
|
+
# This class parses a Debezium event message (JSON) and provides methods to determine the type
|
8
|
+
# of operation and access the changes between the `before` and `after` states in case of an update operation.
|
9
|
+
#
|
10
|
+
class Message
|
11
|
+
# @return [Object] The `after` state of the record.
|
12
|
+
attr_accessor :after
|
13
|
+
|
14
|
+
# @return [Object] The `before` state of the record.
|
15
|
+
attr_accessor :before
|
16
|
+
|
17
|
+
# @return [Symbol] The operation type (`:create`, `:update`, `:delete`, or `:unknown`).
|
18
|
+
attr_accessor :op
|
19
|
+
|
20
|
+
# @return [Hash] The parsed JSON of the event.
|
21
|
+
attr_accessor :json
|
22
|
+
|
23
|
+
# Initializes a new Message instance by parsing the given Debezium JSON message.
|
24
|
+
#
|
25
|
+
# @param json [String] The Debezium JSON message to parse.
|
26
|
+
# @return [Message] The newly created Message instance.
|
27
|
+
def initialize(json)
|
28
|
+
@json = JSON.parse(json)
|
29
|
+
|
30
|
+
@before = @json['before']
|
31
|
+
@after = @json['after']
|
32
|
+
@op = parse_op(@json['op'])
|
33
|
+
end
|
34
|
+
|
35
|
+
# Checks if the operation is a "create" operation.
|
36
|
+
#
|
37
|
+
# @return [Boolean] `true` if the operation is a create, otherwise `false`.
|
38
|
+
def create?
|
39
|
+
@op == :create
|
40
|
+
end
|
41
|
+
|
42
|
+
# Checks if the operation is an "update" operation.
|
43
|
+
#
|
44
|
+
# @return [Boolean] `true` if the operation is an update, otherwise `false`.
|
45
|
+
def update?
|
46
|
+
@op == :update
|
47
|
+
end
|
48
|
+
|
49
|
+
# Checks if the operation is a "delete" operation.
|
50
|
+
#
|
51
|
+
# @return [Boolean] `true` if the operation is a delete, otherwise `false`.
|
52
|
+
def delete?
|
53
|
+
@op == :delete
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns the changes between the `before` and `after` states for update operations.
|
57
|
+
#
|
58
|
+
# @return [Change, nil] Returns a `Change` object if the operation is an update; otherwise `nil`.
|
59
|
+
def changes
|
60
|
+
return nil unless update?
|
61
|
+
|
62
|
+
@changes ||= Change.new(before, after)
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
# Parses the operation type from the given Debezium operation code.
|
68
|
+
#
|
69
|
+
# @param operation [String] The Debezium operation code (`'c'` for create, `'u'` for update, `'d'` for delete).
|
70
|
+
# @return [Symbol] The operation (`:create`, `:update`, `:delete`, or `:unknown`).
|
71
|
+
def parse_op(operation)
|
72
|
+
case operation
|
73
|
+
when 'c'
|
74
|
+
:create
|
75
|
+
when 'u'
|
76
|
+
:update
|
77
|
+
when 'd'
|
78
|
+
:delete
|
79
|
+
else
|
80
|
+
:unknown
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/debezium.rb
ADDED
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: debezium
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tate Thurston
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-03-04 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/debezium.rb
|
20
|
+
- lib/debezium/change.rb
|
21
|
+
- lib/debezium/message.rb
|
22
|
+
- lib/debezium/version.rb
|
23
|
+
homepage: https://github.com/tatethurston/ruby-debezium
|
24
|
+
licenses:
|
25
|
+
- MIT
|
26
|
+
metadata:
|
27
|
+
rubygems_mfa_required: 'true'
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '3.2'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubygems_version: 3.5.9
|
44
|
+
signing_key:
|
45
|
+
specification_version: 4
|
46
|
+
summary: A gem to handle CDC messages from Debezium
|
47
|
+
test_files: []
|