Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Define CookieJar#each and include Enumerable instead of defining to_a.

  • Loading branch information...
commit 363b1d35b132020042c6904e5e3d97da981161bc 1 parent 8c18e54
@knu knu authored
Showing with 12 additions and 16 deletions.
  1. +12 −16 lib/mechanize/cookie_jar.rb
View
28 lib/mechanize/cookie_jar.rb
@@ -3,6 +3,7 @@
# any particular website.
class Mechanize::CookieJar
+ include Enumerable
# add_cookie wants something resembling a URI.
@@ -43,15 +44,8 @@ def cookies(url)
cleanup
url.path = '/' if url.path.empty?
- [].tap { |cookies|
- @jar.each { |domain, paths|
- paths.each { |path, hash|
- hash.each_value { |cookie|
- next if cookie.expired? || !cookie.valid_for_uri?(url)
- cookies << cookie
- }
- }
- }
+ select { |cookie|
+ !cookie.expired? && cookie.valid_for_uri?(url)
}
end
@@ -59,14 +53,16 @@ def empty?(url)
cookies(url).length > 0 ? false : true
end
- def to_a
+ def each
+ block_given? or return enum_for(__method__)
cleanup
-
- @jar.map do |domain, paths|
- paths.map do |path, names|
- names.values
- end
- end.flatten
+ @jar.each { |domain, paths|
+ paths.each { |path, hash|
+ hash.each_value { |cookie|
+ yield cookie
+ }
+ }
+ }
end
# Save the cookie jar to a file in the format specified.
Please sign in to comment.
Something went wrong with that request. Please try again.