forked from jondot/awesome-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.rb
53 lines (46 loc) · 1.16 KB
/
validate.rb
1
2
3
4
5
6
7
8
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
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'parallel'
require 'open-uri'
require 'net/http'
require 'rexml/document'
require 'kramdown'
require 'httparty'
def check_link(uri)
HTTParty.head(uri, :verify => false).code.to_i.tap do |status|
if (400..422).include?(status)
if status != 403 && !uri.exclude?('udemy.com')
raise "Request had status #{status}"
else
putc('S')
end
end
end
end
BASE_URI = ENV['BASE_URI'] || 'https://github.com/jondot/awesome-react-native'
html_readme = "<html>#{Kramdown::Document.new(open('README.md').read).to_html}</html>"
readme_doctree = REXML::Document.new(html_readme)
links = REXML::XPath.match(readme_doctree, '//a')
puts "Validating #{links.size} links..."
invalids = []
Parallel.each(links, in_threads: 4) do |link|
href = link.attribute('href').to_s
begin
case check_link(URI.join(BASE_URI, href))
when (200...300)
putc('.')
when (300..302)
putc('w')
end
rescue => e
putc('F')
invalids << "#{href} (reason: #{e.message})"
end
end
unless invalids.empty?
puts "\n\nFailed links:"
invalids.each do |link|
puts "- #{link}"
end
puts "Done with errors."
exit(1)
end
# puts "\nDone."