Class: Readthis::Compressor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(threshold: 1024) ⇒ Compressor

Create a new Readthis::Compressor object that pivots on the provided threshold value.

Parameters:

  • threshold (Number) (defaults to: 1024)

    the threshold size required for compression



11
12
13
# File 'lib/readthis/compressor.rb', line 11

def initialize(threshold: 1024)
  @threshold = threshold
end

Instance Attribute Details

#thresholdObject (readonly)

Returns the value of attribute threshold.



5
6
7
# File 'lib/readthis/compressor.rb', line 5

def threshold
  @threshold
end

Instance Method Details

#compress(value) ⇒ Object

Compress a value if its size is greater or equal to the current threshold.

Parameters:

  • value (String)

    a string to compress



18
19
20
21
22
23
24
# File 'lib/readthis/compressor.rb', line 18

def compress(value)
  if value.size >= threshold
    Zlib::Deflate.deflate(value)
  else
    value
  end
end

#decompress(value) ⇒ Object

Decompress a previously compressed object. It will attempt to decode a value regardless of whether it has been compressed, but will rescue decoding errors.

Parameters:

  • value (String)

    a possibly compressed string to decompress



31
32
33
34
35
36
37
38
39
# File 'lib/readthis/compressor.rb', line 31

def decompress(value)
  if value.size >= threshold
    Zlib::Inflate.inflate(value)
  else
    value
  end
rescue Zlib::Error
  value
end