Module: CatForms::GzipCookie

Defined in:
lib/cat_forms/gzip_cookie.rb

Overview

Saves form as cookie as json+gzip

Class Method Summary collapse

Class Method Details

.load(options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cat_forms/gzip_cookie.rb', line 7

def self.load options={}
  request     = options[:request]
  cookie_name = options[:cookie_name].to_s
  result = ActiveSupport::HashWithIndifferentAccess.new
  return result if request.blank?
  cookie = request.cookies[cookie_name]
  return result if cookie.nil? or cookie.empty?
  begin
    result.merge!(ActiveSupport::JSON.decode(Zlib::Inflate.inflate(cookie)).stringify_keys)
    return result
  rescue Zlib::DataError
    return result
  end
end

.save(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cat_forms/gzip_cookie.rb', line 22

def self.save options = {}
  attributes  = options[:attributes]
  response    = options[:response]
  cookie_name = options[:cookie_name]

  cookie_json = ActiveSupport::JSON.encode(attributes)
  cookie_json = Zlib::Deflate.deflate(cookie_json, Zlib::BEST_COMPRESSION)
  cookie_hash = { value: cookie_json,
                  httponly: true,
                }
  response.set_cookie(cookie_name, cookie_hash)
end