Class: Relevance::Tarantula::W3CValidator

Inherits:
Object
  • Object
show all
Includes:
Relevance::Tarantula
Defined in:
lib/relevance/tarantula/w3c_validator.rb

Instance Method Summary collapse

Methods included from Relevance::Tarantula

#log, #rails_root, #tarantula_home, #verbose

Constructor Details

#initialize(options = {}) ⇒ W3CValidator

Possible options: :validator_uri => ‘localhost/w3c-validator/check

(if not specified, public w3c validator is used)

:show_warnings => true (default: false)



11
12
13
14
# File 'lib/relevance/tarantula/w3c_validator.rb', line 11

def initialize(options = {})
  @show_warnings = options.delete(:show_warnings)
  @validator = W3CValidators::MarkupValidator.new(options)
end

Instance Method Details

#handle(result) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/relevance/tarantula/w3c_validator.rb', line 16

def handle(result)
  response = result.response
  return unless response.html? and response.code.to_s == '200'

  results = @validator.validate_text(response.body)

  if !results.errors.empty? or (@show_warnings and !results.warnings.empty?)
    error_result = result.dup
    error_result.description = "Bad HTML (W3C Validator)"
    error_result.data = results.errors.collect {|e| "Line: #{e.line}, column: #{e.col}, error: #{e.message}"}.join("\n")
    if @show_warnings
      error_result.data << "\n" unless error_result.data.empty?
      error_result.data << results.warnings.collect {|e| "Line: #{e.line}, column: #{e.col}, warning: #{e.message}"}.join("\n")
    end
    error_result
  end
end