Skip to content

Commit ad1b5e4

Browse files
delnerivoanjo
authored andcommitted
Added: Datadog::Utils::Compression
1 parent 67cd90b commit ad1b5e4

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lib/ddtrace/utils/compression.rb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require 'zlib'
2+
3+
module Datadog
4+
module Utils
5+
# Common database-related utility functions.
6+
module Compression
7+
module_function
8+
9+
def gzip(string, level: nil, strategy: nil)
10+
sio = StringIO.new
11+
sio.binmode
12+
gz = Zlib::GzipWriter.new(sio, level, strategy)
13+
gz.write(string)
14+
gz.close
15+
sio.string
16+
end
17+
18+
def gunzip(string, encoding = ::Encoding::ASCII_8BIT)
19+
sio = StringIO.new(string)
20+
gz = Zlib::GzipReader.new(sio, encoding: encoding)
21+
gz.read
22+
ensure
23+
gz && gz.close
24+
end
25+
end
26+
end
27+
end
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'securerandom'
2+
require 'ddtrace/utils/compression'
3+
4+
RSpec.describe Datadog::Utils::Compression do
5+
describe '::gzip' do
6+
subject(:gzip) { described_class.gzip(unzipped) }
7+
let(:unzipped) { SecureRandom.uuid }
8+
9+
it { is_expected.to be_a_kind_of(String) }
10+
11+
context 'when result is unzipped' do
12+
subject(:gunzip) { described_class.gunzip(gzip) }
13+
it { is_expected.to eq(unzipped) }
14+
end
15+
end
16+
17+
describe '::gunzip' do
18+
subject(:gunzip) { described_class.gunzip(zipped) }
19+
20+
context 'given a zipped string' do
21+
let(:zipped) { described_class.gzip(unzipped) }
22+
let(:unzipped) { SecureRandom.uuid }
23+
it { is_expected.to eq(unzipped) }
24+
end
25+
end
26+
end

0 commit comments

Comments
 (0)