Class: ActionDispatch::Cookies::CookieJar

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

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: $& => example.co.uk

example.com gives: $& => example.com

lots.of.subdomains.example.local gives: $& => example.local

/[^.]*\.([^.]*|..\...|...\...)$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#as_json, #exclude?, #index_by, #many?, #pluck, #sum, #without

Methods included from ChainedCookieJars

#encrypted, #permanent, #signed, #signed_or_encrypted

Constructor Details

#initialize(request) ⇒ CookieJar

Returns a new instance of CookieJar



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

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

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request



291
292
293
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 291

def request
  @request
end

Class Method Details

.build(req, cookies) ⇒ Object



285
286
287
288
289
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 285

def self.build(req, cookies)
  new(req).tap do |hash|
    hash.update(cookies)
  end
end

Instance Method Details

#[](name) ⇒ Object

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



314
315
316
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 314

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.



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 363

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 or 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



407
408
409
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 407

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

#commit!Object



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

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

#committed?Boolean

Returns:

  • (Boolean)


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

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.



386
387
388
389
390
391
392
393
394
395
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 386

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)


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

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

#each(&block) ⇒ Object



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

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

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



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

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

#handle_options(options) ⇒ Object

:nodoc:



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 343

def handle_options(options) #:nodoc:
  options[:path] ||= "/"

  if options[:domain] == :all || options[:domain] == 'all'
    # if there is a provided tld length then we use it otherwise default domain regexp
    domain_regexp = options[:tld_length] ? /([^.]+\.?){#{options[:tld_length]}}$/ : DOMAIN_REGEXP

    # if host is not ip and matches domain regexp
    # (ip confirms to domain regexp so we explicitly check for ip)
    options[:domain] = if (request.host !~ /^[\d.]+$/) && (request.host =~ domain_regexp)
      ".#{$&}"
    end
  elsif options[:domain].is_a? Array
    # if host matches one of the supplied domains without a dot in front of it
    options[:domain] = options[:domain].find {|domain| request.host.include? domain.sub(/^\./, '') }
  end
end

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

Returns:

  • (Boolean)


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

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

#to_headerObject



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

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

#update(other_hash) ⇒ Object



327
328
329
330
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 327

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

#update_cookies_from_jarObject



332
333
334
335
336
337
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 332

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

  @cookies.update set_cookies if set_cookies
end

#write(headers) ⇒ Object



411
412
413
414
415
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 411

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