Skip to content

Instantly share code, notes, and snippets.

@caraya
Created July 16, 2012 04:10
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 caraya/3120460 to your computer and use it in GitHub Desktop.
Save caraya/3120460 to your computer and use it in GitHub Desktop.
#!/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
#!/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