Skip to content

Instantly share code, notes, and snippets.

@bhb
Forked from caius/gist:6002173
Last active December 20, 2015 10:31
Show Gist options
  • Save bhb/6116481 to your computer and use it in GitHub Desktop.
Save bhb/6116481 to your computer and use it in GitHub Desktop.
# -*- 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