headhunter 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8102daaade0c10e0c06ae81c5bc10d209109d2e3
4
- data.tar.gz: b05a93b7d16ac6e504be2e4ef2e0f3f562466361
3
+ metadata.gz: acb7d65c5957cda5253ba5d8a1e15844e29d93ac
4
+ data.tar.gz: d64796a9aa68bd8e2ceb68b94ab4f477d625abac
5
5
  SHA512:
6
- metadata.gz: 4c0a62b10ad5a9b9415ca6c8e574e6c6a157379a1711f4a5838a01c915662e9c936747d0d668815ecd1349afe4f70e5fe3b0f03f56d9553f94305533acaebd13
7
- data.tar.gz: 0c82cca1d97bda609576f19b658e681352d8506a0c53e9f847ea4cd94dae53cc19738db1a18fa3ea29bbd445ef2253f8f19a5a54d4d390d8f4a16567764e809c
6
+ metadata.gz: 38fdf1b08a700c7576b924315ab38717fd5b4b3777ca129b5ab6c0afbd88237cf013e04477dc1cabf82abd365e5910c942b1f1d00215dd3b037b27f31833b9b1
7
+ data.tar.gz: 04fd8d0f39ab2afda16600fc407310532a61545b703ea5adb80f774276d06df738f6c3ebb0075d9ea00fc699e2cd9d6ce86ef0872fcd5d6ad62886a945d78ddd
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- headhunter (0.0.2)
4
+ headhunter (0.0.8)
5
5
  colorize
6
6
  css_parser (~> 1.2.6)
7
7
  html_validation
data/README.md CHANGED
@@ -8,6 +8,8 @@ Headhunter is an HTML and CSS validation tool that injects itself into your Rail
8
8
 
9
9
  In addition, it also looks out for unused (and therefore superfluous) CSS selectors.
10
10
 
11
+ This is all done **locally**, so no external service is used.
12
+
11
13
  ## How to use
12
14
 
13
15
  Add Headhunter to your `Gemfile`:
@@ -20,9 +22,9 @@ end
20
22
 
21
23
  Just set the environment variable `HEADHUNTER` to `true` when running your tests, e.g.:
22
24
 
23
- - `rake HEADHUNTER=true`
24
- - `rspec HEADHUNTER=true`
25
- - `cucumber HEADHUNTER=true`
25
+ ```
26
+ $ rake HEADHUNTER=true
27
+ ```
26
28
 
27
29
  Headhunter doesn't keep your tests from passing if invalid HTML or unused CSS is found. Instead it displays a short statistic after the tests are run.
28
30
 
@@ -43,7 +45,7 @@ Headhunter doesn't keep your tests from passing if invalid HTML or unused CSS is
43
45
  Validated 1 stylesheets.
44
46
  1 stylesheet is invalid.
45
47
  application.css:
46
- - Invalid css: line 1: Property bla doesn't exist
48
+ - Line 1: Property bla doesn't exist
47
49
 
48
50
  Found 23 CSS selectors.
49
51
  20 selectors are in use.
@@ -61,22 +63,22 @@ For being able to validate CSS, `rake assets:precompile` is triggered at the beg
61
63
 
