Class: Rage::Cookies
- Inherits:
-
Object
- Object
- Rage::Cookies
- Defined in:
- lib/rage/cookies.rb
Defined Under Namespace
Classes: EncryptedJar, SimpleJar
Instance Method Summary collapse
-
#[](key) ⇒ String
Read a cookie.
-
#[]=(key, value) ⇒ Object
Set a cookie.
-
#delete(key, path: "/", domain: nil) ⇒ Object
Delete a cookie.
-
#encrypted ⇒ Object
Returns a jar that'll automatically encrypt cookie values before sending them to the client and will decrypt them for read.
- #inspect ⇒ Object
-
#permanent ⇒ Object
Returns a jar that'll automatically set the assigned cookies to have an expiration date 20 years from now.
-
#size ⇒ Integer
Get the number of cookies.
Instance Method Details
#[](key) ⇒ String
Read a cookie.
30 31 32 33 |
# File 'lib/rage/cookies.rb', line 30 def [](key) value = [key] @jar.load(value) if value end |
#[]=(key, value) ⇒ Object
Set a cookie.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rage/cookies.rb', line 90 def []=(key, value) @headers.compare_by_identity unless value.is_a?(Hash) serialized_value = @jar.dump(value) @request_cookies[key] = serialized_value @headers[(key)] = Rack::Utils.(nil, key, { value: serialized_value, expires: @expires }) return end if (domain = value[:domain]) host = @env["HTTP_HOST"] _domain = if domain.is_a?(String) domain elsif domain == :all DomainName(host).domain elsif domain.is_a?(Array) host if domain.include?(host) end end serialized_value = @jar.dump(value[:value]) = Rack::Utils.(nil, key, { path: value[:path], secure: value[:secure], expires: value[:expires] || @expires, httponly: value[:httponly], same_site: value[:same_site], value: serialized_value, domain: _domain }) @request_cookies[key] = serialized_value @headers[(key)] = end |
#delete(key, path: "/", domain: nil) ⇒ Object
Delete a cookie.
47 48 49 50 51 52 53 54 |
# File 'lib/rage/cookies.rb', line 47 def delete(key, path: "/", domain: nil) @headers.compare_by_identity @request_cookies[key] = nil @headers[(key)] = Rack::Utils.(nil, key, { value: "", expires: Time.at(0), path: path, domain: domain }) end |
#encrypted ⇒ Object
Returns a jar that'll automatically encrypt cookie values before sending them to the client and will decrypt them
for read. If the cookie was tampered with by the user (or a 3rd party), nil
will be returned.
This jar requires that you set a suitable secret for the verification on your app's secret_key_base
.
63 64 65 |
# File 'lib/rage/cookies.rb', line 63 def encrypted dup.tap { |c| c.jar = EncryptedJar } end |
#inspect ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rage/cookies.rb', line 127 def inspect = .transform_values do |v| decoded = Base64.urlsafe_decode64(v) rescue nil is_encrypted = decoded&.start_with?(EncryptedJar::PADDING) is_encrypted ? "<encrypted>" : v end "#<#{self.class.name} @request_cookies=#{.inspect}" end |
#permanent ⇒ Object
Returns a jar that'll automatically set the assigned cookies to have an expiration date 20 years from now.
71 72 73 |
# File 'lib/rage/cookies.rb', line 71 def permanent dup.tap { |c| c.expires = Time.now + 20 * 365 * 24 * 60 * 60 } end |
#size ⇒ Integer
Get the number of cookies.
38 39 40 |
# File 'lib/rage/cookies.rb', line 38 def size .count { |_, v| !v.nil? } end |