Module: Rabbit::Compressor

Extended by:
Compressor
Included in:
Compressor
Defined in:
lib/rabbit/compressor.rb

Constant Summary collapse

Error =
Class.new(StandardError)
UncompressingError =
Class.new(Error)

Instance Method Summary collapse

Instance Method Details

#dump(data, msgpack_options: {}, with_base64: false) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/rabbit/compressor.rb', line 15

def dump(data, msgpack_options: {}, with_base64: false)
  dumped = Zlib::Deflate.deflate(MessagePack.pack(data, msgpack_options))

  return dumped unless with_base64

  strict_encode64(dumped)
end

#load(data, msgpack_options: {}, with_base64: false) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/rabbit/compressor.rb', line 23

def load(data, msgpack_options: {}, with_base64: false)
  return {} unless data

  data = decode64(data) if with_base64

  MessagePack.unpack(Zlib::Inflate.inflate(data), msgpack_options)
rescue Zlib::Error, MessagePack::UnpackError => error
  raise UncompressingError, "Unable to uncompress data, #{error}"
end