Class: Norman::Adapters::Cookie
- Inherits:
-
Norman::Adapter
- Object
- Norman::Adapter
- Norman::Adapters::Cookie
- Defined in:
- lib/norman/adapters/cookie.rb
Overview
Norman’s cookie adapter allows you to store a Norman 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 collapse
- MAX_DATA_LENGTH =
4096
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#verifier ⇒ Object
readonly
Returns the value of attribute verifier.
Attributes inherited from Norman::Adapter
Class Method Summary collapse
Instance Method Summary collapse
- #export_data ⇒ Object
- #import_data ⇒ Object
-
#initialize(options) ⇒ Cookie
constructor
A new instance of Cookie.
- #load_database ⇒ Object
- #save_database ⇒ Object
Methods inherited from Norman::Adapter
Constructor Details
#initialize(options) ⇒ Cookie
Returns a new instance of Cookie.
26 27 28 29 30 |
# File 'lib/norman/adapters/cookie.rb', line 26 def initialize() @data = [:data] @verifier = ActiveSupport::MessageVerifier.new([:secret]) super end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
18 19 20 |
# File 'lib/norman/adapters/cookie.rb', line 18 def data @data end |
#verifier ⇒ Object (readonly)
Returns the value of attribute verifier.
17 18 19 |
# File 'lib/norman/adapters/cookie.rb', line 17 def verifier @verifier end |
Class Method Details
.max_data_length ⇒ Object
22 23 24 |
# File 'lib/norman/adapters/cookie.rb', line 22 def self.max_data_length MAX_DATA_LENGTH end |
Instance Method Details
#export_data ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/norman/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(NormanError, "Data is %s bytes, cannot exceed %s" % [length, Cookie.max_data_length]) end end |
#import_data ⇒ Object
41 42 43 |
# File 'lib/norman/adapters/cookie.rb', line 41 def import_data data.blank? ? {} : Marshal.load(Zlib::Inflate.inflate(verifier.verify(data))) end |
#load_database ⇒ Object
45 46 47 48 |
# File 'lib/norman/adapters/cookie.rb', line 45 def load_database @db = import_data @db.map(&:freeze) end |
#save_database ⇒ Object
50 51 52 |
# File 'lib/norman/adapters/cookie.rb', line 50 def save_database @data = export_data end |