def load(io, jar)
begin
data = YAML.load(io)
rescue ArgumentError => e
case e.message
when %r{\Aundefined class/module Mechanize::}
begin
io.rewind
yaml = io.read
yaml.gsub!(%r{^( [^ ].*:) !ruby/object:Mechanize::Cookie$}, "\\1")
data = YAML.load(yaml)
rescue Errno::ESPIPE
@logger.warn "could not rewind the stream for conversion" if @logger
rescue ArgumentError
end
end
end
case data
when Array
data.each { |cookie|
jar.add(cookie)
}
when Hash
data.each { |domain, paths|
paths.each { |path, names|
names.each { |cookie_name, cookie_hash|
if cookie_hash.respond_to?(:ivars)
cookie_hash = cookie_hash.ivars
end
cookie = HTTP::Cookie.new({}.tap { |hash|
cookie_hash.each_pair { |key, value|
hash[key.to_sym] = value
}
})
jar.add(cookie)
}
}
}
else
@logger.warn "incompatible YAML cookie data discarded" if @logger
return
end
end