Class: Merb::Cookies

Inherits:
Mash show all
Defined in:
lib/merb-core/dispatch/cookies.rb

Instance Method Summary collapse

Constructor Details

#initialize(constructor = {}) ⇒ Cookies

Returns a new instance of Cookies.



5
6
7
8
9
# File 'lib/merb-core/dispatch/cookies.rb', line 5

def initialize(constructor = {})
  @_options_lookup  = Mash.new
  @_cookie_defaults = { "domain" => Merb::Controller._default_cookie_domain, "path" => '/' }
  super constructor
end

Instance Method Details

#[]=(key, value) ⇒ Object

Implicit assignment of cookie key and value.

Parameters

name<~to_s>

Name of the cookie.

value<~to_s>

Value of the cookie.

Notes

By using this method, a cookie key is marked for being included in the Set-Cookie response header.



20
21
22
23
# File 'lib/merb-core/dispatch/cookies.rb', line 20

def []=(key, value)
  @_options_lookup[key] ||= {}
  super
end

#delete(name, options = {}) ⇒ Object

Removes the cookie on the client machine by setting the value to an empty string and setting its expiration date into the past.

Parameters

name<~to_s>

Name of the cookie to delete.

options<Hash>

Additional options to pass to set_cookie.



52
53
54
# File 'lib/merb-core/dispatch/cookies.rb', line 52

def delete(name, options = {})
  set_cookie(name, "", options.merge("expires" => Time.at(0)))
end

#extract_headers(controller_defaults = {}) ⇒ Object

Generate any necessary headers.

Returns

Hash

The headers to set, or an empty array if no cookies are set.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/merb-core/dispatch/cookies.rb', line 60

def extract_headers(controller_defaults = {})
  defaults = @_cookie_defaults.merge(controller_defaults)
  cookies = []
  self.each do |name, value|
    # Only set cookies that marked for inclusion in the response header. 
    next unless @_options_lookup[name]
    options = defaults.merge(@_options_lookup[name])
    if (expiry = options["expires"]).respond_to?(:gmtime)
      options["expires"] = expiry.gmtime.strftime(Merb::Const::COOKIE_EXPIRATION_FORMAT)
    end
    secure  = options.delete("secure")
    kookie  = "#{name}=#{Merb::Request.escape(value)}; "
    # WebKit in particular doens't like empty cookie options - skip them.
    options.each { |k, v| kookie << "#{k}=#{v}; " unless v.blank? }
    kookie  << 'secure' if secure
    cookies << kookie.rstrip
  end
  cookies.empty? ? {} : { 'Set-Cookie' => cookies }
end

Explicit assignment of cookie key, value and options

Parameters

name<~to_s>

Name of the cookie.

value<~to_s>

Value of the cookie.

options<Hash>

Additional options for the cookie (see below).

Options (options)

:path<String>

The path for which this cookie applies. Defaults to “/”.

:expires<Time>

Cookie expiry date.

:domain<String>

The domain for which this cookie applies.

:secure<Boolean>

Security flag.

Notes

By using this method, a cookie key is marked for being included in the Set-Cookie response header.



41
42
43
44
# File 'lib/merb-core/dispatch/cookies.rb', line 41

def set_cookie(name, value, options = {})
  @_options_lookup[name] = options
  self[name] = value
end