Skip to content

Instantly share code, notes, and snippets.

@Arood
Created March 19, 2012 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arood/2113129 to your computer and use it in GitHub Desktop.
Save Arood/2113129 to your computer and use it in GitHub Desktop.
Validate a URI
# Modified https://gist.github.com/9281 to validate a remote URI instead.
# Wouldn't surprise me if a better CLI-tool is already available, but hey,
# we might improve it later. Also: Pretty colors!
task :validate, :site_uri do | t, args|
require 'net/http'
require 'w3c_validators'
include W3CValidators
desc "W3C validation of a URI"
private
def colorize(text, color_code); "#{color_code}#{text}\e[0m"; end
def red(text); colorize(text, "\e[31m"); end
def green(text); colorize(text, "\e[32m"); end
if args.site_uri[-3,3] == "css"
@validator = CSSValidator.new
else
@validator = MarkupValidator.new
end
rofl = Net::HTTP.get_response(URI.parse(args.site_uri)).body
results = @validator.validate_text(rofl)
if results.errors.length > 0
results.errors.each do |err|
err = "#{err}"
err[0,38] = ""
puts "\t #{args.site_uri} => #{red(err)}"
end
else
puts "\t #{args.site_uri} => #{green('Valid!')}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment