Module: Fewer::Serializer

Defined in:
lib/fewer/serializer.rb

Defined Under Namespace

Classes: BadString

Class Method Summary collapse

Class Method Details

.b64_decode(string) ⇒ Object


8
9
10
11
# File 'lib/fewer/serializer.rb', line 8

def b64_decode(string)
  padding_length = string.length % 4
  Base64.decode64(string + '=' * padding_length)
end

.b64_encode(string) ⇒ Object


13
14
15
# File 'lib/fewer/serializer.rb', line 13

def b64_encode(string)
  Base64.encode64(string).tr("\n=",'')
end

.marshal_decode(string) ⇒ Object Also known as: decode


17
18
19
20
21
# File 'lib/fewer/serializer.rb', line 17

def marshal_decode(string)
  Marshal.load(b64_decode(string))
rescue TypeError, ArgumentError => e
  raise BadString, "couldn't decode #{string} - got #{e}"
end

.marshal_encode(object) ⇒ Object Also known as: encode


24
25
26
# File 'lib/fewer/serializer.rb', line 24

def marshal_encode(object)
  b64_encode(Marshal.dump(object))
end