Class: Readthis::Compressor
- Inherits:
-
Object
- Object
- Readthis::Compressor
- Defined in:
- lib/readthis/compressor.rb
Instance Attribute Summary collapse
-
#threshold ⇒ Object
readonly
Returns the value of attribute threshold.
Instance Method Summary collapse
-
#compress(value) ⇒ Object
Compress a value if its size is greater or equal to the current threshold.
-
#decompress(value) ⇒ Object
Decompress a previously compressed object.
-
#initialize(threshold: 1024) ⇒ Compressor
constructor
Create a new Readthis::Compressor object that pivots on the provided threshold value.
Constructor Details
#initialize(threshold: 1024) ⇒ Compressor
Create a new Readthis::Compressor object that pivots on the provided threshold value.
11 12 13 |
# File 'lib/readthis/compressor.rb', line 11 def initialize(threshold: 1024) @threshold = threshold end |
Instance Attribute Details
#threshold ⇒ Object (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.
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.
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 |