Module: Webmachine::Resource::Encodings

Included in:
Webmachine::Resource
Defined in:
lib/webmachine/resource/encodings.rb

Overview

This module implements standard Content-Encodings that you might want to use in your Webmachine::Resource. To use one, simply return it in the hash from Callbacks#encodings_provided.

Instance Method Summary collapse

Instance Method Details

#encode_deflate(data) ⇒ Object

The ‘deflate’ encoding, which uses libz’s DEFLATE compression.



16
17
18
19
20
21
22
23
24
# File 'lib/webmachine/resource/encodings.rb', line 16

def encode_deflate(data)
  # The deflate options were borrowed from Rack and Mongrel1.
  Zlib::Deflate.deflate(data, *[Zlib::DEFAULT_COMPRESSION,
                                # drop the zlib header which causes both Safari and IE to choke
                                -Zlib::MAX_WBITS,
                                Zlib::DEF_MEM_LEVEL,
                                Zlib::DEFAULT_STRATEGY
                               ])
end

#encode_gzip(data) ⇒ Object

Note:

Because of the header/checksum requirements, gzip cannot be used on streamed responses.

The ‘gzip’ encoding, which uses GNU Zip (via libz).



29
30
31
32
33
# File 'lib/webmachine/resource/encodings.rb', line 29

def encode_gzip(data)
  "".tap do |out|
    Zlib::GzipWriter.wrap(StringIO.new(out)){|gz| gz << data }
  end
end

#encode_identity(data) ⇒ Object

The ‘identity’ encoding, which does no compression.



11
12
13
# File 'lib/webmachine/resource/encodings.rb', line 11

def encode_identity(data)
  data
end