Class: Dommy::Rack::CookieJar
- Inherits:
-
Object
- Object
- Dommy::Rack::CookieJar
- Defined in:
- lib/dommy/rack/cookie_jar.rb
Overview
A simplified, same-origin cookie store. Parses Set-Cookie response headers, generates the Cookie request header, and applies domain, path, expiry, and secure matching. No public-suffix handling.
Defined Under Namespace
Classes: CookieEntry
Instance Method Summary collapse
- #all ⇒ Object
- #clear ⇒ Object
-
#cookies_for(request_url) ⇒ Object
Build the Cookie request header value for the given URL, or “”.
-
#get(name) ⇒ Object
First non-expired cookie value matching the name.
-
#initialize ⇒ CookieJar
constructor
A new instance of CookieJar.
-
#set!(name, value, domain: nil, path: "/", expires: nil, secure: false, http_only: false) ⇒ Object
Manually store a cookie.
-
#store_from_header(set_cookie_string, request_url) ⇒ Object
Parse a single Set-Cookie header value and store the result.
Constructor Details
#initialize ⇒ CookieJar
Returns a new instance of CookieJar.
17 18 19 |
# File 'lib/dommy/rack/cookie_jar.rb', line 17 def initialize @entries = [] end |
Instance Method Details
#all ⇒ Object
58 59 60 |
# File 'lib/dommy/rack/cookie_jar.rb', line 58 def all @entries.reject { |e| expired?(e) } end |
#clear ⇒ Object
54 55 56 |
# File 'lib/dommy/rack/cookie_jar.rb', line 54 def clear @entries = [] end |
#cookies_for(request_url) ⇒ Object
Build the Cookie request header value for the given URL, or “”.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/dommy/rack/cookie_jar.rb', line 63 def (request_url) uri = URI.parse(request_url) secure_request = uri.scheme == "https" host = uri.host.to_s.downcase path = uri.path.to_s.empty? ? "/" : uri.path matches = @entries.reject { |e| expired?(e) }.select do |e| domain_match?(e, host) && path_match?(e.path, path) && (!e.secure || secure_request) end # More specific (longer) paths first, per RFC 6265. matches.sort_by! { |e| -e.path.length } matches.map { |e| "#{e.name}=#{e.value}" }.join("; ") end |
#get(name) ⇒ Object
First non-expired cookie value matching the name.
50 51 52 |
# File 'lib/dommy/rack/cookie_jar.rb', line 50 def get(name) @entries.find { |e| e.name == name.to_s && !expired?(e) }&.value end |
#set!(name, value, domain: nil, path: "/", expires: nil, secure: false, http_only: false) ⇒ Object
Manually store a cookie. domain defaults to host-only on request_host.
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dommy/rack/cookie_jar.rb', line 35 def set!(name, value, domain: nil, path: "/", expires: nil, secure: false, http_only: false) entry = CookieEntry.new( name: name.to_s, value: value.to_s, domain: (domain || "").sub(/\A\./, "").downcase, path: path || "/", expires: expires, secure: secure, http_only: http_only, host_only: domain.nil? ) store_entry(entry) end |
#store_from_header(set_cookie_string, request_url) ⇒ Object
Parse a single Set-Cookie header value and store the result.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dommy/rack/cookie_jar.rb', line 22 def store_from_header(, request_url) uri = URI.parse(request_url) entry = (, uri) return unless entry if expired?(entry) remove(entry.name, entry.domain, entry.path) else store_entry(entry) end end |