lib/url_canonicalize/request.rb in url_canonicalize-0.1.9 vs lib/url_canonicalize/request.rb in url_canonicalize-0.1.10

- old
+ new

@@ -4,23 +4,44 @@ class Request def fetch handle_response end + def location + @location ||= relative_to_absolute(response['location']) + end + + def with_uri(uri) + @uri = uri + + @url = nil + @host = nil + @response = nil + @location = nil + @html = nil + + self + end + private attr_reader :http, :http_method def initialize(http, http_method = :head) @http = http @http_method = http_method end def response - @response ||= http.request request # Some URLs can throw an exception here + @response ||= do_http_request end + # We can stub this method in testing then call #response any number of times + def do_http_request #:nodoc: internal use only + http.do_request request # Some URLs can throw an exception here + end + def request @request ||= request_for_method end def handle_response @@ -49,14 +70,14 @@ end end def handle_redirection case response - when Net::HTTPFound, Net::HTTPMovedTemporarily, Net::HTTPTemporaryRedirect + when Net::HTTPFound, Net::HTTPMovedTemporarily, Net::HTTPTemporaryRedirect # Temporary redirection self.http_method = :get handle_success - else + else # Permanent redirection if location URLCanonicalize::Response::Redirect.new(location) else URLCanonicalize::Response::Failure.new(::URI::InvalidURIError, response['location']) end @@ -103,14 +124,10 @@ def host @host ||= uri.host end - def location - @location ||= relative_to_absolute(response['location']) - end - def request_for_method r = base_request headers.each { |header_key, header_value| r[header_key] = header_value } r end @@ -122,11 +139,11 @@ when :head Net::HTTP::Head.new uri when :get Net::HTTP::Get.new uri else - raise URLCanonicalize::Exception::Request, "Unknown method: #{method}" + raise URLCanonicalize::Exception::Request, "Unknown method: #{http_method}" end end def headers @headers ||= { @@ -139,9 +156,10 @@ def http_method=(value) @http_method = value @request = nil @response = nil + @location = nil @html = nil end # Some sites treat HEAD requests as suspicious activity and block the # requester after a few attempts. For these sites we'll use GET requests