From a0ea64da560a37950bbc6f60221f8a5df512caf4 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Wed, 27 Mar 2013 17:43:58 +0900 Subject: [PATCH] CookieJar.new: Pass the store class via a :store keyword. --- lib/http/cookie_jar.rb | 32 ++++++++++++++++++++++---------- test/test_http_cookie_jar.rb | 6 ++++-- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lib/http/cookie_jar.rb b/lib/http/cookie_jar.rb index a4c8698..0e91823 100644 --- a/lib/http/cookie_jar.rb +++ b/lib/http/cookie_jar.rb @@ -10,18 +10,30 @@ class HTTP::CookieJar attr_reader :store - # Generates a new cookie jar. The default store class is `:hash`, - # which maps to `HTTP::CookieJar::HashStore`. Any given options are - # passed through to the initializer of the specified store class. - # For example, the `:mozilla` (`HTTP::CookieJar::MozillaStore`) - # store class requires a `:filename` option. - def initialize(store = :hash, options = nil) - case store + # Generates a new cookie jar. + # + # Available option keywords are as below: + # + # :store + # : The store class that backs this jar. (default: `:hash`) + # A symbol or an instance of a store class is accepted. Symbols are + # mapped to store classes, like `:hash` to + # `HTTP::CookieJar::HashStore` and `:mozilla` to + # `HTTP::CookieJar::MozillaStore`. + # + # Any options given are passed through to the initializer of the + # specified store class. For example, the `:mozilla` + # (`HTTP::CookieJar::MozillaStore`) store class requires a + # `:filename` option. See individual store classes for details. + def initialize(options = nil) + opthash = { + :store => :hash, + } + opthash.update(options) if options + case store = opthash[:store] when Symbol - @store = AbstractStore.implementation(store).new(options) + @store = AbstractStore.implementation(store).new(opthash) when AbstractStore - options.empty? or - raise ArgumentError, 'wrong number of arguments (%d for 1)' % (1 + options.size) @store = store else raise TypeError, 'wrong object given as cookie store: %s' % store.inspect diff --git a/test/test_http_cookie_jar.rb b/test/test_http_cookie_jar.rb index f621b92..827a5b2 100644 --- a/test/test_http_cookie_jar.rb +++ b/test/test_http_cookie_jar.rb @@ -591,7 +591,8 @@ class TestHTTPCookieJar < Test::Unit::TestCase def test_max_cookies_hashstore gc_threshold = 150 h_test_max_cookies( - HTTP::CookieJar.new(:hash, + HTTP::CookieJar.new( + :store => :hash, :gc_threshold => gc_threshold), HTTP::Cookie::MAX_COOKIES_TOTAL + gc_threshold) end @@ -600,7 +601,8 @@ class TestHTTPCookieJar < Test::Unit::TestCase gc_threshold = 150 Dir.mktmpdir { |dir| h_test_max_cookies( - HTTP::CookieJar.new(:mozilla, + HTTP::CookieJar.new( + :store => :mozilla, :gc_threshold => gc_threshold, :filename => File.join(dir, "cookies.sqlite")), HTTP::Cookie::MAX_COOKIES_TOTAL + gc_threshold)