From 6ec66d11eddb2e2ca60a2f7ed076863ada26f2cf Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Sun, 14 Apr 2013 13:01:37 +0900 Subject: [PATCH] Restore and improve compatibility with ruby 1.8. --- lib/http/cookie.rb | 19 +--------------- lib/http/cookie/ruby_compat.rb | 33 ++++++++++++++++++++++++++++ lib/http/cookie_jar/mozilla_store.rb | 6 ++--- 3 files changed, 37 insertions(+), 21 deletions(-) create mode 100644 lib/http/cookie/ruby_compat.rb diff --git a/lib/http/cookie.rb b/lib/http/cookie.rb index 098dd57..906bf62 100644 --- a/lib/http/cookie.rb +++ b/lib/http/cookie.rb @@ -3,29 +3,12 @@ require 'http/cookie/version' require 'time' require 'uri' require 'domain_name' +require 'http/cookie/ruby_compat' module HTTP autoload :CookieJar, 'http/cookie_jar' end -# In Ruby < 1.9.3 URI() does not accept a URI object. -if RUBY_VERSION < "1.9.3" - begin - URI(URI('')) - rescue - def URI(url) # :nodoc: - case url - when URI - url - when String - URI.parse(url) - else - raise ArgumentError, 'bad argument (expected URI object or URI string)' - end - end - end -end - # This class is used to represent an HTTP Cookie. class HTTP::Cookie # Maximum number of bytes per cookie (RFC 6265 6.1 requires 4096 at diff --git a/lib/http/cookie/ruby_compat.rb b/lib/http/cookie/ruby_compat.rb new file mode 100644 index 0000000..6cee6fd --- /dev/null +++ b/lib/http/cookie/ruby_compat.rb @@ -0,0 +1,33 @@ +class Array + def select! + i = 0 + each_with_index { |x, j| + yield x or next + self[i] = x if i != j + i += 1 + } + return nil if i == size + self[i..-1] = [] + self + end unless method_defined?(:select!) +end + +# In Ruby < 1.9.3 URI() does not accept a URI object. +if RUBY_VERSION < "1.9.3" + require 'uri' + + begin + URI(URI('')) + rescue + def URI(url) # :nodoc: + case url + when URI + url + when String + URI.parse(url) + else + raise ArgumentError, 'bad argument (expected URI object or URI string)' + end + end + end +end diff --git a/lib/http/cookie_jar/mozilla_store.rb b/lib/http/cookie_jar/mozilla_store.rb index ba545fe..ffc02ec 100644 --- a/lib/http/cookie_jar/mozilla_store.rb +++ b/lib/http/cookie_jar/mozilla_store.rb @@ -255,7 +255,7 @@ class HTTP::CookieJar db_delete(cookie) end - def each(uri = nil) + def each(uri = nil, &block) now = Time.now if uri @st_cookies_for_domain ||= @@ -304,7 +304,7 @@ class HTTP::CookieJar yield cookie end } - @sjar.each(uri, &proc) + @sjar.each(uri, &block) else @st_all_cookies ||= @db.prepare(<<-'SQL') @@ -333,7 +333,7 @@ class HTTP::CookieJar yield cookie } - @sjar.each(&proc) + @sjar.each(&block) end self end