Class: HTML::Proofer
- Inherits:
-
Object
show all
- Includes:
- Yell::Loggable
- Defined in:
- lib/html/proofer.rb,
lib/html/proofer/checks.rb,
lib/html/proofer/checkable.rb
Defined Under Namespace
Classes: Checkable, Checks
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(src, opts = {}) ⇒ Proofer
Returns a new instance of Proofer.
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/html/proofer.rb', line 13
def initialize(src, opts={})
@srcDir = src
@proofer_opts = {:ext => ".html", :href_swap => [], :href_ignore => [], :disable_external => false, :verbose => false }
@options = @proofer_opts.merge({:followlocation => true}).merge(opts)
@failed_tests = []
Yell.new({ :format => false, :name => "HTML::Proofer", :level => "gte.#{log_level}" }) do |l|
l.adapter :stdout, level: [:debug, :info, :warn]
l.adapter :stderr, level: [:error, :fatal]
end
end
|
Instance Attribute Details
#failed_tests ⇒ Object
Returns the value of attribute failed_tests.
11
12
13
|
# File 'lib/html/proofer.rb', line 11
def failed_tests
@failed_tests
end
|
Class Method Details
.create_nokogiri(path) ⇒ Object
124
125
126
127
128
|
# File 'lib/html/proofer.rb', line 124
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
116
117
118
119
120
121
122
|
# File 'lib/html/proofer.rb', line 116
def files
if File.directory? @srcDir
Dir.glob("#{@srcDir}/**/*#{@options[:ext]}")
else
File.extname(@srcDir) == @options[:ext] ? [@srcDir] : []
end
end
|
#get_checks ⇒ Object
130
131
132
|
# File 'lib/html/proofer.rb', line 130
def get_checks
HTML::Proofer::Checks::Check.subclasses
end
|
#hydra ⇒ Object
112
113
114
|
# File 'lib/html/proofer.rb', line 112
def hydra
@hydra ||= Typhoeus::Hydra.new
end
|
#log_level ⇒ Object
134
135
136
|
# File 'lib/html/proofer.rb', line 134
def log_level
@options[:verbose] ? :debug : :info
end
|
#response_handler(response, filenames) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/html/proofer.rb', line 85
def response_handler(response, filenames)
href = response.options[:effective_url]
method = response.request.options[:method]
response_code = response.code
logger.debug "Received a #{response_code} for #{href} in #{filenames.join(' ')}"
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/html/proofer.rb', line 27
def run
total_files = 0
external_urls = {}
logger.info "Running #{get_checks} checks on #{@srcDir} on *#{@options[:ext]}... \n\n".white
files.each do |path|
total_files += 1
html = HTML::Proofer.create_nokogiri(path)
get_checks.each do |klass|
logger.debug "Checking #{klass.to_s.downcase} on #{path} ...".blue
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]
logger.info "Checking #{external_urls.length} external links...".yellow
@proofer_opts.each_key do |opt|
@options.delete opt
end
Ethon.logger = logger
external_urls.each_pair do |href, filenames|
request = Typhoeus::Request.new(href, @options.merge({:method => :head, :timeout => 100}))
request.on_complete { |response| response_handler(response, filenames) }
hydra.queue request
end
logger.debug "Running requests for all #{hydra.queued_requests.size} external URLs...".yellow
hydra.run
end
logger.info "Ran on #{total_files} files!\n\n".green
if @failed_tests.empty?
logger.info "HTML-Proofer finished successfully.".green
else
@failed_tests.each do |issue|
logger.error (issue + "\n\n").red
end
raise "HTML-Proofer found #{@failed_tests.length} failures!".red
end
end
|