Class: HTTPX::Plugins::Compression::GZIP::Encoder

Inherits:
Object
  • Object
show all
Defined in:
lib/httpx/plugins/compression/gzip.rb

Instance Method Summary collapse

Constructor Details

#initializeEncoder

Returns a new instance of Encoder.



18
19
20
# File 'lib/httpx/plugins/compression/gzip.rb', line 18

def initialize
  @compressed_chunk = "".b
end

Instance Method Details

#deflate(raw, buffer, chunk_size:) {|compressed| ... } ⇒ Object

Yields:

  • (compressed)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/httpx/plugins/compression/gzip.rb', line 22

def deflate(raw, buffer, chunk_size:)
  gzip = Zlib::GzipWriter.new(self)

  begin
    while (chunk = raw.read(chunk_size))
      gzip.write(chunk)
      gzip.flush
      compressed = compressed_chunk
      buffer << compressed
      yield compressed if block_given?
    end
  ensure
    gzip.close
  end

  return unless (compressed = compressed_chunk)

  buffer << compressed
  yield compressed if block_given?
end