Class: HTTPX::Transcoder::Deflater

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/httpx/transcoder/utils/deflater.rb,
sig/transcoder/utils/deflater.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body) ⇒ Deflater

Returns a new instance of Deflater.

Parameters:

  • body (body_encoder)


10
11
12
13
14
# File 'lib/httpx/transcoder/utils/deflater.rb', line 10

def initialize(body)
  @content_type = body.content_type
  @body = BodyReader.new(body)
  @closed = false
end

Instance Attribute Details

#content_typeString (readonly)

Returns the value of attribute content_type.

Returns:

  • (String)


8
9
10
# File 'lib/httpx/transcoder/utils/deflater.rb', line 8

def content_type
  @content_type
end

Instance Method Details

#buffer_deflate!void

This method returns an undefined value.

rubocop:disable Naming/MemoizedInstanceVariableName



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/httpx/transcoder/utils/deflater.rb', line 59

def buffer_deflate!
  return @buffer if defined?(@buffer)

  buffer = Response::Buffer.new(
    threshold_size: Options::MAX_BODY_THRESHOLD_SIZE
  )
  IO.copy_stream(self, buffer)

  buffer.rewind if buffer.respond_to?(:rewind)

  @buffer = buffer
end

#bytesizeInteger, Float

Returns:

  • (Integer, Float)


16
17
18
19
20
# File 'lib/httpx/transcoder/utils/deflater.rb', line 16

def bytesize
  buffer_deflate!

  @buffer.size
end

#closevoid

This method returns an undefined value.



40
41
42
43
44
45
46
47
48
# File 'lib/httpx/transcoder/utils/deflater.rb', line 40

def close
  return if @closed

  @buffer.close if @buffer

  @body.close

  @closed = true
end

#deflateString?

Parameters:

  • chunk (String, nil)

Returns:

  • (String, nil)


20
# File 'sig/transcoder/utils/deflater.rbs', line 20

def deflate: (String? chunk) -> String?

#read(length = nil, outbuf = nil) ⇒ String?

Parameters:

  • length (int, nil) (defaults to: nil)
  • outbuf (string, nil) (defaults to: nil)

Returns:

  • (String, nil)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/httpx/transcoder/utils/deflater.rb', line 22

def read(length = nil, outbuf = nil)
  return @buffer.read(length, outbuf) if @buffer

  return if @closed

  chunk = @body.read(length)

  compressed_chunk = deflate(chunk)

  return unless compressed_chunk

  if outbuf
    outbuf.replace(compressed_chunk)
  else
    compressed_chunk
  end
end

#rewindvoid

This method returns an undefined value.



50
51
52
53
54
# File 'lib/httpx/transcoder/utils/deflater.rb', line 50

def rewind
  return unless @buffer

  @buffer.rewind
end