Class: HTML::Proofer
- Inherits:
-
Object
show all
- Defined in:
- lib/html/proofer.rb,
lib/html/proofer/checks.rb,
lib/html/proofer/version.rb,
lib/html/proofer/checkable.rb
Defined Under Namespace
Classes: Checkable, Checks
Constant Summary
collapse
- VERSION =
"0.6.2"
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(src, opts = {}) ⇒ Proofer
Returns a new instance of Proofer.
8
9
10
11
12
13
14
15
|
# File 'lib/html/proofer.rb', line 8
def initialize(src, opts={})
@srcDir = src
@proofer_opts = {:ext => ".html", :href_swap => [], :href_ignore => [], :disable_external => false }
@options = @proofer_opts.merge({:followlocation => true}).merge(opts)
@failed_tests = []
end
|
Class Method Details
.create_nokogiri(path) ⇒ Object
108
109
110
111
112
|
# File 'lib/html/proofer.rb', line 108
def self.create_nokogiri(path)
path << "/index.html" if File.directory? path content = File.open(path, "rb") {|f| f.read }
Nokogiri::HTML(content)
end
|
Instance Method Details
#files ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/html/proofer.rb', line 100
def files
if File.directory? @srcDir
Dir.glob("#{@srcDir}/**/*#{@options[:ext]}")
else
File.extname(@srcDir) == @options[:ext] ? [@srcDir] : []
end
end
|
#hydra ⇒ Object
96
97
98
|
# File 'lib/html/proofer.rb', line 96
def hydra
@hydra ||= Typhoeus::Hydra.new
end
|
#response_handler(response, filenames) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/html/proofer.rb', line 71
def response_handler(response, filenames)
href = response.options[:effective_url]
method = response.request.options[:method]
response_code = response.code
if response_code.between?(200, 299)
elsif response.timed_out?
@failed_tests << "#{filenames.join(' ').blue}: External link #{href} failed: got a time out"
elsif (response_code == 405 || response_code == 420 || response_code == 503) && method == :head
uri = URI(href)
next_response = Typhoeus.get(uri.scheme + "://" + uri.host + uri.path, @options)
response_handler(next_response, filenames)
elsif method == :head
next_response = Typhoeus.get(href, @options)
response_handler(next_response, filenames)
else
@failed_tests << "#{filenames.join(' ').blue}: External link #{href} failed: #{response_code} #{response.return_message}"
end
end
|
#run ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/html/proofer.rb', line 17
def run
total_files = 0
external_urls = {}
puts "Running #{get_checks} checks on #{@srcDir} on *#{@options[:ext]}... \n\n"
files.each do |path|
total_files += 1
html = HTML::Proofer.create_nokogiri(path)
get_checks.each do |klass|
check = klass.new(@srcDir, path, html, @options)
check.run
external_urls.merge!(check.external_urls)
@failed_tests.concat(check.issues) if check.issues.length > 0
end
end
external_urls = Hash[external_urls.sort]
unless @options[:disable_external]
puts "Checking #{external_urls.length} external links... \n\n"
@proofer_opts.each_key do |opt|
@options.delete opt
end
external_urls.each_pair do |href, filenames|
request = Typhoeus::Request.new(href, @options.merge({:method => :head}))
request.on_complete { |response| response_handler(response, filenames) }
hydra.queue request
end
hydra.run
end
puts "Ran on #{total_files} files!"
if @failed_tests.empty?
puts "HTML-Proofer finished successfully.".green
else
@failed_tests.each do |issue|
$stderr.puts issue + "\n\n"
end
raise "HTML-Proofer found #{@failed_tests.length} failures!"
end
end
|