Class: HTMLProofer::Runner
- Inherits:
-
Object
- Object
- HTMLProofer::Runner
- Includes:
- Utils
- Defined in:
- lib/html-proofer/runner.rb
Instance Attribute Summary collapse
-
#external_urls ⇒ Object
readonly
Returns the value of attribute external_urls.
-
#failures ⇒ Object
readonly
Returns the value of attribute failures.
-
#internal_urls ⇒ Object
readonly
Returns the value of attribute internal_urls.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#before_request(&block) {|Typhoeus::Request| ... } ⇒ Array<Block>
Set before_request callback.
-
#check_files ⇒ Object
Collects any external URLs found in a directory of files.
- #check_list_of_links ⇒ Object
- #check_parsed(html, path) ⇒ Object
- #check_path(path) ⇒ Object
- #checks ⇒ Object
- #failed_tests ⇒ Object
- #files ⇒ Object
- #ignore_file?(file) ⇒ Boolean
-
#initialize(src, opts = {}) ⇒ Runner
constructor
A new instance of Runner.
- #load_internal_cache ⇒ Object
- #print_failed_tests ⇒ Object
-
#process_files ⇒ Object
Walks over each implemented check and runs them on the files, in parallel.
- #run ⇒ Object
- #validate_external_urls ⇒ Object
- #validate_internal_urls ⇒ Object
Methods included from Utils
#create_nokogiri, #pluralize, #swap
Constructor Details
#initialize(src, opts = {}) ⇒ Runner
Returns a new instance of Runner.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/html-proofer/runner.rb', line 9 def initialize(src, opts = {}) @src = src @options = HTMLProofer::Configuration::PROOFER_DEFAULTS.merge(opts) @options[:typhoeus] = HTMLProofer::Configuration::TYPHOEUS_DEFAULTS.merge(opts[:typhoeus] || {}) @options[:hydra] = HTMLProofer::Configuration::HYDRA_DEFAULTS.merge(opts[:hydra] || {}) @options[:parallel] = HTMLProofer::Configuration::PARALLEL_DEFAULTS.merge(opts[:parallel] || {}) @options[:validation] = HTMLProofer::Configuration::VALIDATION_DEFAULTS.merge(opts[:validation] || {}) @options[:cache] = HTMLProofer::Configuration::CACHE_DEFAULTS.merge(opts[:cache] || {}) @type = @options.delete(:type) @logger = HTMLProofer::Log.new(@options[:log_level]) @cache = Cache.new(@logger, @options[:cache]) @internal_link_checks = nil # Add swap patterns for internal domains unless @options[:internal_domains].empty? @options[:internal_domains].each do |dom| @options[:url_swap][Regexp.new("^http://#{dom}")] = '' @options[:url_swap][Regexp.new("^https://#{dom}")] = '' @options[:url_swap][Regexp.new("^//#{dom}")] = '' end end @internal_urls = {} @internal_urls_to_paths = {} @external_urls = {} @failures = [] @before_request = [] end |
Instance Attribute Details
#external_urls ⇒ Object (readonly)
Returns the value of attribute external_urls.
7 8 9 |
# File 'lib/html-proofer/runner.rb', line 7 def external_urls @external_urls end |
#failures ⇒ Object (readonly)
Returns the value of attribute failures.
7 8 9 |
# File 'lib/html-proofer/runner.rb', line 7 def failures @failures end |
#internal_urls ⇒ Object (readonly)
Returns the value of attribute internal_urls.
7 8 9 |
# File 'lib/html-proofer/runner.rb', line 7 def internal_urls @internal_urls end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/html-proofer/runner.rb', line 7 def @options end |
Instance Method Details
#before_request(&block) {|Typhoeus::Request| ... } ⇒ Array<Block>
Set before_request callback.
227 228 229 230 231 |
# File 'lib/html-proofer/runner.rb', line 227 def before_request(&block) @before_request ||= [] @before_request << block if block_given? @before_request end |
#check_files ⇒ Object
Collects any external URLs found in a directory of files. Also collectes every failed test from process_files. Sends the external URLs to Typhoeus for batch processing.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/html-proofer/runner.rb', line 73 def check_files process_files.each do |item| @external_urls.merge!(item[:external_urls]) @failures.concat(item[:failures]) end # TODO: lazy. if we're checking only external links, # we'll just trash all the failed tests. really, we should # just not run those other checks at all. if @options[:external_only] @failures = [] validate_external_urls elsif !@options[:disable_external] validate_external_urls validate_internal_urls else validate_internal_urls end end |
#check_list_of_links ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/html-proofer/runner.rb', line 60 def check_list_of_links if @options[:url_swap] @src = @src.map do |url| swap(url, @options[:url_swap]) end end @external_urls = Hash[*@src.map { |s| [s, nil] }.flatten] validate_external_urls end |
#check_parsed(html, path) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/html-proofer/runner.rb', line 102 def check_parsed(html, path) result = { external_urls: {}, failures: [] } @src = [@src] if @type == :file @src.each do |src| checks.each do |klass| @logger.log :debug, "Checking #{klass.to_s.downcase} on #{path} ..." check = Object.const_get(klass).new(src, path, html, @logger, @cache, @options) check.run if klass == 'LinkCheck' @internal_link_checks = check check.internal_urls.each_pair do |url, internal_urls| if @internal_urls_to_paths[url] @internal_urls_to_paths[url].concat(internal_urls.map(&:path)) else @internal_urls_to_paths[url] = internal_urls.map(&:path) end end @internal_urls.merge!(check.internal_urls) end external_urls = check.external_urls external_urls = Hash[check.external_urls.map { |url, file| [swap(url, @options[:url_swap]), file] }] if @options[:url_swap] result[:external_urls].merge!(external_urls) result[:failures].concat(check.issues) end end result end |
#check_path(path) ⇒ Object
134 135 136 |
# File 'lib/html-proofer/runner.rb', line 134 def check_path(path) check_parsed(create_nokogiri(path), path) end |
#checks ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/html-proofer/runner.rb', line 187 def checks return @checks if defined?(@checks) && !@checks.nil? return (@checks = ['LinkCheck']) if @type == :links @checks = HTMLProofer::Check.subchecks.map(&:name) @checks.delete('FaviconCheck') unless @options[:check_favicon] @checks.delete('HtmlCheck') unless @options[:check_html] @checks.delete('OpenGraphCheck') unless @options[:check_opengraph] @options[:checks_to_ignore].each { |ignored| @checks.delete(ignored) } @checks end |
#failed_tests ⇒ Object
200 201 202 203 204 205 206 |
# File 'lib/html-proofer/runner.rb', line 200 def failed_tests result = [] return result if @failures.empty? @failures.each { |f| result << f.to_s } result end |
#files ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/html-proofer/runner.rb', line 164 def files @files ||= if @type == :directory @src.map do |src| pattern = File.join(src, '**', "*#{@options[:extension]}") files = Dir.glob(pattern).select { |fn| File.file? fn } files.reject { |f| ignore_file?(f) } end.flatten elsif @type == :file && File.extname(@src) == @options[:extension] [@src].reject { |f| ignore_file?(f) } else [] end end |
#ignore_file?(file) ⇒ Boolean
178 179 180 181 182 183 184 185 |
# File 'lib/html-proofer/runner.rb', line 178 def ignore_file?(file) @options[:file_ignore].each do |pattern| return true if pattern.is_a?(String) && pattern == file return true if pattern.is_a?(Regexp) && pattern =~ file end false end |
#load_internal_cache ⇒ Object
233 234 235 236 237 238 239 |
# File 'lib/html-proofer/runner.rb', line 233 def load_internal_cache urls_to_check = @cache.retrieve_urls(@internal_urls) cache_text = pluralize(urls_to_check.count, 'internal link', 'internal links') @logger.log :info, "Found #{cache_text} in the cache..." urls_to_check end |
#print_failed_tests ⇒ Object
208 209 210 211 212 213 214 215 |
# File 'lib/html-proofer/runner.rb', line 208 def print_failed_tests sorted_failures = SortedIssues.new(@failures, @options[:error_sort], @logger) sorted_failures.sort_and_report count = @failures.length failure_text = pluralize(count, 'failure', 'failures') raise @logger.colorize :fatal, "HTML-Proofer found #{failure_text}!" end |
#process_files ⇒ Object
Walks over each implemented check and runs them on the files, in parallel.
94 95 96 97 98 99 100 |
# File 'lib/html-proofer/runner.rb', line 94 def process_files if @options[:parallel].empty? files.map { |path| check_path(path) } else Parallel.map(files, @options[:parallel]) { |path| check_path(path) } end end |
#run ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/html-proofer/runner.rb', line 42 def run if @type == :links @logger.log :info, "Running #{checks} on #{@src}... \n\n" check_list_of_links unless @options[:disable_external] else @logger.log :info, "Running #{checks} on #{@src} on *#{@options[:extension]}... \n\n" check_files file_text = pluralize(files.length, 'file', 'files') @logger.log :info, "Ran on #{file_text}!\n\n" end if @failures.empty? @logger.log :info, 'HTML-Proofer finished successfully.' else print_failed_tests end end |
#validate_external_urls ⇒ Object
138 139 140 141 142 143 |
# File 'lib/html-proofer/runner.rb', line 138 def validate_external_urls url_validator = HTMLProofer::UrlValidator.new(@logger, @cache, @external_urls, @options) url_validator.before_request = @before_request @failures.concat(url_validator.run) @external_urls = url_validator.external_urls end |
#validate_internal_urls ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/html-proofer/runner.rb', line 145 def validate_internal_urls if @cache.use_cache? urls_to_check = load_internal_cache urls_to_check.each_pair do |url, internal_urls| result = @internal_link_checks.check_internal_link(internal_urls.first.link, internal_urls.first.line, internal_urls.first.content) code = result ? 200 : 404 @cache.add(url, @internal_urls_to_paths[url].sort, code, '') # TODO: blank msg for now end @cache.write else @internal_urls.values.flatten.each do |internal_url| @internal_link_checks.check_internal_link(internal_url.link, internal_url.line, internal_url.content) end end @failures.concat(@internal_link_checks.issues) unless @internal_urls.length.zero? end |