Created
March 19, 2012 13:55
-
-
Save Arood/2113129 to your computer and use it in GitHub Desktop.
Validate a URI
This file contains hidden or 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
# 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