Module: Dragonfly::Serializer

Extended by:
Serializer
Included in:
Serializer
Defined in:
lib/dragonfly/serializer.rb

Defined Under Namespace

Classes: BadString, MaliciousString

Instance Method Summary collapse

Instance Method Details

#b64_decode(string) ⇒ Object


19
20
21
22
23
24
25
# File 'lib/dragonfly/serializer.rb', line 19

def b64_decode(string)
  padding_length = (-(string.length % 4)) % 4
  string = string.tr('+','-').tr('~/','_')
  Base64.urlsafe_decode64(string + '=' * padding_length)
rescue ArgumentError => e
  raise BadString, "couldn't b64_decode string - got #{e}"
end

#b64_encode(string) ⇒ Object


15
16
17
# File 'lib/dragonfly/serializer.rb', line 15

def b64_encode(string)
  Base64.urlsafe_encode64(string).tr('=','')
end

#json_b64_decode(string) ⇒ Object


50
51
52
# File 'lib/dragonfly/serializer.rb', line 50

def json_b64_decode(string)
  json_decode(b64_decode(string))
end

#json_b64_encode(object) ⇒ Object


46
47
48
# File 'lib/dragonfly/serializer.rb', line 46

def json_b64_encode(object)
  b64_encode(json_encode(object))
end

#json_decode(string) ⇒ Object


39
40
41
42
43
44
# File 'lib/dragonfly/serializer.rb', line 39

def json_decode(string)
  raise BadString, "can't decode blank string" if Utils.blank?(string)
  MultiJson.decode(string)
rescue MultiJson::DecodeError => e
  raise BadString, "couldn't json decode string - got #{e}"
end

#json_encode(object) ⇒ Object


35
36
37
# File 'lib/dragonfly/serializer.rb', line 35

def json_encode(object)
  MultiJson.encode(object)
end

#marshal_b64_decode(string) ⇒ Object


27
28
29
30
31
32
33
# File 'lib/dragonfly/serializer.rb', line 27

def marshal_b64_decode(string)
  marshal_string = b64_decode(string)
  raise MaliciousString, "potentially malicious marshal string #{marshal_string.inspect}" if marshal_string[/@[a-z_]/i]
  Marshal.load(marshal_string)
rescue TypeError, ArgumentError => e
  raise BadString, "couldn't marshal decode string - got #{e}"
end