Class: ActionDispatch::Cookies::CookieJar
Overview
:nodoc:
Constant Summary
- DOMAIN_REGEXP =
This regular expression is used to split the levels of a domain. The top level domain can be any string without a period or *.*, **.* style TLDs like co.uk or com.au
www.example.co.uk gives: $1 => example $2 => co.uk
example.com gives: $1 => example $2 => com
lots.of.subdomains.example.local gives: $1 => example $2 => local
/([^.]*)\.([^.]*|..\...|...\...)$/
Class Method Summary (collapse)
Instance Method Summary (collapse)
-
- (Object) [](name)
Returns the value of the cookie by name, or nil if no such cookie exists.
-
- (Object) []=(key, options)
Sets the cookie named name.
-
- (Object) delete(key, options = {})
Removes the cookie on the client machine by setting the value to an empty string and setting its expiration date into the past.
-
- (Object) handle_options(options)
:nodoc:.
-
- (CookieJar) initialize(secret = nil, host = nil, secure = false)
constructor
A new instance of CookieJar.
-
- (Object) permanent
Returns a jar that'll automatically set the assigned cookies to have an expiration date 20 years from now.
-
- (Object) signed
Returns a jar that'll automatically generate a signed representation of cookie value and verify it when reading from the cookie again.
- - (Object) write(headers)
Methods inherited from Hash
#as_json, #assert_valid_keys, #deep_merge, #deep_merge!, #diff, #encode_json, #except, #except!, #extract!, #extractable_options?, from_xml, #reverse_merge, #reverse_merge!, #slice, #slice!, #stringify_keys, #stringify_keys!, #symbolize_keys, #symbolize_keys!, #to_param, #to_xml, #with_indifferent_access
Constructor Details
- (CookieJar) initialize(secret = nil, host = nil, secure = false)
A new instance of CookieJar
108 109 110 111 112 113 114 115 116 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 108 def initialize(secret = nil, host = nil, secure = false) @secret = secret @set_cookies = {} @delete_cookies = {} @host = host @secure = secure super() end |
Class Method Details
+ (Object) build(request)
98 99 100 101 102 103 104 105 106 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 98 def self.build(request) secret = request.env[TOKEN_KEY] host = request.host secure = request.ssl? new(secret, host, secure).tap do |hash| hash.update(request.) end end |
Instance Method Details
- (Object) [](name)
Returns the value of the cookie by name, or nil if no such cookie exists.
119 120 121 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 119 def [](name) super(name.to_s) end |
- (Object) []=(key, options)
Sets the cookie named name. The second argument may be the very cookie value, or a hash of options as documented above.
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 134 def []=(key, ) if .is_a?(Hash) .symbolize_keys! value = [:value] else value = = { :value => value } end value = super(key.to_s, value) () @set_cookies[key] = @delete_cookies.delete(key) value end |
- (Object) delete(key, options = {})
Removes the cookie on the client machine by setting the value to an empty string and setting its expiration date into the past. Like []=, you can pass in an options hash to delete cookies with extra data such as a :path.
155 156 157 158 159 160 161 162 163 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 155 def delete(key, = {}) .symbolize_keys! () value = super(key.to_s) @delete_cookies[key] = value end |
- (Object) handle_options(options)
:nodoc:
123 124 125 126 127 128 129 130 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 123 def () #:nodoc: [:path] ||= "/" if [:domain] == :all @host =~ DOMAIN_REGEXP [:domain] = ".#{$1}.#{$2}" end end |
- (Object) permanent
Returns a jar that'll automatically set the assigned cookies to have an expiration date 20 years from now. Example:
.permanent[:prefers_open_id] = true
# => Set-Cookie: prefers_open_id=true; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT
This jar is only meant for writing. You'll read permanent cookies through the regular accessor.
This jar allows chaining with the signed jar as well, so you can set permanent, signed cookies. Examples:
.permanent.signed[:remember_me] = current_user.id
# => Set-Cookie: remember_me=BAhU--848956038e692d7046deab32b7131856ab20e14e; path=/; expires=Sun, 16-Dec-2029 03:24:16 GMT
176 177 178 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 176 def permanent @permanent ||= PermanentCookieJar.new(self, @secret) end |
- (Object) signed
Returns a jar that'll automatically generate a signed representation of cookie value and verify it when reading from the cookie again. This is useful for creating cookies with values that the user is not supposed to change. If a signed cookie was tampered with by the user (or a 3rd party), an ActiveSupport::MessageVerifier::InvalidSignature exception will be raised.
This jar requires that you set a suitable secret for the verification on your app's config.secret_token.
Example:
.signed[:discount] = 45
# => Set-Cookie: discount=BAhpMg==--2c1c6906c90a3bc4fd54a51ffb41dffa4bf6b5f7; path=/
.signed[:discount] # => 45
193 194 195 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 193 def signed @signed ||= SignedCookieJar.new(self, @secret) end |
- (Object) write(headers)
197 198 199 200 |
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 197 def write(headers) @set_cookies.each { |k, v| ::Rack::Utils.(headers, k, v) if (v) } @delete_cookies.each { |k, v| ::Rack::Utils.(headers, k, v) } end |