Class: MonkeyForms::Serializers::GzipCookie

Inherits:
Object
  • Object
show all
Defined in:
lib/monkey_forms/serializers/gzip_cookie.rb

Overview

Saves form as cookie as json+gzip

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GzipCookie

Returns a new instance of GzipCookie.



6
7
8
9
10
11
12
# File 'lib/monkey_forms/serializers/gzip_cookie.rb', line 6

def initialize options={}
  @cookie_name = options[:name].to_s
  @domain      = options[:domain].to_s
  @secure      = options[:secure]
  @httponly    = options[:httponly]
  @path        = (options[:path] || '/').to_s
end

Instance Method Details

#load(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/monkey_forms/serializers/gzip_cookie.rb', line 14

def load options={}
  request = options[:request]
  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)
  rescue Zlib::DataError
    return result
  end
end

#save(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/monkey_forms/serializers/gzip_cookie.rb', line 27

def save options = {}
  attributes = options[:attributes]
  response   = options[:response]

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