Class: Ramaze::Filter::Gzip

Inherits:
Object show all
Extended by:
Trinity
Defined in:
lib/ramaze/contrib/gzip_filter.rb

Class Method Summary collapse

Class Method Details

.call(response, options = {}) ⇒ Object

Enables being plugged into Dispatcher::Action::FILTER



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ramaze/contrib/gzip_filter.rb', line 29

def call( response, options = {} )
  return response if not trait[ :enabled ]
  return response if response.body.nil?
  return response if response.body.respond_to?( :read )

  accepts = request.env[ 'HTTP_ACCEPT_ENCODING' ]
  return response if accepts.nil? || ( accepts !~ /(x-gzip|gzip)/ )

  if response.content_type == 'text/html' && response.body.size > trait[ :threshold ]
    output = StringIO.new
    def output.close
      # Zlib closes the file handle, so we want to circumvent this
      rewind
    end
    gz = Zlib::GzipWriter.new( output )
    gz.write( response.body )
    gz.close

    response.body = output.string
    response.header[ 'Content-encoding' ] = 'gzip'
  end

  response
end