mirror of
https://github.com/samsonjs/http-cookie.git
synced 2026-04-27 14:57:46 +00:00
Directly define String#try_convert and Hash#try_convert in ruby 1.8.
This commit is contained in:
parent
39861d2dbb
commit
88e3f28591
2 changed files with 33 additions and 41 deletions
|
|
@ -31,40 +31,6 @@ class HTTP::Cookie
|
||||||
expires max_age
|
expires max_age
|
||||||
created_at accessed_at
|
created_at accessed_at
|
||||||
]
|
]
|
||||||
|
|
||||||
if String.respond_to?(:try_convert)
|
|
||||||
def check_string_type(object)
|
|
||||||
String.try_convert(object)
|
|
||||||
end
|
|
||||||
private :check_string_type
|
|
||||||
else
|
|
||||||
def check_string_type(object)
|
|
||||||
if object.is_a?(String) ||
|
|
||||||
(object.respond_to?(:to_str) && (object = object.to_str).is_a?(String))
|
|
||||||
object
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
private :check_string_type
|
|
||||||
end
|
|
||||||
|
|
||||||
if Hash.respond_to?(:try_convert)
|
|
||||||
def check_hash_type(object)
|
|
||||||
Hash.try_convert(object)
|
|
||||||
end
|
|
||||||
private :check_hash_type
|
|
||||||
else
|
|
||||||
def check_hash_type(object)
|
|
||||||
if object.is_a?(Hash) ||
|
|
||||||
(object.respond_to?(:to_h) && (object = object.to_str).is_a?(Hash))
|
|
||||||
object
|
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
private :check_hash_type
|
|
||||||
end
|
|
||||||
# :startdoc:
|
# :startdoc:
|
||||||
|
|
||||||
# The cookie name. It may not be nil or empty.
|
# The cookie name. It may not be nil or empty.
|
||||||
|
|
@ -170,14 +136,14 @@ class HTTP::Cookie
|
||||||
@created_at = @accessed_at = Time.now
|
@created_at = @accessed_at = Time.now
|
||||||
case argc = args.size
|
case argc = args.size
|
||||||
when 1
|
when 1
|
||||||
if attr_hash = check_hash_type(args.last)
|
if attr_hash = Hash.try_convert(args.last)
|
||||||
args.pop
|
args.pop
|
||||||
else
|
else
|
||||||
self.name, self.value = args # value is set to nil
|
self.name, self.value = args # value is set to nil
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
when 2..3
|
when 2..3
|
||||||
if attr_hash = check_hash_type(args.last)
|
if attr_hash = Hash.try_convert(args.last)
|
||||||
args.pop
|
args.pop
|
||||||
self.name, value = args
|
self.name, value = args
|
||||||
else
|
else
|
||||||
|
|
@ -378,7 +344,7 @@ class HTTP::Cookie
|
||||||
|
|
||||||
# See #name.
|
# See #name.
|
||||||
def name=(name)
|
def name=(name)
|
||||||
name = check_string_type(name) or
|
name = String.try_convert(name) or
|
||||||
raise TypeError, "#{name.class} is not a String"
|
raise TypeError, "#{name.class} is not a String"
|
||||||
if name.empty?
|
if name.empty?
|
||||||
raise ArgumentError, "cookie name cannot be empty"
|
raise ArgumentError, "cookie name cannot be empty"
|
||||||
|
|
@ -399,7 +365,7 @@ class HTTP::Cookie
|
||||||
self.expires = UNIX_EPOCH
|
self.expires = UNIX_EPOCH
|
||||||
return @value = ''
|
return @value = ''
|
||||||
end
|
end
|
||||||
value = check_string_type(value) or
|
value = String.try_convert(value) or
|
||||||
raise TypeError, "#{value.class} is not a String"
|
raise TypeError, "#{value.class} is not a String"
|
||||||
if value.match(/[\x00-\x1F\x7F]/)
|
if value.match(/[\x00-\x1F\x7F]/)
|
||||||
raise ArgumentError, "invalid cookie value"
|
raise ArgumentError, "invalid cookie value"
|
||||||
|
|
@ -427,7 +393,7 @@ class HTTP::Cookie
|
||||||
when DomainName
|
when DomainName
|
||||||
@domain_name = domain
|
@domain_name = domain
|
||||||
else
|
else
|
||||||
domain = check_string_type(domain) or
|
domain = String.try_convert(domain) or
|
||||||
raise TypeError, "#{domain.class} is not a String"
|
raise TypeError, "#{domain.class} is not a String"
|
||||||
if domain.start_with?('.')
|
if domain.start_with?('.')
|
||||||
for_domain = true
|
for_domain = true
|
||||||
|
|
@ -472,7 +438,7 @@ class HTTP::Cookie
|
||||||
|
|
||||||
# See #path.
|
# See #path.
|
||||||
def path=(path)
|
def path=(path)
|
||||||
path = check_string_type(path) or
|
path = String.try_convert(path) or
|
||||||
raise TypeError, "#{path.class} is not a String"
|
raise TypeError, "#{path.class} is not a String"
|
||||||
@path = path.start_with?('/') ? path : '/'
|
@path = path.start_with?('/') ? path : '/'
|
||||||
end
|
end
|
||||||
|
|
@ -538,7 +504,7 @@ class HTTP::Cookie
|
||||||
case sec
|
case sec
|
||||||
when Integer, nil
|
when Integer, nil
|
||||||
else
|
else
|
||||||
str = check_string_type(sec) or
|
str = String.try_convert(sec) or
|
||||||
raise TypeError, "#{sec.class} is not an Integer or String"
|
raise TypeError, "#{sec.class} is not an Integer or String"
|
||||||
/\A-?\d+\z/.match(str) or
|
/\A-?\d+\z/.match(str) or
|
||||||
raise ArgumentError, "invalid Max-Age: #{sec.inspect}"
|
raise ArgumentError, "invalid Max-Age: #{sec.inspect}"
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,32 @@ class Array
|
||||||
end unless method_defined?(:select!)
|
end unless method_defined?(:select!)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Hash
|
||||||
|
class << self
|
||||||
|
def try_convert(object)
|
||||||
|
if object.is_a?(Hash) ||
|
||||||
|
(object.respond_to?(:to_h) && (object = object.to_str).is_a?(Hash))
|
||||||
|
object
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end unless method_defined?(:try_convert)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class String
|
||||||
|
class << self
|
||||||
|
def try_convert(object)
|
||||||
|
if object.is_a?(String) ||
|
||||||
|
(object.respond_to?(:to_str) && (object = object.to_str).is_a?(String))
|
||||||
|
object
|
||||||
|
else
|
||||||
|
nil
|
||||||
|
end
|
||||||
|
end unless method_defined?(:try_convert)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# In Ruby < 1.9.3 URI() does not accept a URI object.
|
# In Ruby < 1.9.3 URI() does not accept a URI object.
|
||||||
if RUBY_VERSION < "1.9.3"
|
if RUBY_VERSION < "1.9.3"
|
||||||
require 'uri'
|
require 'uri'
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue