Skip to content

Commit 24f267b

Browse files
committed
#360: extract Compress module (unzip/zipped) from god-class
1 parent bcf0dd5 commit 24f267b

2 files changed

Lines changed: 50 additions & 26 deletions

File tree

lib/baza-rb.rb

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
require 'tempfile'
1616
require 'typhoeus'
1717
require 'zlib'
18+
require_relative 'baza-rb/compress'
1819
require_relative 'baza-rb/version'
1920

2021
# Ruby client for the Zerocracy API.
@@ -34,6 +35,8 @@
3435
class BazaRb
3536
DEFAULT_CHUNK_SIZE = 1_000_000
3637

38+
include Compress
39+
3740
# When the server failed (503).
3841
class ServerFailure < StandardError; end
3942

@@ -562,32 +565,6 @@ def headers
562565
}
563566
end
564567

565-
# Decompress gzipped data.
566-
#
567-
# @param [String] data The gzipped data to decompress
568-
# @return [String] The decompressed data
569-
def unzip(data)
570-
Zlib::GzipReader.new(StringIO.new(data)).read
571-
rescue Zlib::GzipFile::Error => e
572-
raise(BadCompression, "Failed to unzip #{data.bytesize} bytes: #{e.message}")
573-
end
574-
575-
# Compress request parameters with gzip.
576-
#
577-
# @param [Hash] params The request parameters with :body and :headers keys
578-
# @return [Hash] The modified parameters with compressed body and updated headers
579-
def zipped(params)
580-
io = StringIO.new
581-
gz = Zlib::GzipWriter.new(io)
582-
gz.write(params.fetch(:body))
583-
gz.close
584-
body = io.string
585-
params.merge(
586-
body:,
587-
headers: params.fetch(:headers).merge({ 'Content-Encoding' => 'gzip', 'Content-Length' => body.bytesize })
588-
)
589-
end
590-
591568
# Build the base URI for API requests.
592569
#
593570
# @return [Iri] The base URI object

lib/baza-rb/compress.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
# SPDX-FileCopyrightText: Copyright (c) 2024-2026 Zerocracy
4+
# SPDX-License-Identifier: MIT
5+
6+
require 'stringio'
7+
require 'zlib'
8+
9+
# Compression helpers for BazaRb.
10+
#
11+
# Provides gzip compression and decompression for request bodies
12+
# and response data.
13+
#
14+
# Author:: Yegor Bugayenko (yegor256@gmail.com)
15+
# Copyright:: Copyright (c) 2024-2026 Yegor Bugayenko
16+
# License:: MIT
17+
class BazaRb
18+
# Compression helpers.
19+
module Compress
20+
# Decompress gzipped data.
21+
#
22+
# @param [String] data The gzipped data to decompress
23+
# @return [String] The decompressed data
24+
# @raise [BadCompression] If the data is not valid gzip
25+
def unzip(data)
26+
Zlib::GzipReader.new(StringIO.new(data)).read
27+
rescue Zlib::GzipFile::Error => e
28+
raise(BadCompression, "Failed to unzip #{data.bytesize} bytes: #{e.message}")
29+
end
30+
31+
# Compress request parameters with gzip.
32+
#
33+
# @param [Hash] params The request parameters with :body and :headers keys
34+
# @return [Hash] The modified parameters with compressed body and updated headers
35+
def zipped(params)
36+
io = StringIO.new
37+
gz = Zlib::GzipWriter.new(io)
38+
gz.write(params.fetch(:body))
39+
gz.close
40+
body = io.string
41+
params.merge(
42+
body:,
43+
headers: params.fetch(:headers).merge({ 'Content-Encoding' => 'gzip', 'Content-Length' => body.bytesize })
44+
)
45+
end
46+
end
47+
end

0 commit comments

Comments
 (0)