Class: ActionDispatch::Cookies::CookieJar
- Inherits:
-
Object
- Object
- ActionDispatch::Cookies::CookieJar
- Includes:
- ChainedCookieJars, Enumerable
- Defined in:
- lib/action_dispatch/middleware/cookies.rb
Overview
:nodoc:
Direct Known Subclasses
ActionController::RequestForgeryProtection::ProtectionMethods::NullSession::NullCookieJar
Instance Attribute Summary collapse
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](name) ⇒ Object
Returns the value of the cookie by ‘name`, or `nil` if no such cookie exists.
-
#[]=(name, options) ⇒ Object
Sets the cookie named ‘name`.
-
#clear(options = {}) ⇒ Object
Removes all cookies on the client machine by calling ‘delete` for each cookie.
- #commit! ⇒ Object
- #committed? ⇒ Boolean
-
#delete(name, options = {}) ⇒ Object
Removes the cookie on the client machine by setting the value to an empty string and the expiration date in the past.
-
#deleted?(name, options = {}) ⇒ Boolean
Whether the given cookie is to be deleted by this CookieJar.
- #each(&block) ⇒ Object
- #fetch(name, *args, &block) ⇒ Object
-
#initialize(request) ⇒ CookieJar
constructor
A new instance of CookieJar.
- #key?(name) ⇒ Boolean (also: #has_key?)
- #to_header ⇒ Object
- #update(other_hash) ⇒ Object
- #update_cookies_from_jar ⇒ Object
- #write(response) ⇒ Object
Methods included from ChainedCookieJars
#encrypted, #permanent, #signed, #signed_or_encrypted
Constructor Details
#initialize(request) ⇒ CookieJar
Returns a new instance of CookieJar.
322 323 324 325 326 327 328 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 322 def initialize(request) @set_cookies = {} @delete_cookies = {} @request = request @cookies = {} @committed = false end |
Instance Attribute Details
#request ⇒ Object (readonly)
Returns the value of attribute request.
320 321 322 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 320 def request @request end |
Class Method Details
.build(req, cookies) ⇒ Object
314 315 316 317 318 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 314 def self.build(req, ) jar = new(req) jar.update() jar end |
Instance Method Details
#[](name) ⇒ Object
Returns the value of the cookie by ‘name`, or `nil` if no such cookie exists.
343 344 345 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 343 def [](name) @cookies[name.to_s] end |
#[]=(name, options) ⇒ Object
Sets the cookie named ‘name`. The second argument may be the cookie’s value or a hash of options as documented above.
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 377 def []=(name, ) if .is_a?(Hash) .symbolize_keys! value = [:value] else value = = { value: value } end () if @cookies[name.to_s] != value || [:expires] @cookies[name.to_s] = value @set_cookies[name.to_s] = @delete_cookies.delete(name.to_s) end value end |
#clear(options = {}) ⇒ Object
Removes all cookies on the client machine by calling ‘delete` for each cookie.
423 424 425 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 423 def clear( = {}) @cookies.each_key { |k| delete(k, ) } end |
#commit! ⇒ Object
332 333 334 335 336 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 332 def commit! @committed = true @set_cookies.freeze @delete_cookies.freeze end |
#committed? ⇒ Boolean
330 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 330 def committed?; @committed; end |
#delete(name, options = {}) ⇒ Object
Removes the cookie on the client machine by setting the value to an empty string and the expiration date in the past. Like ‘[]=`, you can pass in an options hash to delete cookies with extra data such as a `:path`.
Returns the value of the cookie, or ‘nil` if the cookie does not exist.
402 403 404 405 406 407 408 409 410 411 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 402 def delete(name, = {}) return unless @cookies.has_key? name.to_s .symbolize_keys! () value = @cookies.delete(name.to_s) @delete_cookies[name.to_s] = value end |
#deleted?(name, options = {}) ⇒ Boolean
Whether the given cookie is to be deleted by this CookieJar. Like ‘[]=`, you can pass in an options hash to test if a deletion applies to a specific `:path`, `:domain` etc.
416 417 418 419 420 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 416 def deleted?(name, = {}) .symbolize_keys! () @delete_cookies[name.to_s] == end |
#each(&block) ⇒ Object
338 339 340 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 338 def each(&block) @cookies.each(&block) end |
#fetch(name, *args, &block) ⇒ Object
347 348 349 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 347 def fetch(name, *args, &block) @cookies.fetch(name.to_s, *args, &block) end |
#key?(name) ⇒ Boolean Also known as: has_key?
351 352 353 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 351 def key?(name) @cookies.key?(name.to_s) end |
#to_header ⇒ Object
371 372 373 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 371 def to_header @cookies.map { |k, v| "#{escape(k)}=#{escape(v)}" }.join "; " end |
#update(other_hash) ⇒ Object
359 360 361 362 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 359 def update(other_hash) @cookies.update other_hash.stringify_keys self end |
#update_cookies_from_jar ⇒ Object
364 365 366 367 368 369 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 364 def request_jar = @request..instance_variable_get(:@cookies) = request_jar.reject { |k, _| @delete_cookies.key?(k) || @set_cookies.key?(k) } @cookies.update if end |
#write(response) ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/action_dispatch/middleware/cookies.rb', line 427 def write(response) @set_cookies.each do |name, value| if (value) response.(name, value) end end @delete_cookies.each do |name, value| response.(name, value) end end |