Class: CookieJar::Cookie
- Inherits:
-
Object
- Object
- CookieJar::Cookie
- Defined in:
- lib/cookiejar/cookie.rb
Overview
Cookie is an immutable object which defines the data model of a HTTP Cookie. The data values within the cookie may be different from the values described in the literal cookie declaration. Specifically, the ‘domain’ and ‘path’ values may be set to defaults based on the requested resource that resulted in the cookie being set.
Instance Attribute Summary collapse
-
#comment ⇒ Object
readonly
- String
-
RFC 2965 field for indicating comment (or a location) describing the cookie to a usesr agent.
-
#comment_url ⇒ Object
readonly
- String
-
RFC 2965 field for indicating comment (or a location) describing the cookie to a usesr agent.
-
#created_at ⇒ Object
readonly
- Time
-
Time when this cookie was first evaluated and created.
-
#discard ⇒ Object
readonly
- Boolean
-
RFC 2965 field for indicating session lifetime for a cookie.
-
#domain ⇒ Object
readonly
- String
-
The domain scope of the cookie.
-
#http_only ⇒ Object
readonly
- Boolean
-
Popular browser extension to mark a cookie as invisible to code running within the browser, such as JavaScript.
-
#name ⇒ Object
readonly
- String
-
The name of the cookie.
-
#path ⇒ Object
readonly
- String
-
The path scope of the cookie.
-
#ports ⇒ Object
readonly
- Array<FixNum>, nil
-
RFC 2965 port scope for the cookie.
-
#secure ⇒ Object
readonly
- Boolean
-
The secure flag is set to indicate that the cookie should only be sent securely.
-
#value ⇒ Object
readonly
- String
-
The value of the cookie, without any attempts at decoding.
-
#version ⇒ Object
readonly
- Fixnum
-
Version indicator, currently either * 0 for netscape cookies * 1 for RFC 2965 cookies.
Class Method Summary collapse
-
.compute_search_domains(request_uri) ⇒ Array<String>
Compute the cookie search domains for a given request URI This will be the effective host of the request uri, along with any possibly matching dot-prefixed domains.
-
.from_set_cookie(request_uri, set_cookie_value) ⇒ Cookie
Create a cookie based on an absolute URI and the string value of a ‘Set-Cookie’ header.
-
.from_set_cookie2(request_uri, set_cookie_value) ⇒ Cookie
Create a cookie based on an absolute URI and the string value of a ‘Set-Cookie2’ header.
-
.json_create(o) ⇒ Cookie
Given a Hash representation of a JSON document, create a local cookie from the included data.
Instance Method Summary collapse
- #decoded_value ⇒ Object
-
#expired?(time = Time.now) ⇒ Boolean
Indicates whether the cookie is currently considered valid.
-
#expires_at ⇒ Time?
Evaluate when this cookie will expire.
-
#session? ⇒ Boolean
Indicates whether the cookie will be considered invalid after the end of the current user session.
-
#should_send?(request_uri, script) ⇒ Boolean
Determine if a cookie should be sent given a request URI along with other options.
-
#to_hash ⇒ Object
Return a hash representation of the cookie.
-
#to_json(*a) ⇒ String
Return a JSON ‘object’ for the various data values.
-
#to_s(ver = 0, prefix = true) ⇒ Object
Returns cookie in a format appropriate to send to a server.
Instance Attribute Details
#comment ⇒ Object (readonly)
- String
-
RFC 2965 field for indicating comment (or a location)
describing the cookie to a usesr agent.
43 44 45 |
# File 'lib/cookiejar/cookie.rb', line 43 def comment @comment end |
#comment_url ⇒ Object (readonly)
- String
-
RFC 2965 field for indicating comment (or a location)
describing the cookie to a usesr agent.
43 44 45 |
# File 'lib/cookiejar/cookie.rb', line 43 def comment_url @comment_url end |
#created_at ⇒ Object (readonly)
- Time
-
Time when this cookie was first evaluated and created.
51 52 53 |
# File 'lib/cookiejar/cookie.rb', line 51 def created_at @created_at end |
#discard ⇒ Object (readonly)
- Boolean
-
RFC 2965 field for indicating session lifetime for a cookie
45 46 47 |
# File 'lib/cookiejar/cookie.rb', line 45 def discard @discard end |
#domain ⇒ Object (readonly)
- String
-
The domain scope of the cookie. Follows the RFC 2965
‘effective host’ rules. A ‘dot’ prefix indicates that it applies both to the non-dotted domain and child domains, while no prefix indicates that only exact matches of the domain are in scope.
21 22 23 |
# File 'lib/cookiejar/cookie.rb', line 21 def domain @domain end |
#http_only ⇒ Object (readonly)
- Boolean
-
Popular browser extension to mark a cookie as invisible
to code running within the browser, such as JavaScript
35 36 37 |
# File 'lib/cookiejar/cookie.rb', line 35 def http_only @http_only end |
#name ⇒ Object (readonly)
- String
-
The name of the cookie.
13 14 15 |
# File 'lib/cookiejar/cookie.rb', line 13 def name @name end |
#path ⇒ Object (readonly)
- String
-
The path scope of the cookie. The cookie applies to URI paths
that prefix match this value.
25 26 27 |
# File 'lib/cookiejar/cookie.rb', line 25 def path @path end |
#ports ⇒ Object (readonly)
- Array<FixNum>, nil
-
RFC 2965 port scope for the cookie. If not nil,
indicates specific ports on the HTTP server which should receive this cookie if contacted.
49 50 51 |
# File 'lib/cookiejar/cookie.rb', line 49 def ports @ports end |
#secure ⇒ Object (readonly)
- Boolean
-
The secure flag is set to indicate that the cookie should
only be sent securely. Nearly all HTTP User Agent implementations assume this to mean that the cookie should only be sent over a SSL/TLS-protected connection
31 32 33 |
# File 'lib/cookiejar/cookie.rb', line 31 def secure @secure end |
#value ⇒ Object (readonly)
- String
-
The value of the cookie, without any attempts at decoding.
15 16 17 |
# File 'lib/cookiejar/cookie.rb', line 15 def value @value end |
#version ⇒ Object (readonly)
- Fixnum
-
Version indicator, currently either
-
0 for netscape cookies
-
1 for RFC 2965 cookies
40 41 42 |
# File 'lib/cookiejar/cookie.rb', line 40 def version @version end |
Class Method Details
.compute_search_domains(request_uri) ⇒ Array<String>
Compute the cookie search domains for a given request URI This will be the effective host of the request uri, along with any possibly matching dot-prefixed domains
234 235 236 |
# File 'lib/cookiejar/cookie.rb', line 234 def self.compute_search_domains(request_uri) CookieValidation.compute_search_domains request_uri end |
.from_set_cookie(request_uri, set_cookie_value) ⇒ Cookie
Create a cookie based on an absolute URI and the string value of a ‘Set-Cookie’ header.
This is used to fill in domain and port if missing from the cookie, and to perform appropriate validation.
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/cookiejar/cookie.rb', line 89 def self.(request_uri, ) args = CookieJar::CookieValidation. args[:domain] = CookieJar::CookieValidation . request_uri, args[:domain] args[:path] = CookieJar::CookieValidation . request_uri, args[:path] = Cookie.new args CookieJar::CookieValidation. request_uri, end |
.from_set_cookie2(request_uri, set_cookie_value) ⇒ Cookie
Create a cookie based on an absolute URI and the string value of a ‘Set-Cookie2’ header.
This is used to fill in domain and port if missing from the cookie, and to perform appropriate validation.
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/cookiejar/cookie.rb', line 109 def self.(request_uri, ) args = CookieJar::CookieValidation. args[:domain] = CookieJar::CookieValidation . request_uri, args[:domain] args[:path] = CookieJar::CookieValidation . request_uri, args[:path] = Cookie.new args CookieJar::CookieValidation. request_uri, end |
.json_create(o) ⇒ Cookie
Given a Hash representation of a JSON document, create a local cookie from the included data.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/cookiejar/cookie.rb', line 211 def self.json_create(o) params = o.inject({}) do |hash, (key, value)| hash[key.to_sym] = value hash end params[:version] ||= 0 params[:created_at] = Time.parse params[:created_at] if params[:expiry].is_a? String params[:expires_at] = Time.parse params[:expiry] else params[:max_age] = params[:expiry] end params.delete :expiry new params end |
Instance Method Details
#decoded_value ⇒ Object
192 193 194 |
# File 'lib/cookiejar/cookie.rb', line 192 def decoded_value CookieJar::CookieValidation.decode_value value end |
#expired?(time = Time.now) ⇒ Boolean
Indicates whether the cookie is currently considered valid
69 70 71 |
# File 'lib/cookiejar/cookie.rb', line 69 def expired?(time = Time.now) !expires_at.nil? && time > expires_at end |
#expires_at ⇒ Time?
Evaluate when this cookie will expire. Uses the original cookie fields for a max age or expires
57 58 59 60 61 62 63 |
# File 'lib/cookiejar/cookie.rb', line 57 def expires_at if @expiry.nil? || @expiry.is_a?(Time) @expiry else @created_at + @expiry end end |
#session? ⇒ Boolean
Indicates whether the cookie will be considered invalid after the end of the current user session
76 77 78 |
# File 'lib/cookiejar/cookie.rb', line 76 def session? @expiry.nil? || @discard end |
#should_send?(request_uri, script) ⇒ Boolean
Determine if a cookie should be sent given a request URI along with other options.
This currently ignores domain.
this cookie extension should be ignored
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/cookiejar/cookie.rb', line 174 def should_send?(request_uri, script) uri = CookieJar::CookieValidation.to_uri request_uri # cookie path must start with the uri, it must not be a secure cookie # being sent over http, and it must not be a http_only cookie sent to # a script path = if uri.path == '' '/' else uri.path end path_match = path.start_with? @path secure_match = !(@secure && uri.scheme == 'http') script_match = !(script && @http_only) expiry_match = !expired? ports_match = ports.nil? || (ports.include? uri.port) path_match && secure_match && script_match && expiry_match && ports_match end |
#to_hash ⇒ Object
Return a hash representation of the cookie.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/cookiejar/cookie.rb', line 140 def to_hash result = { name: @name, value: @value, domain: @domain, path: @path, created_at: @created_at } { expiry: @expiry, secure: (true if @secure), http_only: (true if @http_only), version: (@version if version != 0), comment: @comment, comment_url: @comment_url, discard: (true if @discard), ports: @ports }.each do |name, value| result[name] = value if value end result end |
#to_json(*a) ⇒ String
Return a JSON ‘object’ for the various data values. Allows for persistence of the cookie information
202 203 204 |
# File 'lib/cookiejar/cookie.rb', line 202 def to_json(*a) to_hash.merge(json_class: self.class.name).to_json(*a) end |
#to_s(ver = 0, prefix = true) ⇒ Object
Returns cookie in a format appropriate to send to a server.
“$Version=<version>;”. Ignored for Netscape-style cookies
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/cookiejar/cookie.rb', line 126 def to_s(ver = 0, prefix = true) return "#{name}=#{value}" if ver == 0 # we do not need to encode path; the only characters required to be # quoted must be escaped in URI str = prefix ? "$Version=#{version};" : '' str << "#{name}=#{value};$Path=\"#{path}\"" str << ";$Domain=#{domain}" if domain.start_with? '.' str << ";$Port=\"#{ports.join ','}\"" if ports str end |