Class: Merb::Cookies
Overview
Cookies are read and written through Merb::Controller#cookies. The cookies you read are those received in request along with those that have been set during the current request. The cookies you write will be sent out with the response. Cookies are read by value (so you won’t get the cookie object itself back – just the value it holds).
Instance Method Summary collapse
-
#[](name) ⇒ Object
Parameters name<~to_s>:: Name of the cookie.
-
#[]=(name, options) ⇒ Object
Parameters name<~to_s>:: Name of the cookie.
-
#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.
-
#initialize(request_cookies, headers) ⇒ Cookies
constructor
Parameters request_cookies<Hash>:: Initial cookie store.
-
#set_cookie(name, value, options) ⇒ Object
Parameters name<~to_s>:: Name of the cookie.
Constructor Details
#initialize(request_cookies, headers) ⇒ Cookies
Parameters
- request_cookies<Hash>
-
Initial cookie store.
- headers<Hash>
-
The response headers.
13 14 15 16 |
# File 'lib/merb-core/dispatch/cookies.rb', line 13 def initialize(, headers) @_cookies = @_headers = headers end |
Instance Method Details
#[](name) ⇒ Object
Parameters
- name<~to_s>
-
Name of the cookie.
Returns
- String
-
Value of the cookie.
23 24 25 |
# File 'lib/merb-core/dispatch/cookies.rb', line 23 def [](name) @_cookies[name] end |
#[]=(name, options) ⇒ Object
Parameters
- name<~to_s>
-
Name of the cookie.
- options<Hash, ~to_s>
-
Options for the cookie being set (see below).
Options (options)
- :value<~to_s>
-
Value of the cookie
- :path<String>
-
The path for which this cookie applies. Defaults to “/”.
- :expires<Time>
-
Cookie expiry date.
Alternatives
If options is not a hash, it will be used as the cookie value directly.
Examples
[:user] = "dave" # => Sets a simple session cookie
[:token] = { :value => user.token, :expires => Time.now + 2.weeks }
# => Will set a cookie that expires in 2 weeks
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/merb-core/dispatch/cookies.rb', line 43 def []=(name, ) value = '' if .is_a?(Hash) = Mash.new() value = .delete(:value) else value = = Mash.new end @_cookies[name] = value (name, Merb::Request.escape(value), ) Merb.logger.info("Cookie set: #{name} => #{value} -- #{.inspect}") 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
.
64 65 66 67 68 69 70 71 |
# File 'lib/merb-core/dispatch/cookies.rb', line 64 def delete(name, = {}) = @_cookies.delete(name) = Mash.new() [:expires] = Time.at(0) (name, "", ) Merb.logger.info("Cookie deleted: #{name} => #{.inspect}") end |
#set_cookie(name, value, options) ⇒ Object
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.
81 82 83 84 85 86 87 88 89 |
# File 'lib/merb-core/dispatch/cookies.rb', line 81 def (name, value, ) [:path] = '/' unless [:path] if expiry = [:expires] [:expires] = expiry.gmtime.strftime(Merb::Const::COOKIE_EXPIRATION_FORMAT) end # options are sorted for testing purposes (@_headers['Set-Cookie'] ||=[]) << "#{name}=#{value}; " + .map{|k, v| "#{k}=#{v};"}.sort.join(' ') end |