class PeekAView::Tools::HtmlValidator

Public Class Methods

new(options) click to toggle source
Calls superclass method PeekAView::Tools::Checker::new
# File lib/peek_a_view/tools/html_validator.rb, line 9
def initialize(options)
  validator_uri = options.delete(:validator_uri)
  raise ArgumentError, "Option validator_uri must be specified" unless validator_uri
  super

  # Somehow the validator does not get this from the response
  charset = (options[:charset] || 'UTF-8').upcase

  @validator = W3CValidators::NuValidator.new(
    validator_uri: validator_uri,
    charset:       charset
  )
end

Public Instance Methods

check(uri) click to toggle source
# File lib/peek_a_view/tools/html_validator.rb, line 23
def check(uri)
  results = validate(uri)
  puts "Valid: #{results.is_valid?}"
  puts
  puts 'Errors'
  puts results.errors.map(&:message)
  puts
  puts 'Warnings'
  puts results.warnings.map(&:message)
  puts
end
report(uri) click to toggle source
# File lib/peek_a_view/tools/html_validator.rb, line 35
def report(uri)
  results = validate(uri)
  write_report(uri, results)
end

Private Instance Methods

report_dir() click to toggle source
# File lib/peek_a_view/tools/html_validator.rb, line 77
def report_dir
  File.join(Rails.root, 'reports', 'html_validator')
end
report_file(uri) click to toggle source
# File lib/peek_a_view/tools/html_validator.rb, line 81
def report_file(uri)
  report_path(uri, '.xml')
end
validate(uri) click to toggle source
# File lib/peek_a_view/tools/html_validator.rb, line 42
def validate(uri)
  @validator.validate_uri(uri)
end
write_report(uri, results) click to toggle source
# File lib/peek_a_view/tools/html_validator.rb, line 46
def write_report(uri, results)
  xml = Builder::XmlMarkup.new(indent: 2)
  xml.instruct!

  tests    = results.errors.size + results.warnings.size
  failures = results.errors.size

  xml.testsuite(name: "HTML5 Validation for #{uri}", tests: tests, failures: failures) do
    results.errors.each do |error|
      write_testcase(xml, error, 'error')
    end
    results.warnings.each do |warning|
      write_testcase(xml, warning, 'warning')
    end
  end

  File.open(report_file(uri), 'w') do |out|
    out.puts(xml.target!)
  end
end
write_testcase(xml, testcase, status) click to toggle source
# File lib/peek_a_view/tools/html_validator.rb, line 67
def write_testcase(xml, testcase, status)
  short_message = testcase.message[/^(.{20,}?\.)/, 1]
  xml.testcase(name: short_message, status: status) do
    xml.failure(message: testcase.message) do
      xml.text!("At #{testcase.line}:#{testcase.col}") if testcase.line.present?
      xml.cdata!(testcase.source) if testcase.source.present?
    end
  end
end