Try not to expose @jar in normal methods.

This commit is contained in:
Akinori MUSHA 2012-10-22 14:52:30 +09:00
parent c7247cf965
commit b7254599bd

View file

@ -54,6 +54,7 @@ class HTTP::CookieJar
} }
} }
} }
self
end end
# call-seq: # call-seq:
@ -110,12 +111,11 @@ class HTTP::CookieJar
# :yaml <- YAML structure. # :yaml <- YAML structure.
# :cookiestxt <- Mozilla's cookies.txt format # :cookiestxt <- Mozilla's cookies.txt format
def load(file, format = :yaml) def load(file, format = :yaml)
@jar = open(file) { |f| open(file) { |f|
case format case format
when :yaml then when :yaml then
load_yaml load_yaml
@jar = YAML.load(f)
YAML.load(f)
when :cookiestxt then when :cookiestxt then
load_cookiestxt(f) load_cookiestxt(f)
else else
@ -124,8 +124,6 @@ class HTTP::CookieJar
} }
cleanup cleanup
self
end end
def load_yaml # :nodoc: def load_yaml # :nodoc:
@ -137,12 +135,14 @@ class HTTP::CookieJar
require 'yaml' require 'yaml'
end end
# Clear the cookie jar # Clear the cookie jar and return self.
def clear def clear
@jar = {} @jar = {}
self
end end
# Read cookies from Mozilla cookies.txt-style IO stream # Read cookies from Mozilla cookies.txt-style IO stream and return
# self.
def load_cookiestxt(io) def load_cookiestxt(io)
now = Time.now now = Time.now
@ -150,19 +150,21 @@ class HTTP::CookieJar
c = HTTP::Cookie.parse_cookiestxt_line(line) and add(c) c = HTTP::Cookie.parse_cookiestxt_line(line) and add(c)
end end
@jar self
end end
# Write cookies to Mozilla cookies.txt-style IO stream # Write cookies to Mozilla cookies.txt-style IO stream and return
# self.
def dump_cookiestxt(io) def dump_cookiestxt(io)
to_a.each do |cookie| to_a.each do |cookie|
io.print cookie.to_cookiestxt_line io.print cookie.to_cookiestxt_line
end end
self
end end
protected protected
# Remove expired cookies # Remove expired cookies and return self.
def cleanup session = false def cleanup session = false
@jar.each do |domain, paths| @jar.each do |domain, paths|
paths.each do |path, names| paths.each do |path, names|
@ -172,6 +174,7 @@ class HTTP::CookieJar
end end
end end
end end
self
end end
end end