Module: BazaRb::Compress
- Included in:
- BazaRb
- Defined in:
- lib/baza-rb/compress.rb
Overview
Compression helpers.
Constant Summary collapse
- LIMIT_UNCOMPRESSED =
10_485_760
Instance Method Summary collapse
-
#unzip(data) ⇒ String
Decompress gzipped data.
-
#zipped(params) ⇒ Hash
Compress request parameters with gzip.
Instance Method Details
#unzip(data) ⇒ String
Decompress gzipped data.
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/baza-rb/compress.rb', line 27 def unzip(data) unzipped = StringIO.new Zlib::GzipReader.new(StringIO.new(data)).each(4096) do |chunk| unzipped.write(chunk) next unless unzipped.length > LIMIT_UNCOMPRESSED raise(BadCompression, "Uncompressed size #{unzipped.length} exceeds limit #{LIMIT_UNCOMPRESSED}") end unzipped.string rescue Zlib::GzipFile::Error => e raise(BadCompression, "Failed to unzip #{data.bytesize} bytes: #{e.}") end |
#zipped(params) ⇒ Hash
Compress request parameters with gzip.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/baza-rb/compress.rb', line 43 def zipped(params) io = StringIO.new gz = Zlib::GzipWriter.new(io) gz.write(params.fetch(:body)) gz.close body = io.string params.merge( body:, headers: params.fetch(:headers).merge({ 'Content-Encoding' => 'gzip', 'Content-Length' => body.bytesize }) ) end |