Class: Stickler::Middleware::Compression

Inherits:
Object
  • Object
show all
Defined in:
lib/stickler/middleware/compression.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Compression

Returns a new instance of Compression.



3
4
5
# File 'lib/stickler/middleware/compression.rb', line 3

def initialize( app )
  @app = app 
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/stickler/middleware/compression.rb', line 7

def call( env )
  status, headers, body = @app.call( env )
  return [ status, headers, body ] unless status == 200

  headers = ::Rack::Utils::HeaderHash.new( headers )
  stream  = body

  if compress_method = env['stickler.compression'] then
    headers.delete('Content-Length')
    case compress_method
    when :gzip
      headers['Content-Type'] = 'application/x-gzip'
      stream = Gem.gzip( body.first )
    when :deflate
      headers['Content-Type'] = 'application/x-deflate'
      stream = Gem.deflate( body.first )
    end
  end
  stream = [ stream.to_s ] unless stream.respond_to?( :each )
  return [ status, headers, stream ]
end