Class: Headhunter::HtmlValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/headhunter/html_validator.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

VALIDATOR_DIR =
Gem.loaded_specs['headhunter'].full_gem_path + '/lib/tidy/'
EXECUTABLE =
'tidy'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHtmlValidator

Returns a new instance of HtmlValidator.



11
12
13
# File 'lib/headhunter/html_validator.rb', line 11

def initialize
  @responses = []
end

Instance Attribute Details

#responsesObject (readonly)

Returns the value of attribute responses.



9
10
11
# File 'lib/headhunter/html_validator.rb', line 9

def responses
  @responses
end

Instance Method Details

#invalid_responsesObject



34
35
36
# File 'lib/headhunter/html_validator.rb', line 34

def invalid_responses
  @responses.reject(&:valid?)
end

#statisticsObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/headhunter/html_validator.rb', line 38

def statistics
  lines = []

  lines << "Validated #{responses.size} pages.".yellow
  lines << "All pages are valid.".green unless invalid_responses.any?
  lines << "#{x_pages_be(invalid_responses.size)} invalid.".red if invalid_responses.any?

  invalid_responses.each do |response|
    lines << "  #{response.uri}:".red

    response.errors.each do |error|
      lines << "    - #{error.to_s}".red
    end
  end

  lines.join("\n")
end

#valid_responsesObject



30
31
32
# File 'lib/headhunter/html_validator.rb', line 30

def valid_responses
  @responses.select(&:valid?)
end

#validate(uri, html) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/headhunter/html_validator.rb', line 15

def validate(uri, html)
  Dir.chdir(VALIDATOR_DIR) do
    raise "Could not find tidy in #{Dir.pwd}" unless File.exists? EXECUTABLE

    # Docs for Tidy: http://tidy.sourceforge.net/docs/quickref.html
    stdin, stdout, stderr = Open3.popen3("#{EXECUTABLE} -quiet")
    stdin.puts html
    stdin.close
    stdout.close

    @responses << Response.new(stderr.read, uri)
    stderr.close
  end
end

#x_pages_be(size) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/headhunter/html_validator.rb', line 56

def x_pages_be(size)
  if size <= 1
    "#{size} page is"
  else
    "#{size} pages are"
  end
end