Class: ActionDispatch::Cookies::CookieJar
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
| 
         Subclasses: 
        
       | 
    |
| Super Chains via Extension / Inclusion / Inheritance | |
| 
         Instance Chain: 
        
       | 
    |
| Inherits: | Object | 
| Defined in: | actionpack/lib/action_dispatch/middleware/cookies.rb | 
Class Attribute Summary
- .always_write_cookie (also: #always_write_cookie) rw
 
Class Method Summary
- .build(req, cookies)
 - .new(request) ⇒ CookieJar constructor
 
Instance Attribute Summary
- #always_write_cookie rw
 - #committed? ⇒ Boolean readonly
 - #request readonly
 
::Enumerable - Included
| #many? | Returns   | 
    
ChainedCookieJars - Included
Instance Method Summary
- 
    
      #[](name)  
    
    
Returns the value of the cookie by
name, ornilif no such cookie exists. - 
    
      #[]=(name, options)  
    
    
Sets the cookie named
name. - 
    
      #clear(options = {})  
    
    
Removes all cookies on the client machine by calling #delete for each cookie.
 - #commit!
 - 
    
      #delete(name, options = {})  
    
    
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)
 - #fetch(name, *args, &block)
 - 
    
      #has_key?(name)  
    
    
Alias for #key?.
 - #key?(name) ⇒ Boolean (also: #has_key?)
 - 
    
      #to_hash  
    
    
Returns the cookies as
::Hash. - #to_header
 - #update(other_hash)
 - #update_cookies_from_jar
 - #write(response)
 - #escape(string) private
 - #handle_options(options) private
 - #write_cookie?(cookie) ⇒ Boolean private
 
::Enumerable - Included
| #compact_blank | Returns a new   | 
    
| #exclude? | The negative of the   | 
    
| #excluding | Returns a copy of the enumerable excluding the specified elements.  | 
    
| #in_order_of | Returns a new   | 
    
| #including | Returns a new array that includes the passed elements.  | 
    
| #index_by | Convert an enumerable to a hash, using the block result as the key and the element as the value.  | 
    
| #index_with | Convert an enumerable to a hash, using the element as the key and the block result as the value.  | 
    
| #maximum | Calculates the maximum from the extracted elements.  | 
    
| #minimum | Calculates the minimum from the extracted elements.  | 
    
| #pick | Extract the given key from the first element in the enumerable.  | 
    
| #pluck | Extract the given key from each element in the enumerable.  | 
    
| #sole | Returns the sole item in the enumerable.  | 
    
| #without | Alias for Enumerable#excluding.  | 
    
| #as_json | |
::ActiveSupport::EnumerableCoreExt::Constants - Included
ChainedCookieJars - Included
| #encrypted | Returns a jar that’ll automatically encrypt cookie values before sending them to the client and will decrypt them for read.  | 
    
| #permanent | Returns a jar that’ll automatically set the assigned cookies to have an expiration date 20 years from now.  | 
    
| #signed | Returns a jar that’ll automatically generate a signed representation of cookie value and verify it when reading from the cookie again.  | 
    
| #signed_or_encrypted | Returns the   | 
    
| #encrypted_cookie_cipher, #signed_cookie_digest | |
Constructor Details
    .new(request)  ⇒ CookieJar 
  
Class Attribute Details
.always_write_cookie (rw) Also known as: #always_write_cookie
[ GitHub ]# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 441
mattr_accessor :, default: false
Class Method Details
.build(req, cookies)
[ GitHub ]Instance Attribute Details
#always_write_cookie (rw)
[ GitHub ]# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 441
mattr_accessor :, default: false
    #committed?  ⇒ Boolean  (readonly)
  
  [ GitHub ]
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 332
def committed?; @committed; end
#request (readonly)
[ GitHub ]# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 322
attr_reader :request
Instance Method Details
#[](name)
Returns the value of the cookie by name, or nil if no such cookie exists.
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 345
def [](name) @cookies[name.to_s] end
#[]=(name, options)
Sets the cookie named name. The second argument may be the cookie’s value or a hash of options as documented above.
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 379
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 = {})
Removes all cookies on the client machine by calling #delete for each cookie.
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 425
def clear( = {}) @cookies.each_key { |k| delete(k, ) } end
#commit!
[ GitHub ]# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 334
def commit! @committed = true @set_cookies.freeze @delete_cookies.freeze end
#delete(name, options = {})
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.
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 404
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.
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 418
def deleted?(name, = {}) .symbolize_keys! () @delete_cookies[name.to_s] == end
#each(&block)
[ GitHub ]# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 340
def each(&block) @cookies.each(&block) end
#escape(string) (private)
[ GitHub ]# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 444
def escape(string) ::Rack::Utils.escape(string) end
#fetch(name, *args, &block)
[ GitHub ]# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 349
def fetch(name, *args, &block) @cookies.fetch(name.to_s, *args, &block) end
#handle_options(options) (private)
[ GitHub ]# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 452
def () if [:expires].respond_to?(:from_now) [:expires] = [:expires].from_now end [:path] ||= "/" unless .key?(:same_site) [:same_site] = request. end if [:domain] == :all || [:domain] == "all" = "" dot_splitted_host = request.host.split(".", -1) # Case where request.host is not an IP address or it's an invalid domain (ip # confirms to the domain structure we expect so we explicitly check for ip) if request.host.match?(/^[\d.]+$/) || dot_splitted_host.include?("") || dot_splitted_host.length == 1 [:domain] = nil return end # If there is a provided tld length then we use it otherwise default domain. if [:tld_length].present? # Case where the tld_length provided is valid if dot_splitted_host.length >= [:tld_length] = dot_splitted_host.last([:tld_length]).join(".") end # Case where tld_length is not provided else # Regular TLDs if !(/\.[^.]{2,3}\.[^.]{2}\z/.match?(request.host)) = dot_splitted_host.last(2).join(".") # **.**, ***.** style TLDs like co.uk and com.au else = dot_splitted_host.last(3).join(".") end end [:domain] = if .present? end elsif [:domain].is_a? Array # If host matches one of the supplied domains. [:domain] = [:domain].find do |domain| domain = domain.delete_prefix(".") request.host == domain || request.host.end_with?(".#{domain}") end elsif [:domain].respond_to?(:call) [:domain] = [:domain].call(request) end end
#has_key?(name)
Alias for #key?.
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 356
alias :has_key? :key?
    #key?(name)  ⇒ Boolean 
    Also known as: #has_key?
  
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 353
def key?(name) @cookies.key?(name.to_s) end
#to_hash
Returns the cookies as ::Hash.
# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 359
alias :to_hash :to_h
#to_header
[ GitHub ]#update(other_hash)
[ GitHub ]# File 'actionpack/lib/action_dispatch/middleware/cookies.rb', line 361
def update(other_hash) @cookies.update other_hash.stringify_keys self end