Created
July 16, 2012 04:10
-
-
Save caraya/3120460 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
#!/usr/bin/env ruby -w | |
require_relative '../lib/ctools/validator.rb' | |
class Tool_test | |
puts $LOAD_PATH | |
@html = File.new('/Users/carlos/experiment/code/conversion_tools/test/index.html').read | |
Ctools.Validator.validate_text(@html) | |
end |
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
#!/usr/bin/env ruby -w | |
require 'json' | |
require 'rest_client' | |
module Ctools | |
class Validator | |
# Based on the code from https://github.com/damian/html5_validator/blob/master/lib/html5_validator.rb | |
attr_reader :errors | |
BASE_URI = 'http://html5.validator.nu' | |
HEADERS = { 'Content-Type' => 'text/html; charset=utf-8', 'Content-Encoding' => 'UTF-8' } | |
def initialize(proxy = nil) | |
RestClient.proxy = proxy unless proxy.nil? | |
end | |
# Validate the markup of a String | |
def validate_text(text) | |
response = RestClient.post "#{BASE_URI}/?out=json", text, HEADERS | |
@json = JSON.parse(response.body) | |
@errors = retrieve_errors | |
end | |
# Validate the markup of a URI | |
def validate_uri(uri) | |
response = RestClient.get BASE_URI, :params => { :doc => uri, :out => 'json' } | |
@json = JSON.parse(response.body) | |
@errors = retrieve_errors | |
end | |
def inspect | |
@errors.map do |err| | |
"- Error: #{err['message']}" | |
end.join("\n") | |
end | |
def valid? | |
@errors.length == 0 | |
end | |
private | |
def retrieve_errors | |
@json['messages'].select { |mssg| mssg['type'] == 'error' } | |
end | |
end #class | |
end # module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment