Class: Merb::Cookies
Instance Method Summary collapse
-
#[]=(key, value) ⇒ Object
Implicit assignment of cookie key and value.
-
#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.
-
#extract_headers(controller_defaults = {}) ⇒ Object
Generate any necessary headers.
-
#initialize(constructor = {}) ⇒ Cookies
constructor
:api: private.
-
#set_cookie(name, value, options = {}) ⇒ Object
Explicit assignment of cookie key, value and options.
Constructor Details
#initialize(constructor = {}) ⇒ Cookies
:api: private
6 7 8 9 10 |
# File 'lib/merb-core/dispatch/cookies.rb', line 6 def initialize(constructor = {}) @_options_lookup = Mash.new @_cookie_defaults = { "domain" => Merb::Controller., "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.
:api: public
23 24 25 26 |
# File 'lib/merb-core/dispatch/cookies.rb', line 23 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
.
:api: public
60 61 62 |
# File 'lib/merb-core/dispatch/cookies.rb', line 60 def delete(name, = {}) (name, "", .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.
:api: private
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/merb-core/dispatch/cookies.rb', line 70 def extract_headers(controller_defaults = {}) defaults = @_cookie_defaults.merge(controller_defaults) = [] self.each do |name, value| # Only set cookies that marked for inclusion in the response header. next unless @_options_lookup[name] = defaults.merge(@_options_lookup[name]) if (expiry = ["expires"]).respond_to?(:gmtime) ["expires"] = expiry.gmtime.strftime(Merb::Const::COOKIE_EXPIRATION_FORMAT) end secure = .delete("secure") http_only = .delete("http_only") kookie = "#{name}=#{Merb::Parse.escape(value)}; " # WebKit in particular doens't like empty cookie options - skip them. .each { |k, v| kookie << "#{k}=#{v}; " unless v.blank? } kookie << 'secure; ' if secure kookie << 'HttpOnly; ' if http_only << kookie.rstrip end .empty? ? {} : { 'Set-Cookie' => .join(Merb::Const::NEWLINE) } end |
#set_cookie(name, value, options = {}) ⇒ Object
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.
- :http_only<Boolean>
-
HttpOnly cookies
Notes
By using this method, a cookie key is marked for being included in the Set-Cookie response header.
:api: private
47 48 49 50 |
# File 'lib/merb-core/dispatch/cookies.rb', line 47 def (name, value, = {}) @_options_lookup[name] = self[name] = value end |