Class: Headhunter::CssValidator

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

Defined Under Namespace

Classes: Response

Constant Summary collapse

VALIDATOR_DIR =
Gem.loaded_specs['headhunter'].full_gem_path + '/lib/css-validator/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stylesheets = [], profile = 'css3', vextwarning = true) ⇒ CssValidator

Returns a new instance of CssValidator.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/headhunter/css_validator.rb', line 12

def initialize(stylesheets = [], profile = 'css3', vextwarning = true)
  @stylesheets = stylesheets
  @profile     = profile     # TODO!
  @vextwarning = vextwarning # TODO!

  @responses = []

  @stylesheets.map do |stylesheet|
    validate(stylesheet)
  end
end

Instance Attribute Details

#responsesObject (readonly)

Returns the value of attribute responses.



10
11
12
# File 'lib/headhunter/css_validator.rb', line 10

def responses
  @responses
end

#stylesheetsObject (readonly)

Returns the value of attribute stylesheets.



10
11
12
# File 'lib/headhunter/css_validator.rb', line 10

def stylesheets
  @stylesheets
end

Instance Method Details

#extract_filename(path) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/headhunter/css_validator.rb', line 65

def extract_filename(path)
  if matches = path.match(/public\/assets\/([a-z\-_]*)-([a-z0-9]{32})(\.css)$/)
    matches[1] + matches[3] # application-d205d6f344d8623ca0323cb6f6bd7ca1.css becomes application.css
  else
    File.basename(path)
  end
end

#invalid_responsesObject



43
44
45
# File 'lib/headhunter/css_validator.rb', line 43

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

#statisticsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/headhunter/css_validator.rb', line 47

def statistics
  lines = []

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

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

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

  lines.join("\n")
end

#valid_responsesObject



39
40
41
# File 'lib/headhunter/css_validator.rb', line 39

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

#validate(uri) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/headhunter/css_validator.rb', line 24

def validate(uri)
  Dir.chdir(VALIDATOR_DIR) do
    raise "Couldn't locate uri #{uri}" unless File.exists? uri

    # See http://stackoverflow.com/questions/1137884/is-there-an-open-source-css-validator-that-can-be-run-locally
    # More config options see http://jigsaw.w3.org/css-validator/manual.html
    stdin, stdout, stderr = Open3.popen3("java -jar css-validator.jar --output=soap12 file:#{uri}")
    stdin.close
    stderr.close

    @responses << Response.new(stdout.read)
    stdout.close
  end
end

#x_stylesheets_be(size) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/headhunter/css_validator.rb', line 73

def x_stylesheets_be(size)
  if size <= 1
    "#{size} stylesheet is"
  else
    "#{size} stylesheets are"
  end
end