Class: CookiesManager::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cookies_manager/base.rb

Overview

The base class of CookiesManager

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_cookies) ⇒ Base

Constructs a new CookiesManager instance based on a cookies hash

The CookiesManager instance is created automatically when you call the load_cookies_manager class method on your controller, so you don’t need to instantiate it directly.

Example:

Inside your controller, call the load_cookies_manager method:

class YourController < ActionController::Base
  load_cookies_manager


18
19
20
# File 'lib/cookies_manager/base.rb', line 18

def initialize(input_cookies)
  self.cookies = input_cookies
end

Instance Attribute Details

#cookiesObject

The cookies hash to be based on



5
6
7
# File 'lib/cookies_manager/base.rb', line 5

def cookies
  @cookies
end

Instance Method Details

#delete(key, opts = {}) ⇒ Object

Deletes the data corresponding to the key, and return it.

  • Removes the data from both the cookies and the cache.

  • The returned value is read from the cache if this is in sync with the cookies. Otherwise, the data is read from the cookies, in which case it is successively base64-decoded, unzipped, and unmarshalled by default. If you consider your data does not need to be transformed, you can disable this feature by passing the :skip_unpack option.

Example:

data = cookies_manager.delete('my_key')  # deletes and returns the data associated with the key 'my_key'
raw_data = cookies_manager.delete('my_key', :skip_unpack => true)  # same as above except that the returned data is not unpacked

Parameters:

  • key (String or Symbol)

    a unique key corresponding to the data to delete

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :skip_unpack (Boolean)

    if true, DO NOT base64-decode, unzip, nor unmarshall the data. Default is false. This option is used only when the cache is out of sync.

Returns:

  • (Object)

    the data associated with the key



94
95
96
97
98
99
100
101
102
103
# File 'lib/cookies_manager/base.rb', line 94

def delete(key, opts = {})
  opts.symbolize_keys!
  result = nil
  getMutex(key).synchronize do
    result = read_from_cache_or_cookies(key, opts)
    cookies.delete(key)
    cache.delete(key)
  end
  return result
end

#read(key, opts = {}) ⇒ Object

Reads the data object corresponding to the key.

  • Reads from the cache, or from the cookies if the cache is not in sync.

  • Cache desynchronization can occur when the cookies hash has been modified directly, in which case the cache is automatically re-synchronized.

  • When the data needs to be retrieved from the cookies, it is successively base64-decoded, unzipped, and unmarshalled by default. If you consider your data does not need to be transformed, you can disable this feature by passing the :skip_unpack option.

Examples:

data = cookies_manager.read('my_key') # reads the data associated with the key 'my_key'
raw_data = cookies_manager.read('my_key', :skip_unpack => true) # reads without unpacking

Parameters:

  • key (String or Symbol)

    a unique key corresponding to the data to read

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :skip_unpack (Boolean)

    if true, DO NOT base64-decode, unzip, nor unmarshall the data. Default is false. This option is used only when the cache is out of sync.

Returns:

  • (Object)

    the data associated with the key



35
36
37
38
39
40
41
42
# File 'lib/cookies_manager/base.rb', line 35

def read(key, opts = {})
  opts.symbolize_keys!
  result = nil
  getMutex(key).synchronize do
    result = read_from_cache_or_cookies(key, opts)  
  end      
  return result
end

#write(key, data, opts = {}) ⇒ Integer

Writes the data object and associates it with the key.

  • Data is stored in both the cookies and the cache.

  • By default, before being stored in the cookies, data is marshalled, zipped, and base64-encoded. Although this feature is recommended, you can disable it by passing the option :skip_pack if you consider your data can be stored as is in the cookies (ex: US-ASCII string).

Examples:

array_data = {:some_item => "an item", :some_array => ['This', 'is', 'an', 'array']}
#store the data in the cookies as a base64-encoded string, for one hour:
len_bytes1 = cookies_manager.write('key_for_my_array', data, :expires => 1.hour.from_now)

simple_data = "a simple string"
# store the data as in in the cookies, and keep it as long as the browser remains open:
len_bytes2 = cookies_manager.write('key_for_my_simple_data', simple_data, :skip_pack => true)

Parameters:

  • key (String or Symbol)

    a unique key to associate the data with

  • opts (Hash) (defaults to: {})

    a customizable set of options, built on top of the native set of options supported when setting cookies

Options Hash (opts):

  • :skip_pack (Boolean)

    if true, DO NOT marshall, zip, nor base64-encode the data. Default is false.

  • :path (String)

    the path for which this cookie applies. Defaults to the root of the application.

  • :domain (String)

    the domain for which this cookie applies.

  • :expires (String)

    the time at which this cookie expires, as a Time object.

  • :secure (String)

    whether this cookie is a only transmitted to HTTPS servers. Default is false.

  • :httponly (String)

    whether this cookie is accessible via scripting or only HTTP. Defaults to false.

Returns:

  • (Integer)

    the number of bytes written in the cookies



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cookies_manager/base.rb', line 68

def write(key, data, opts = {})
  opts.symbolize_keys!
  unpacked_data = data
  data = pack(data) unless opts[:skip_pack]
  result = nil
  getMutex(key).synchronize do
    cache[key] ||= {}
    result = cookies.signed[key] = {:value => data}.merge(opts) # store the packed data in the cookies hash
    cache[key][:unpacked_data] = unpacked_data # store the unpacked data in the cache for fast read in the read method
    cache[key][:packed_data] = data # store the packed data in the cache for fast change diff in the read method
  end
  return result[:value].try(:bytesize) || 0
end