Class: Ramaze::Filter::Gzip
- Extended by:
- Trinity
- Defined in:
- lib/ramaze/contrib/gzip_filter.rb
Class Method Summary collapse
-
.call(response, options = {}) ⇒ Object
Enables being plugged into Dispatcher::Action::FILTER.
Methods included from StateAccessor
each, #state_accessor, #state_reader, #state_writer
Class Method Details
.call(response, options = {}) ⇒ Object
Enables being plugged into Dispatcher::Action::FILTER
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ramaze/contrib/gzip_filter.rb', line 39 def call( response, = {} ) return response unless trait[ :enabled ] return response unless body = response.body return response if body.respond_to?( :read ) accepts = request.env[ 'HTTP_ACCEPT_ENCODING' ] return response if accepts.nil? || ( accepts !~ /(x-gzip|gzip)/ ) acceptable_size = body.size >= trait[ :threshold ] acceptable_type = response.content_type =~ trait[:content_type] if acceptable_type and acceptable_size 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( body ) gz.close response.body = output.string response.header[ 'Content-encoding' ] = 'gzip' end response end |