Class: Ambry::Adapters::Cookie
- Inherits:
-
Ambry::Adapter
- Object
- Ambry::Adapter
- Ambry::Adapters::Cookie
- Defined in:
- lib/ambry/adapters/cookie.rb
Overview
Ambry's cookie adapter allows you to store a Ambry database inside a zipped and signed string suitable for setting as a cookie. This can be useful for modelling things like basic shopping carts or form wizards. Keep in mind the data is signed, so it can't be tampered with. However, the data is not encrypted, so somebody that wanted to could unzip and load the cookie data to see what's inside. So don't send this data client-side if it's at all sensitive.
Constant Summary
- MAX_DATA_LENGTH =
4096
Instance Attribute Summary (collapse)
-
- (Object) data
Returns the value of attribute data.
-
- (Object) verifier
readonly
Returns the value of attribute verifier.
Attributes inherited from Ambry::Adapter
Class Method Summary (collapse)
Instance Method Summary (collapse)
- - (Object) export_data
- - (Object) import_data
-
- (Cookie) initialize(options)
constructor
A new instance of Cookie.
- - (Object) load_database
- - (Object) save_database
Methods inherited from Ambry::Adapter
Constructor Details
- (Cookie) initialize(options)
A new instance of Cookie
26 27 28 29 30 |
# File 'lib/ambry/adapters/cookie.rb', line 26 def initialize() @data = [:data] @verifier = ActiveSupport::MessageVerifier.new([:secret]) super end |
Instance Attribute Details
- (Object) data
Returns the value of attribute data
18 19 20 |
# File 'lib/ambry/adapters/cookie.rb', line 18 def data @data end |
- (Object) verifier (readonly)
Returns the value of attribute verifier
17 18 19 |
# File 'lib/ambry/adapters/cookie.rb', line 17 def verifier @verifier end |
Class Method Details
+ (Object) max_data_length
22 23 24 |
# File 'lib/ambry/adapters/cookie.rb', line 22 def self.max_data_length MAX_DATA_LENGTH end |
Instance Method Details
- (Object) export_data
32 33 34 35 36 37 38 39 |
# File 'lib/ambry/adapters/cookie.rb', line 32 def export_data = verifier.generate(Zlib::Deflate.deflate(Marshal.dump(db))) length = .bytesize if length > Cookie.max_data_length raise(AmbryError, "Data is %s bytes, cannot exceed %s" % [length, Cookie.max_data_length]) end end |
- (Object) import_data
41 42 43 |
# File 'lib/ambry/adapters/cookie.rb', line 41 def import_data data.blank? ? {} : Marshal.load(Zlib::Inflate.inflate(verifier.verify(data))) end |
- (Object) load_database
45 46 47 48 |
# File 'lib/ambry/adapters/cookie.rb', line 45 def load_database @db = import_data @db.map(&:freeze) end |
- (Object) save_database
50 51 52 |
# File 'lib/ambry/adapters/cookie.rb', line 50 def save_database @data = export_data end |