-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for amqp payload compress/decompress
- Loading branch information
Showing
7 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'dry/monads' | ||
require 'dry/monads/do' | ||
require 'dry-initializer' | ||
# require 'zlib' | ||
# require 'base64' | ||
module EventSource | ||
module Operations | ||
# A class for handling payload compression and decompression using Dry-rb. | ||
class PayloadCodec | ||
include Dry::Monads[:result] # For Success/Failure monads | ||
include Dry::Monads::Do # For do notation | ||
extend Dry::Initializer | ||
|
||
option :payload, reader: :private | ||
|
||
# Compresses the payload into a Base64-encoded compressed string | ||
# | ||
# @return [Dry::Monads::Success<String>] if compression is successful | ||
# @return [Dry::Monads::Failure<String>] if an error occurs | ||
def compress | ||
json_payload = yield validate_payload_for_compression | ||
compressed_data = Zlib.deflate(json_payload) | ||
encoded_data = Base64.encode64(compressed_data) | ||
|
||
Success(encoded_data) | ||
rescue StandardError => e | ||
Failure("Compression failed: #{e.message}") | ||
end | ||
|
||
# Decompresses a Base64-encoded compressed payload back to its original form | ||
# | ||
# @return [Dry::Monads::Success<String>] if decompression is successful | ||
# @return [Dry::Monads::Failure<String>] if an error occurs | ||
def decompress | ||
decoded_data = yield validate_payload_for_decompression | ||
decompressed_data = Zlib.inflate(decoded_data) | ||
|
||
Success(decompressed_data) | ||
rescue StandardError => e | ||
Failure("Decompression failed: #{e.message}") | ||
end | ||
|
||
private | ||
|
||
# Validates the payload before compression | ||
# | ||
# @return [Dry::Monads::Success<String>] if the payload is valid | ||
# @return [Dry::Monads::Failure<String>] if the payload is invalid | ||
def validate_payload_for_compression | ||
return Failure('Payload must be a Hash or String') unless payload.is_a?(Hash) || payload.is_a?(String) | ||
|
||
Success(payload.is_a?(Hash) ? payload.to_json : payload) | ||
end | ||
|
||
# Validates the payload before decompression | ||
# | ||
# @return [Dry::Monads::Success<String>] if the payload is valid | ||
# @return [Dry::Monads::Failure<String>] if the payload is invalid | ||
def validate_payload_for_decompression | ||
return Failure('Payload must be a Base64-encoded String') unless payload.is_a?(String) | ||
|
||
decoded_data = Base64.decode64(payload) | ||
Success(decoded_data) | ||
rescue StandardError | ||
Failure('Invalid Base64 string for decompression') | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters