From 7fa5b10df6bd56ec0dc4dc347455ddebc384d8fc Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Mon, 22 Oct 2012 15:08:08 +0900 Subject: [PATCH] HTTP::CookieJar#each: Take an optional URL/URI which is used as a scope. Do not call cleanup in each or cookies. --- lib/http/cookie_jar.rb | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/http/cookie_jar.rb b/lib/http/cookie_jar.rb index abee5ac..595bb5a 100644 --- a/lib/http/cookie_jar.rb +++ b/lib/http/cookie_jar.rb @@ -32,11 +32,11 @@ class HTTP::CookieJar end alias << add - # Fetch the cookies that should be used for the URI object passed in. + # Fetch the cookies that should be used for the URL/URI. def cookies(url) now = Time.now - select { |cookie| - !cookie.expired? && cookie.valid_for_uri?(url) && (cookie.accessed_at = now) + each(url).select { |cookie| + !cookie.expired? && (cookie.accessed_at = now) }.sort end @@ -44,14 +44,20 @@ class HTTP::CookieJar cookies(url).empty? end - def each - block_given? or return enum_for(__method__) - cleanup + # Iterate over cookies. If +uri+ is given, cookies not for the + # URL/URI are excluded. + def each(uri = nil, &block) + block_given? or return enum_for(__method__, uri) + + if uri + block = proc { |cookie| + yield cookie if cookie.valid_for_uri?(uri) + } + end + @jar.each { |domain, paths| paths.each { |path, hash| - hash.each_value { |cookie| - yield cookie - } + hash.each_value(&block) } } self