uri_fetcher.rb revision 82100ce1e7d4a5050e23256b055e80d9b0678266
# Currently only File and Tempfile are io.rewind if io.respond_to?(:rewind) def fetch_uri_content(uri, limit: 10, write_file: nil, prev_resp: nil) raise TooManyRedirectionsError.new(last_response: prev_resp) if limit == 0 Net::HTTP.get_response(URI(uri)) do |response| if has_actual_content?(response) produce_response_body(response, write_file) elsif response['location'] && !response['location'].empty? fetch_uri_content(response['location'], raise UnfollowableResponseError.new(last_response: response) def produce_response_body(response, write_file=nil) File.open(write_file.to_s, 'w') do |file| file.flock(File::LOCK_EX) response.read_body do |chunk| def has_actual_content?(response) response.is_a?(Net::HTTPSuccess) && response.content_type != 'text/html'