62
64
  **[Tidy HTML](http://tidy.sourceforge.net/)** should be installed on a typical OSX and Linux installation already. You're not developing on a Windows machine, are you?! If you want to validate HTML5 (and you should want to!), install the HTML5 version like described here: [homebrew tidy html5](http://techblog.willshouse.com/2013/10/21/homebrew-tidy-html5/).
63
65
 
64
- You need a **working internet connection** to run CSS validation. As a Rails application typically contains one single CSS file (`application.css`), this won't have much impact on the speed of your tests.
66
+ You need a **Java Runtime Environment** to run CSS validation. This should normally already be available on OSX and Linux machines, too. A local copy (`[css-validator.jar](http://jigsaw.w3.org/css-validator/DOWNLOAD.html)`) of the [W3 CSS Validation Service](http://jigsaw.w3.org/css-validator/) is used.
65
67
 
66
68
  ## Known issues and future plans
67
69
 
68
70
  - It would be nice to use Rails' own assets compilation that's executed when the first JavaScript test is run. Anyone has an idea on how to do this?
69
- - At the moment, in addition to precompiling and removing your assets, `rake assets:clobber` is run also **before** precompiling! The issue is explained here: [Rake assets are generated twice when precompiling them once from command line and once from within a Ruby script](http://stackoverflow.com/questions/20938891/rake-assets-are-generated-twice-when-precompiling-them-once-from-command-line-an)
70
- - Instead of running `rake assets:clobber`, it may be also sufficient to simply remove all *.css files from `public/assets/stylesheets` manually. This would save some compilation time.
71
- - Instead of using the online CSS validation service of ???, it would be nice to have a local CSS validator. Is there anything like this? TotalValidator seems to be able to do something like this, but it's not free for CSS validation and I don't know how to use it.
72
- - HTML and CSS sources should not be compressed, to allow more concise error messages
71
+ - HTML and CSS sources should not be uglified, to allow more concise error messages
73
72
  - Would be really useful to have the concrete URL of every validated HTML page. But can't find a way to extract it from Rack response.
74
73
  - There are not tests yet. I first want to see whether this gem would be appreciated by the community, and if so, I will definitely add tests.
75
74
  - Didn't try this with AJAX requests yet. Would be great if such responses would be validated, too!
75
+ - In feature tests, the same views are getting rendered again and again. These same sources shouldn't be validated over and over again!
76
+ - More configuration options needed! CSS1, CSS2, CSS2.1, CSS3! XHTML, HTML5, etc.
77
+ - Better output needed! With context lines, etc.
76
78
 
77
79
  ## Disclaimer
78
80
 
79
- Headhunter is heavily inspired by Aanand Prasad's (outdated) [Deadweight](https://github.com/aanand/deadweight) gem and by Unboxed Consulting's [be_valid_asset](https://github.com/unboxed/be_valid_asset) gem. Thank you for your pioneering work!
81
+ Headhunter is heavily inspired by Aanand Prasad's [Deadweight gem](https://github.com/aanand/deadweight), Eric Beland's [HTML Validation gem](https://github.com/ericbeland/html_validation) and Unboxed Consulting's [be\_valid\_asset gem](https://github.com/unboxed/be_valid_asset). Thank you for your pioneering work!
80
82
 
81
83
  **USE THIS GEM AT YOUR OWN RISK!**
82
84
 
Binary file
Binary file
@@ -2,7 +2,28 @@ require 'net/http'
2
2
  require 'rexml/document'
3
3
 
4
4
  module Headhunter
5
+ class LocalResponse
6
+ attr_reader :body
7
+
8
+ def initialize(body)
9
+ @body = body
10
+ @headers = {'x-w3c-validator-status' => valid?(body)}
11
+ end
12
+
13
+ def [](key)
14
+ @headers[key]
15
+ end
16
+
17
+ private
18
+
19
+ def valid?(body)
20
+ REXML::Document.new(body).root.each_element('//m:validity') { |e| return e.text == 'true' }
21
+ end
22
+ end
23
+
5
24
  class CssValidator
25
+ USE_LOCAL_VALIDATOR = true
26
+
6
27
  def initialize(stylesheets)
7
28
  @profile = 'css3' # TODO: Option for profile css1 and css21
8
29
  @stylesheets = stylesheets
@@ -31,6 +52,8 @@ module Headhunter
31
52
 
32
53
  messages.each { |message| log.puts " - #{message}".red }
33
54
  end
55
+
56
+ log.puts
34
57
  end
35
58
 
36
59
  private
@@ -53,10 +76,18 @@ module Headhunter
53
76
  @messages_per_stylesheet[file] = []
54
77
 
55
78
  REXML::Document.new(response.body).root.each_element('//m:error') do |e|
56
- @messages_per_stylesheet[file] << "#{error_line_prefix}: line #{e.elements['m:line'].text}: #{e.elements['m:message'].get_text.value.strip}\n"
79
+ @messages_per_stylesheet[file] << "Line #{extract_line_from_error(e)}: #{extract_message_from_error(e)}"
57
80
  end
58
81
  end
59
82
 
83
+ def extract_line_from_error(e)
84
+ e.elements['m:line'].text
85
+ end
86
+
87
+ def extract_message_from_error(e)
88
+ e.elements['m:message'].get_text.value.strip[0..-2]
89
+ end
90
+
60
91
  def fetch(path) # TODO: Move to Headhunter!
61
92
  loc = path
62
93
 
@@ -71,24 +102,50 @@ module Headhunter
71
102
 
72
103
  def get_validation_response(query_params)
73
104
  query_params.merge!({:output => 'soap12'})
74
- get_validator_response(query_params)
105
+
106
+ if USE_LOCAL_VALIDATOR
107
+ call_local_validator(query_params)
108
+ else
109
+ call_remote_validator(query_params)
110
+ end
75
111
  end
76
112
 
77
113
  def response_indicates_valid?(response)
78
114
  response['x-w3c-validator-status'] == 'Valid'
79
115
  end
80
116
 
81
- def get_validator_response(query_params = {})
82
- response = call_validator(query_params)
117
+ def call_remote_validator(query_params = {})
118
+ boundary = Digest::MD5.hexdigest(Time.now.to_s)
119
+ data = encode_multipart_params(boundary, query_params)
120
+ response = http_start(validator_host).post2(validator_path,
121
+ data,
122
+ 'Content-type' => "multipart/form-data; boundary=#{boundary}")
83
123
 
84
124
  raise "HTTP error: #{response.code}" unless response.is_a? Net::HTTPSuccess
85
- return response
125
+ response
86
126
  end
87
127
 
88
- def call_validator(query_params)
89
- boundary = Digest::MD5.hexdigest(Time.now.to_s)
90
- data = encode_multipart_params(boundary, query_params)
91
- return http_start(validator_host).post2(validator_path, data, "Content-type" => "multipart/form-data; boundary=#{boundary}" )
128
+ def call_local_validator(query_params)
129
+ path = Gem.loaded_specs['headhunter'].full_gem_path + '/lib/css-validator/'
130
+ css_file = 'tmp.css'
131
+ results_file = 'results'
132
+ results = nil
133
+
134
+ Dir.chdir(path) do
135
+ File.open(css_file, 'a') { |f| f.write query_params[:text] }
136
+
137
+ # See http://stackoverflow.com/questions/1137884/is-there-an-open-source-css-validator-that-can-be-run-locally
138
+ if system "java -jar css-validator.jar --output=soap12 file:#{css_file} > #{results_file}"
139
+ results = IO.read results_file
140
+ else
141
+ raise 'Could not execute local validation!'
142
+ end
143
+
144
+ File.delete css_file
145
+ File.delete results_file
146
+ end
147
+
148
+ LocalResponse.new(results)
92
149
  end
93
150
 
94
151
  def encode_multipart_params(boundary, params = {})
@@ -1,3 +1,3 @@
1
1
  module Headhunter
2
- VERSION = '0.0.8'
2
+ VERSION = '0.0.9'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: headhunter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Muheim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-19 00:00:00.000000000 Z
11
+ date: 2014-01-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -145,6 +145,11 @@ executables: []
145
145
  extensions: []
146
146
  extra_rdoc_files: []
147
147
  files:
148
+ - lib/css-validator/css-validator.jar
149
+ - lib/css-validator/lib/commons-collections-3.2.1.jar
150
+ - lib/css-validator/lib/commons-lang-2.6.jar
151
+ - lib/css-validator/lib/jigsaw.jar
152
+ - lib/css-validator/lib/velocity-1.7.jar
148
153
  - lib/headhunter/css_hunter.rb
149
154
  - lib/headhunter/css_validator.rb
150
155
  - lib/headhunter/html_validator.rb