Class: CookieMonster::Jar

Inherits:
Base
  • Object
show all
Defined in:
lib/cookie_monster/jar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookies) ⇒ Jar

Returns a new instance of Jar.



5
6
7
# File 'lib/cookie_monster/jar.rb', line 5

def initialize(cookies)
  @cookies = cookies
end

Instance Attribute Details

#cookiesObject (readonly)

Returns the value of attribute cookies.



3
4
5
# File 'lib/cookie_monster/jar.rb', line 3

def cookies
  @cookies
end

Instance Method Details

#[](key) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cookie_monster/jar.rb', line 9

def [](key)
  if @cookies[key]
    cookie = @cookies[key]
  else
    return nil
  end

  if cookie.is_a? Hash
    cookie = cookie[:value]
  end

  encrypted = Encryption.new(cookie)
  encrypted.decrypt
end

#[]=(key, value_or_options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cookie_monster/jar.rb', line 24

def []=(key, value_or_options)
  value, options = value_or_options
  options ||= {}

  encrypted_value = Encryption.new(value).encrypt

  @cookies[key] = {
    :value => encrypted_value,
    :httponly => options[:httponly],
    :expires => options[:expires],
    :domain => options[:domain],
    :path => options[:path] || '/',
    :secure => false # Needed so it can be read by cookie logger over http
  }
end