lib/eat.rb in eat-0.1.7 vs lib/eat.rb in eat-0.1.8

- old
+ new

@@ -1,19 +1,28 @@ require 'uri' require 'httpclient' require 'eat/version' -# http://weblog.jamisbuck.org/2007/2/7/infinity -unless defined?(::Infinity) - ::Infinity = 1.0/0 -end - module Eat # httpclient 2.2.3 inserts the platform info for you, albeit with problems # AGENT_NAME = "Mozilla/5.0 (#{::RUBY_PLATFORM}) Ruby/#{::RUBY_VERSION} HTTPClient/#{::HTTPClient::VERSION} eat/#{::Eat::VERSION}" AGENT_NAME = "eat/#{::Eat::VERSION}" + + # http://weblog.jamisbuck.org/2007/2/7/infinity + INFINITY = 1.0/0 + + # https://github.com/nahi/httpclient/blob/master/lib/httpclient.rb#L640 + REDIRECT_HANDLER = ::Proc.new do |uri, res| + newuri = ::URI.parse(res.header['location'][0]) + unless newuri.is_a?(::URI::HTTP) + newuri = uri + newuri + end + newuri + end + + TIMEOUT = 2 module ObjectExtensions # <tt>url</tt> can be filesystem or http/https # # Options: @@ -24,11 +33,11 @@ # Example: # eat('http://brighterplanet.com') #=> '...' # eat('http://brighterplanet.com', :timeout => 10) #=> '...' # eat('http://brighterplanet.com', :limit => 1) #=> '.' def eat(url, options = {}) - limit = options.fetch(:limit, ::Infinity) + limit = options.fetch(:limit, INFINITY) uri = ::URI.parse url.to_s body = [] read_so_far = 0 @@ -44,24 +53,24 @@ body << chunk end end when 'http', 'https' - timeout = options.fetch(:timeout, 2) + timeout = options.fetch(:timeout, TIMEOUT) openssl_verify_mode = options.fetch(:openssl_verify_mode, ::OpenSSL::SSL::VERIFY_PEER) if openssl_verify_mode == 'none' openssl_verify_mode = ::OpenSSL::SSL::VERIFY_NONE end http = ::HTTPClient.new http.agent_name = AGENT_NAME - http.redirect_uri_callback = ::Proc.new { |uri, res| ::URI.parse(res.header['location'][0]) } + http.redirect_uri_callback = REDIRECT_HANDLER http.transparent_gzip_decompression = true http.receive_timeout = timeout if uri.scheme == 'https' http.ssl_config.verify_mode = openssl_verify_mode end - if limit == ::Infinity + if limit == INFINITY body << http.get_content(uri.to_s) else catch :stop do http.get_content(uri.to_s) do |chunk| body << chunk @@ -70,10 +79,10 @@ end end end end - limit == ::Infinity ? body.join : body.join[0...limit] + limit == INFINITY ? body.join : body.join[0...limit] end end end ::Object.send(:include, ::Eat::ObjectExtensions) unless ::Object.method_defined?(:eat)