Class: Rack::SmartCompress::StreamBody

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/smart_compress/stream_body.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body, encoder_name, encoder_class, level: nil) ⇒ StreamBody

Returns a new instance of StreamBody.



11
12
13
14
15
16
17
# File 'lib/rack/smart_compress/stream_body.rb', line 11

def initialize(body, encoder_name, encoder_class, level: nil)
  @body = body
  @encoder_name = encoder_name
  @encoder_class = encoder_class
  @level = level
  @closed = false
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/rack/smart_compress/stream_body.rb', line 9

def body
  @body
end

#encoder_classObject (readonly)

Returns the value of attribute encoder_class.



9
10
11
# File 'lib/rack/smart_compress/stream_body.rb', line 9

def encoder_class
  @encoder_class
end

#encoder_nameObject (readonly)

Returns the value of attribute encoder_name.



9
10
11
# File 'lib/rack/smart_compress/stream_body.rb', line 9

def encoder_name
  @encoder_name
end

#levelObject (readonly)

Returns the value of attribute level.



9
10
11
# File 'lib/rack/smart_compress/stream_body.rb', line 9

def level
  @level
end

Instance Method Details

#closeObject



40
41
42
43
44
45
# File 'lib/rack/smart_compress/stream_body.rb', line 40

def close
  return if @closed

  @closed = true
  @body.close if @body.respond_to?(:close)
end

#each(&block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rack/smart_compress/stream_body.rb', line 19

def each(&block)
  return enum_for(:each) unless block_given?

  begin
    case encoder_name
    when "gzip"
      stream_gzip(&block)
    when "deflate"
      stream_deflate(&block)
    when "zstd"
      stream_zstd(&block)
    when "br"
      stream_brotli(&block)
    else
      @body.each { |chunk| yield encoder_class.compress(chunk.to_s, level: level) }
    end
  ensure
    close
  end
end