diff --git a/.gitignore b/.gitignore index ee9482e8..2c859920 100644 --- a/.gitignore +++ b/.gitignore @@ -24,3 +24,5 @@ docs/ _yardoc doc/ .DS_Store + +.htmlproofer.log diff --git a/lib/html/proofer/cache.rb b/lib/html/proofer/cache.rb index ffec4c27..6d3735eb 100644 --- a/lib/html/proofer/cache.rb +++ b/lib/html/proofer/cache.rb @@ -1,16 +1,57 @@ +require 'json' + module HTML class Proofer - module Cache - def create_nokogiri(path) - if File.exist? path - content = File.open(path).read + class Cache + attr_reader :status + + FILENAME = '.htmlproofer.log' + + def initialize(now, options) + @status = {} + @now = now + @status[:time] = @now + @status[:urls] = [] + + if options.nil? || options.empty? + @store = false + else + @store = true + @timeframe = options[:timeframe] + end + + if File.exist?(FILENAME) + @exists = true + @cache_log = JSON.parse(File.read(FILENAME)) else - content = path + @exists = false end + end + + def store? + @store + end + + def exists? + @exists && within_timeframe + end + + def within_timeframe + + end + + def load + + end + + def add(url, status) + return unless store? + @status[:urls] << { :url => url, :status => status } + end - Nokogiri::HTML(content) + def write + File.write(FILENAME, @status.to_json) end - module_function :create_nokogiri end end end diff --git a/lib/html/proofer/url_validator.rb b/lib/html/proofer/url_validator.rb index 58b69719..eab8aa60 100644 --- a/lib/html/proofer/url_validator.rb +++ b/lib/html/proofer/url_validator.rb @@ -1,6 +1,7 @@ require 'typhoeus' require 'uri' require_relative './utils' +require_relative './cache' module HTML class Proofer @@ -16,10 +17,17 @@ def initialize(logger, external_urls, options, typhoeus_opts, hydra_opts) @options = options @hydra = Typhoeus::Hydra.new(hydra_opts) @typhoeus_opts = typhoeus_opts + @cache = Cache.new(Time.now, @options[:cache]) end def run - external_link_checker(external_urls) + if @cache.exists? + @cache.load + else + external_link_checker(external_urls) + @cache.write if @cache.store? + end + @failed_tests end @@ -81,14 +89,17 @@ def response_handler(response, filenames) if response_code.between?(200, 299) check_hash_in_2xx_response(href, effective_url, response, filenames) + @cache.add(href, response_code) elsif response.timed_out? handle_timeout(href, filenames, response_code) + @cache.add(href, 0) elsif method == :head queue_request(:get, href, filenames) else return if @options[:only_4xx] && !response_code.between?(400, 499) # Received a non-successful http response. add_external_issue(filenames, "External link #{href} failed: #{response_code} #{response.return_message}", response_code) + @cache.add(href, response_code) end end diff --git a/spec/html/proofer/cache_spec.rb b/spec/html/proofer/cache_spec.rb new file mode 100644 index 00000000..8f6f87a4 --- /dev/null +++ b/spec/html/proofer/cache_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' + +describe 'Cache test' do + it 'ignores an invalid tag by default' do + brokenLinkExternalFilepath = "#{FIXTURES_DIR}/links/brokenLinkExternal.html" + proofer = run_proofer(brokenLinkExternalFilepath) + expect(proofer.failed_tests.first).to match(/failed: 0/) + + run_proofer(brokenLinkExternalFilepath, { :cache => { :timeframe => '30d' } }) + end + + +end