http-cookie/lib/http/cookie_jar/yaml_saver.rb
Akinori MUSHA 1f5eb6bc7f Introduce an abstraction layer for saving (serializing) CookieJar.
CookieJar#save is the new name for the now obsolete #save_as.

CookieJar#save and #load now accept IO-like instead of a filename.

Change the YAML file format, and make #load discard incompatible data.
2013-03-15 04:20:59 +09:00

30 lines
569 B
Ruby

require 'http/cookie_jar'
begin
require 'psych'
rescue LoadError
end
require 'yaml'
# YAMLSaver saves and loads cookies in the YAML format.
class HTTP::CookieJar::YAMLSaver < HTTP::CookieJar::AbstractSaver
def save(io, jar)
YAML.dump(@session ? jar.to_a : jar.reject(&:session?), io)
end
def load(io, jar)
begin
YAML.load(io)
rescue ArgumentError
@logger.warn "incompatible YAML cookie data discarded" if @logger
return
end.each { |cookie|
jar.add(cookie)
}
end
private
def default_options
{}
end
end