class Merb::Cookies

Public Class Methods

new(constructor = {}) click to toggle source

:api: private

Calls superclass method
# File lib/merb-core/dispatch/cookies.rb, line 6
def initialize(constructor = {})
  @_options_lookup  = Mash.new
  @_cookie_defaults = { "domain" => Merb::Controller._default_cookie_domain, "path" => '/' }
  super constructor
end

Public Instance Methods

[]=(key, value) click to toggle source

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.

:api: public

Calls superclass method
# File lib/merb-core/dispatch/cookies.rb, line 23
def []=(key, value)
  @_options_lookup[key] ||= {}
  super
end
delete(name, options = {}) click to toggle source

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.

:api: public

# File lib/merb-core/dispatch/cookies.rb, line 60
def delete(name, options = {})
  set_cookie(name, "", options.merge("expires" => Time.at(0)))
end
extract_headers(controller_defaults = {}) click to toggle source

Generate any necessary headers.

Returns

Hash

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

:api: private

# File lib/merb-core/dispatch/cookies.rb, line 70
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")
    http_only = options.delete("http_only")
    kookie  = "#{name}=#{Merb::Parse.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
    kookie  << 'HttpOnly; ' if http_only
    cookies << kookie.rstrip
  end
  cookies.empty? ? {} : { 'Set-Cookie' => cookies.join(Merb::Const::NEWLINE) }
end