Module: Dragonfly::Serializer

Extended by:
Serializer
Included in:
DataStorage::CouchDataStore, DataStorage::MongoDataStore, DataStorage::S3DataStore, Serializer
Defined in:
lib/dragonfly/serializer.rb

Defined Under Namespace

Classes: BadString, MaliciousString

Instance Method Summary collapse

Instance Method Details

#b64_decode(string) ⇒ Object


18
19
20
21
22
# File 'lib/dragonfly/serializer.rb', line 18

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

#b64_encode(string) ⇒ Object


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

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

#json_decode(string, opts = {}) ⇒ Object


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

def json_decode(string, opts={})
  MultiJson.decode(b64_decode(string), :symbolize_keys => opts[:symbolize_keys])
rescue MultiJson::DecodeError => e
  raise BadString, "couldn't decode #{string} - got #{e}"
end

#json_encode(object) ⇒ Object


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

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

#marshal_decode(string, opts = {}) ⇒ Object


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

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

#marshal_encode(object) ⇒ Object


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

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