diff --git a/README.md b/README.md index 52072ff..e49c441 100644 --- a/README.md +++ b/README.md @@ -111,19 +111,24 @@ equivalent using `HTTP::Cookie`: # after jar.save(file) -`HTTP::Cookie` and `CookieJar` raises runtime errors to help -migration, so try running your test code once to find out how to fix -your code base. +`HTTP::Cookie`/`CookieJar` raise runtime errors to help migration, so +after replacing the class names, try running your test code once to +find out how to fix your code base. -The YAML serialization format has changed. Loading YAML files -generated by `Mechanize::CookieJar#save_as` will not raise an -exception, but the content is simply ignored. Note that there is -(obviously) no forward compatibillity in the YAML serialization. +### File formats + +The YAML serialization format has changed, and `HTTP::CookieJar#load` +cannot import what is written in a YAML file saved by +`Mechanize::CookieJar#save_as`. `HTTP::CookieJar#load` will not raise +an exception if an incompatible YAML file is given, but the content is +silently ignored. + +Note that there is (obviously) no forward compatibillity with this. Trying to load a YAML file saved by `HTTP::CookieJar` with -`Mechanize::CookieJar` will fail in `ArgumentError`. +`Mechanize::CookieJar` will fail in runtime error. On the other hand, there has been (and will ever be) no change in the -cookies.txt format. +cookies.txt format, so use it instead if compatibility is required. ## Contributing diff --git a/lib/http/cookie_jar/yaml_saver.rb b/lib/http/cookie_jar/yaml_saver.rb index a9f66b5..2f3fba4 100644 --- a/lib/http/cookie_jar/yaml_saver.rb +++ b/lib/http/cookie_jar/yaml_saver.rb @@ -13,11 +13,18 @@ class HTTP::CookieJar::YAMLSaver < HTTP::CookieJar::AbstractSaver def load(io, jar) begin - YAML.load(io) + data = YAML.load(io) rescue ArgumentError + @logger.warn "unloadable YAML cookie data discarded" if @logger + return + end + + unless data.instance_of?(Array) @logger.warn "incompatible YAML cookie data discarded" if @logger return - end.each { |cookie| + end + + data.each { |cookie| jar.add(cookie) } end