Class: ActionDispatch::Cookies::CookieJar

Inherits:
Object
  • Object
show all
Includes:
ChainedCookieJars, Enumerable
Defined in:
lib/action_dispatch/middleware/cookies.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ChainedCookieJars

#encrypted, #permanent, #signed, #signed_or_encrypted

Constructor Details

#initialize(request) ⇒ CookieJar

Returns a new instance of CookieJar.



301
302
303
304
305
306
307
# File 'lib/action_dispatch/middleware/cookies.rb', line 301

def initialize(request)
  @set_cookies = {}
  @delete_cookies = {}
  @request = request
  @cookies = {}
  @committed = false
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



299
300
301
# File 'lib/action_dispatch/middleware/cookies.rb', line 299

def request
  @request
end

Class Method Details

.build(req, cookies) ⇒ Object



293
294
295
296
297
# File 'lib/action_dispatch/middleware/cookies.rb', line 293

def self.build(req, cookies)
  jar = new(req)
  jar.update(cookies)
  jar
end

Instance Method Details

#[](name) ⇒ Object

Returns the value of the cookie by name, or nil if no such cookie exists.



322
323
324
# File 'lib/action_dispatch/middleware/cookies.rb', line 322

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.



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/action_dispatch/middleware/cookies.rb', line 356

def []=(name, options)
  if options.is_a?(Hash)
    options.symbolize_keys!
    value = options[:value]
  else
    value = options
    options = { value: value }
  end

  handle_options(options)

  if @cookies[name.to_s] != value || options[:expires]
    @cookies[name.to_s] = value
    @set_cookies[name.to_s] = options
    @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.



400
401
402
# File 'lib/action_dispatch/middleware/cookies.rb', line 400

def clear(options = {})
  @cookies.each_key { |k| delete(k, options) }
end

#commit!Object



311
312
313
314
315
# File 'lib/action_dispatch/middleware/cookies.rb', line 311

def commit!
  @committed = true
  @set_cookies.freeze
  @delete_cookies.freeze
end

#committed?Boolean

Returns:

  • (Boolean)


309
# File 'lib/action_dispatch/middleware/cookies.rb', line 309

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.



379
380
381
382
383
384
385
386
387
388
# File 'lib/action_dispatch/middleware/cookies.rb', line 379

def delete(name, options = {})
  return unless @cookies.has_key? name.to_s

  options.symbolize_keys!
  handle_options(options)

  value = @cookies.delete(name.to_s)
  @delete_cookies[name.to_s] = options
  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.

Returns:

  • (Boolean)


393
394
395
396
397
# File 'lib/action_dispatch/middleware/cookies.rb', line 393

def deleted?(name, options = {})
  options.symbolize_keys!
  handle_options(options)
  @delete_cookies[name.to_s] == options
end

#each(&block) ⇒ Object



317
318
319
# File 'lib/action_dispatch/middleware/cookies.rb', line 317

def each(&block)
  @cookies.each(&block)
end

#fetch(name, *args, &block) ⇒ Object



326
327
328
# File 'lib/action_dispatch/middleware/cookies.rb', line 326

def fetch(name, *args, &block)
  @cookies.fetch(name.to_s, *args, &block)
end

#key?(name) ⇒ Boolean Also known as: has_key?

Returns:

  • (Boolean)


330
331
332
# File 'lib/action_dispatch/middleware/cookies.rb', line 330

def key?(name)
  @cookies.key?(name.to_s)
end

#to_headerObject



350
351
352
# File 'lib/action_dispatch/middleware/cookies.rb', line 350

def to_header
  @cookies.map { |k, v| "#{escape(k)}=#{escape(v)}" }.join "; "
end

#update(other_hash) ⇒ Object



338
339
340
341
# File 'lib/action_dispatch/middleware/cookies.rb', line 338

def update(other_hash)
  @cookies.update other_hash.stringify_keys
  self
end

#update_cookies_from_jarObject



343
344
345
346
347
348
# File 'lib/action_dispatch/middleware/cookies.rb', line 343

def update_cookies_from_jar
  request_jar = @request.cookie_jar.instance_variable_get(:@cookies)
  set_cookies = request_jar.reject { |k, _| @delete_cookies.key?(k) || @set_cookies.key?(k) }

  @cookies.update set_cookies if set_cookies
end

#write(headers) ⇒ Object



404
405
406
407
408
# File 'lib/action_dispatch/middleware/cookies.rb', line 404

def write(headers)
  if header = make_set_cookie_header(headers[HTTP_HEADER])
    headers[HTTP_HEADER] = header
  end
end