-
-
Save bhb/6116481 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
require "addressable/uri" | |
require "uri" | |
def uri_valid?(uri) | |
uri = Addressable::URI.parse(uri) | |
# If it could not be parsed, it won't have a valid scheme | |
%w{http https}.include?(uri.scheme) | |
rescue Addressable::URI::InvalidURIError | |
false | |
end | |
urls = [ | |
"http://caius.name/", | |
"http:/caius.name/", | |
"http://ca]ius.name/", | |
"http://cai;us.name/", | |
"http://caius", | |
"http://", | |
"caius.name", | |
] | |
headers = %w(url uri-valid? uri-parse? add-valid?) | |
i = (headers | urls).map {|x| x.length }.max | |
headers.each do |h| | |
printf " %-#{i}s", h | |
end | |
puts | |
urls.each do |u| | |
printf " %-#{i+5}s", u | |
if u =~ URI.regexp | |
printf " %-#{i}s", "β" | |
else | |
printf " %-#{i}s", "π" | |
end | |
begin | |
URI(u) | |
printf " %-#{i}s", "β" | |
rescue URI::InvalidURIError => e | |
printf " %-#{i}s", "π" | |
end | |
if uri_valid?(u) | |
printf " %-#{i}s", "β" | |
else | |
printf " %-#{i}s", "π" | |
end | |
puts | |
end | |
# url uri-valid? uri-parse? add-valid? | |
# http://caius.name/ β β β | |
# http:/caius.name/ β β β | |
# http://ca]ius.name/ β π β | |
# http://cai;us.name/ β π β | |
# http://caius β β β | |
# http:// β π π | |
# caius.name π β π |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment