mirror of
https://github.com/samsonjs/http-cookie.git
synced 2026-04-27 14:57:46 +00:00
Accept a class object where a symbol addressing a class is accepted.
Convert IndexError to ArgumentError, and ArgumentError to TypeError as appropriate.
This commit is contained in:
parent
17d5a128d7
commit
40aba54618
2 changed files with 62 additions and 38 deletions
|
|
@ -29,16 +29,38 @@ class HTTP::CookieJar
|
||||||
|
|
||||||
attr_reader :store
|
attr_reader :store
|
||||||
|
|
||||||
|
def get_impl(base, value, *args)
|
||||||
|
case value
|
||||||
|
when base
|
||||||
|
value
|
||||||
|
when Symbol
|
||||||
|
begin
|
||||||
|
base.implementation(value).new(*args)
|
||||||
|
rescue IndexError => e
|
||||||
|
raise ArgumentError, e.message
|
||||||
|
end
|
||||||
|
when Class
|
||||||
|
if base >= value
|
||||||
|
value.new(*args)
|
||||||
|
else
|
||||||
|
raise TypeError, 'not a subclass of %s: %s' % [base, value]
|
||||||
|
end
|
||||||
|
else
|
||||||
|
raise TypeError, 'invalid object: %s' % value.inspect
|
||||||
|
end
|
||||||
|
end
|
||||||
|
private :get_impl
|
||||||
|
|
||||||
# Generates a new cookie jar.
|
# Generates a new cookie jar.
|
||||||
#
|
#
|
||||||
# Available option keywords are as below:
|
# Available option keywords are as below:
|
||||||
#
|
#
|
||||||
# :store
|
# :store
|
||||||
# : The store class that backs this jar. (default: `:hash`)
|
# : The store class that backs this jar. (default: `:hash`)
|
||||||
# A symbol or an instance of a store class is accepted. Symbols are
|
# A symbol addressing a store class, a store class, or an instance
|
||||||
# mapped to store classes, like `:hash` to
|
# of a store class is accepted. Symbols are mapped to store
|
||||||
# HTTP::CookieJar::HashStore and `:mozilla` to
|
# classes, like `:hash` to HTTP::CookieJar::HashStore and `:mozilla`
|
||||||
# HTTP::CookieJar::MozillaStore.
|
# to HTTP::CookieJar::MozillaStore.
|
||||||
#
|
#
|
||||||
# Any options given are passed through to the initializer of the
|
# Any options given are passed through to the initializer of the
|
||||||
# specified store class. For example, the `:mozilla`
|
# specified store class. For example, the `:mozilla`
|
||||||
|
|
@ -49,14 +71,7 @@ class HTTP::CookieJar
|
||||||
:store => :hash,
|
:store => :hash,
|
||||||
}
|
}
|
||||||
opthash.update(options) if options
|
opthash.update(options) if options
|
||||||
case store = opthash[:store]
|
@store = get_impl(AbstractStore, opthash[:store], opthash)
|
||||||
when Symbol
|
|
||||||
@store = AbstractStore.implementation(store).new(opthash)
|
|
||||||
when AbstractStore
|
|
||||||
@store = store
|
|
||||||
else
|
|
||||||
raise TypeError, 'wrong object given as cookie store: %s' % store.inspect
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# The copy constructor. Not all backend store classes support cloning.
|
# The copy constructor. Not all backend store classes support cloning.
|
||||||
|
|
@ -191,6 +206,10 @@ class HTTP::CookieJar
|
||||||
#
|
#
|
||||||
# * `:format`
|
# * `:format`
|
||||||
#
|
#
|
||||||
|
# Specifies the format for saving. A saver class, a symbol
|
||||||
|
# addressing a saver class, or a pre-generated instance of a
|
||||||
|
# saver class is accepted.
|
||||||
|
#
|
||||||
# <dl class="rdoc-list note-list">
|
# <dl class="rdoc-list note-list">
|
||||||
# <dt>:yaml</dt>
|
# <dt>:yaml</dt>
|
||||||
# <dd>YAML structure (default)</dd>
|
# <dd>YAML structure (default)</dd>
|
||||||
|
|
@ -221,20 +240,20 @@ class HTTP::CookieJar
|
||||||
when Symbol
|
when Symbol
|
||||||
opthash[:format] = options
|
opthash[:format] = options
|
||||||
else
|
else
|
||||||
opthash.update(options) if options
|
if hash = Hash.try_convert(options)
|
||||||
|
opthash.update(hash)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
when 2
|
when 2
|
||||||
opthash[:format], options = options
|
opthash[:format], options = options
|
||||||
opthash.update(options) if options
|
if hash = Hash.try_convert(options)
|
||||||
|
opthash.update(hash)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
raise ArgumentError, 'wrong number of arguments (%d for 1-3)' % (1 + options.size)
|
raise ArgumentError, 'wrong number of arguments (%d for 1-3)' % (1 + options.size)
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
saver = get_impl(AbstractSaver, opthash[:format], opthash)
|
||||||
saver = AbstractSaver.implementation(opthash[:format]).new(opthash)
|
|
||||||
rescue IndexError => e
|
|
||||||
raise ArgumentError, e.message
|
|
||||||
end
|
|
||||||
|
|
||||||
if writable.respond_to?(:write)
|
if writable.respond_to?(:write)
|
||||||
saver.save(writable, self)
|
saver.save(writable, self)
|
||||||
|
|
@ -259,6 +278,10 @@ class HTTP::CookieJar
|
||||||
#
|
#
|
||||||
# * `:format`
|
# * `:format`
|
||||||
#
|
#
|
||||||
|
# Specifies the format for loading. A saver class, a symbol
|
||||||
|
# addressing a saver class, or a pre-generated instance of a
|
||||||
|
# saver class is accepted.
|
||||||
|
#
|
||||||
# <dl class="rdoc-list note-list">
|
# <dl class="rdoc-list note-list">
|
||||||
# <dt>:yaml</dt>
|
# <dt>:yaml</dt>
|
||||||
# <dd>YAML structure (default)</dd>
|
# <dd>YAML structure (default)</dd>
|
||||||
|
|
@ -280,20 +303,20 @@ class HTTP::CookieJar
|
||||||
when Symbol
|
when Symbol
|
||||||
opthash[:format] = options
|
opthash[:format] = options
|
||||||
else
|
else
|
||||||
opthash.update(options) if options
|
if hash = Hash.try_convert(options)
|
||||||
|
opthash.update(hash)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
when 2
|
when 2
|
||||||
opthash[:format], options = options
|
opthash[:format], options = options
|
||||||
opthash.update(options) if options
|
if hash = Hash.try_convert(options)
|
||||||
|
opthash.update(hash)
|
||||||
|
end
|
||||||
else
|
else
|
||||||
raise ArgumentError, 'wrong number of arguments (%d for 1-3)' % (1 + options.size)
|
raise ArgumentError, 'wrong number of arguments (%d for 1-3)' % (1 + options.size)
|
||||||
end
|
end
|
||||||
|
|
||||||
begin
|
saver = get_impl(AbstractSaver, opthash[:format], opthash)
|
||||||
saver = AbstractSaver.implementation(opthash[:format]).new(opthash)
|
|
||||||
rescue IndexError => e
|
|
||||||
raise ArgumentError, e.message
|
|
||||||
end
|
|
||||||
|
|
||||||
if readable.respond_to?(:write)
|
if readable.respond_to?(:write)
|
||||||
saver.load(readable, self)
|
saver.load(readable, self)
|
||||||
|
|
|
||||||
|
|
@ -355,14 +355,6 @@ module TestHTTPCookieJar
|
||||||
assert_equal(0, @jar.cookies(url).length)
|
assert_equal(0, @jar.cookies(url).length)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_save_nonexistent_saver
|
|
||||||
Dir.mktmpdir { |dir|
|
|
||||||
assert_raises(ArgumentError) {
|
|
||||||
@jar.save(File.join(dir, "file"), :nonexistent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_save_cookies_yaml
|
def test_save_cookies_yaml
|
||||||
url = URI 'http://rubyforge.org/'
|
url = URI 'http://rubyforge.org/'
|
||||||
|
|
||||||
|
|
@ -403,12 +395,18 @@ module TestHTTPCookieJar
|
||||||
@jar.save(filename, :format => :cookiestxt)
|
@jar.save(filename, :format => :cookiestxt)
|
||||||
@jar.save(filename, :cookiestxt, :session => true)
|
@jar.save(filename, :cookiestxt, :session => true)
|
||||||
@jar.save(filename, :cookiestxt)
|
@jar.save(filename, :cookiestxt)
|
||||||
|
@jar.save(filename, HTTP::CookieJar::CookiestxtSaver)
|
||||||
|
@jar.save(filename, HTTP::CookieJar::CookiestxtSaver.new)
|
||||||
@jar.save(filename, :session => true)
|
@jar.save(filename, :session => true)
|
||||||
@jar.save(filename)
|
@jar.save(filename)
|
||||||
|
|
||||||
assert_raises(ArgumentError) {
|
assert_raises(ArgumentError) {
|
||||||
@jar.save()
|
@jar.save()
|
||||||
}
|
}
|
||||||
assert_raises(ArgumentError) {
|
assert_raises(ArgumentError) {
|
||||||
|
@jar.save(filename, :nonexistent)
|
||||||
|
}
|
||||||
|
assert_raises(TypeError) {
|
||||||
@jar.save(filename, { :format => :cookiestxt }, { :session => true })
|
@jar.save(filename, { :format => :cookiestxt }, { :session => true })
|
||||||
}
|
}
|
||||||
assert_raises(ArgumentError) {
|
assert_raises(ArgumentError) {
|
||||||
|
|
@ -418,6 +416,8 @@ module TestHTTPCookieJar
|
||||||
@jar.load(filename, :format => :cookiestxt, :linefeed => "\n")
|
@jar.load(filename, :format => :cookiestxt, :linefeed => "\n")
|
||||||
@jar.load(filename, :format => :cookiestxt, :linefeed => "\n")
|
@jar.load(filename, :format => :cookiestxt, :linefeed => "\n")
|
||||||
@jar.load(filename, :format => :cookiestxt)
|
@jar.load(filename, :format => :cookiestxt)
|
||||||
|
@jar.load(filename, HTTP::CookieJar::CookiestxtSaver)
|
||||||
|
@jar.load(filename, HTTP::CookieJar::CookiestxtSaver.new)
|
||||||
@jar.load(filename, :cookiestxt, :linefeed => "\n")
|
@jar.load(filename, :cookiestxt, :linefeed => "\n")
|
||||||
@jar.load(filename, :cookiestxt)
|
@jar.load(filename, :cookiestxt)
|
||||||
@jar.load(filename, :linefeed => "\n")
|
@jar.load(filename, :linefeed => "\n")
|
||||||
|
|
@ -426,6 +426,9 @@ module TestHTTPCookieJar
|
||||||
@jar.load()
|
@jar.load()
|
||||||
}
|
}
|
||||||
assert_raises(ArgumentError) {
|
assert_raises(ArgumentError) {
|
||||||
|
@jar.load(filename, :nonexistent)
|
||||||
|
}
|
||||||
|
assert_raises(TypeError) {
|
||||||
@jar.load(filename, { :format => :cookiestxt }, { :linefeed => "\n" })
|
@jar.load(filename, { :format => :cookiestxt }, { :linefeed => "\n" })
|
||||||
}
|
}
|
||||||
assert_raises(ArgumentError) {
|
assert_raises(ArgumentError) {
|
||||||
|
|
@ -850,16 +853,14 @@ module TestHTTPCookieJar
|
||||||
jar = HTTP::CookieJar.new(:store => :hash)
|
jar = HTTP::CookieJar.new(:store => :hash)
|
||||||
assert_instance_of HTTP::CookieJar::HashStore, jar.store
|
assert_instance_of HTTP::CookieJar::HashStore, jar.store
|
||||||
|
|
||||||
assert_raises(IndexError) {
|
assert_raises(ArgumentError) {
|
||||||
jar = HTTP::CookieJar.new(:store => :nonexistent)
|
jar = HTTP::CookieJar.new(:store => :nonexistent)
|
||||||
}
|
}
|
||||||
|
|
||||||
jar = HTTP::CookieJar.new(:store => HTTP::CookieJar::HashStore.new)
|
jar = HTTP::CookieJar.new(:store => HTTP::CookieJar::HashStore.new)
|
||||||
assert_instance_of HTTP::CookieJar::HashStore, jar.store
|
assert_instance_of HTTP::CookieJar::HashStore, jar.store
|
||||||
|
|
||||||
assert_raises(TypeError) {
|
jar = HTTP::CookieJar.new(:store => HTTP::CookieJar::HashStore)
|
||||||
jar = HTTP::CookieJar.new(:store => HTTP::CookieJar::HashStore)
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_clone
|
def test_clone
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue