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/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHtmlValidator

Returns a new instance of HtmlValidator.



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

def initialize
  @responses = []
end

Instance Attribute Details

#responsesObject (readonly)

Returns the value of attribute responses.



7
8
9
# File 'lib/headhunter/html_validator.rb', line 7

def responses
  @responses
end

Instance Method Details

#invalid_responsesObject



32
33
34
# File 'lib/headhunter/html_validator.rb', line 32

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

#statisticsObject



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

def statistics
  lines = []

  lines << "Validated #{responses.size} pages.".yellow
  lines << "All pages are valid.".green if invalid_responses.size == 0
  lines << "#{x_pages_be(invalid_responses.size)} invalid.".red if invalid_responses.size > 0

  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



28
29
30
# File 'lib/headhunter/html_validator.rb', line 28

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

#validate(uri, html) ⇒ Object



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

def validate(uri, html)
  Dir.chdir(VALIDATOR_DIR) do
    # Docs for Tidy: http://tidy.sourceforge.net/docs/quickref.html
    stdin, stdout, stderr = Open3.popen3('tidy -quiet')
    stdin.puts html
    stdin.close
    stdout.close

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

  @responses.last
end

#x_pages_be(size) ⇒ Object



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

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