Class: Columbo::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/columbo/compressor.rb

Class Method Summary collapse

Class Method Details

.compress(source) ⇒ Object

Compresses a string using gzip inspired by ActiveSupport::Gzip



30
31
32
33
34
35
36
# File 'lib/columbo/compressor.rb', line 30

def self.compress(source)
  output = StringIO.new
  gz = Zlib::GzipWriter.new(output)
  gz.write(source)
  gz.close
  output.string
end

.decompress(source) ⇒ Object



42
43
44
# File 'lib/columbo/compressor.rb', line 42

def self.decompress(source)
  Zlib::GzipReader.new(StringIO.new(source)).read
end

.deflate(source) ⇒ Object



38
39
40
# File 'lib/columbo/compressor.rb', line 38

def self.deflate(source)
  Zlib::Deflate.deflate(source)
end

.encoding_handled?(content_encoding) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/columbo/compressor.rb', line 7

def self.encoding_handled?(content_encoding)
  %w(gzip deflate).include? content_encoding
end

.inflate(source) ⇒ Object



46
47
48
# File 'lib/columbo/compressor.rb', line 46

def self.inflate(source)
  Zlib::Inflate.inflate(source.read)
end

.unzip(source, content_encoding) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/columbo/compressor.rb', line 11

def self.unzip(source, content_encoding)
  case content_encoding
    when 'gzip' then decompress(source)
    when 'deflate' then inflate(source)
    else source
  end
end

.zip(source, accept_encoding) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/columbo/compressor.rb', line 19

def self.zip(source, accept_encoding)
  if accept_encoding.match 'deflate'
    deflate(source)
  elsif  accept_encoding.match 'gzip'
    compress(source)
  else
    source
  end
end