w3c_validators 1.0.2 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -26,3 +26,7 @@
26
26
  == Version 1.0.2
27
27
  * Added support for proxy servers
28
28
  * Fixed to minor unit test errors
29
+
30
+ == Version 1.1
31
+ * Ruby 1.9 compatibility
32
+ * Switched to Nokogiri for XML parsing
@@ -109,12 +109,13 @@ protected
109
109
 
110
110
 
111
111
  def parse_soap_response(response) # :nodoc:
112
- doc = REXML::Document.new(response)
112
+ doc = Nokogiri::XML(response)
113
+ doc.remove_namespaces!
113
114
 
114
115
  result_params = {}
115
116
 
116
117
  {:uri => 'uri', :checked_by => 'checkedby', :validity => 'validity', :css_level => 'csslevel'}.each do |local_key, remote_key|
117
- if val = doc.elements["//*[local-name()='cssvalidationresponse']/*[local-name()='#{remote_key.to_s}']"]
118
+ if val = doc.at('cssvalidationresponse ' + remote_key)
118
119
  result_params[local_key] = val.text
119
120
  end
120
121
  end
@@ -122,17 +123,17 @@ protected
122
123
  results = Results.new(result_params)
123
124
 
124
125
  ['warninglist', 'errorlist'].each do |list_type|
125
- doc.elements.each("//*[local-name()='#{list_type.to_s}']") do |message_list|
126
+ doc.css(list_type).each do |message_list|
126
127
 
127
- if uri_node = message_list.elements["*[local-name()='uri']"]
128
+ if uri_node = message_list.at('uri')
128
129
  uri = uri_node.text
129
130
  end
130
131
 
131
132
  [:warning, :error].each do |msg_type|
132
- message_list.elements.each("*[local-name()='#{msg_type.to_s}']") do |message|
133
+ message_list.css(msg_type.to_s).each do |message|
133
134
  message_params = {}
134
- message.each_element_with_text do |el|
135
- message_params[el.name.to_sym] = el.text
135
+ message.children.each do |el|
136
+ message_params[el.name.to_sym] = el.text unless el.blank?
136
137
  end
137
138
  message_params[:uri] = uri
138
139
  results.add_message(msg_type, message_params)
@@ -81,12 +81,13 @@ protected
81
81
  #
82
82
  # Returns W3CValidators::Results.
83
83
  def parse_soap_response(response) # :nodoc:
84
- doc = REXML::Document.new(response)
84
+ doc = Nokogiri::XML(response)
85
+ doc.remove_namespaces!
85
86
 
86
87
  result_params = {}
87
88
 
88
89
  {:uri => 'uri', :checked_by => 'checkedby', :validity => 'validity'}.each do |local_key, remote_key|
89
- if val = doc.elements["//*[local-name()='feedvalidationresponse']/*[local-name()='#{remote_key.to_s}']"]
90
+ if val = doc.at('feedvalidationresponse ' + remote_key)
90
91
  result_params[local_key] = val.text
91
92
  end
92
93
  end
@@ -94,10 +95,10 @@ protected
94
95
  results = Results.new(result_params)
95
96
 
96
97
  [:warning, :error].each do |msg_type|
97
- doc.elements.each("//*[local-name()='#{msg_type.to_s}']") do |message|
98
+ doc.css(msg_type.to_s).each do |message|
98
99
  message_params = {}
99
- message.each_element_with_text do |el|
100
- message_params[el.name.to_sym] = el.text
100
+ message.children.each do |el|
101
+ message_params[el.name.to_sym] = el.text unless el.blank?
101
102
  end
102
103
  results.add_message(msg_type, message_params)
103
104
  end
@@ -130,7 +130,6 @@ protected
130
130
  else
131
131
  response = send_request(options, :post)
132
132
  end
133
-
134
133
  @results = parse_soap_response(response.body)
135
134
  end
136
135
  @results
@@ -169,36 +168,35 @@ protected
169
168
  #
170
169
  # Returns W3CValidators::Results.
171
170
  def parse_soap_response(response) # :nodoc:
172
- doc = REXML::Document.new(response)
171
+ doc = Nokogiri::XML(response)
172
+ doc.remove_namespaces!
173
173
 
174
174
  result_params = {}
175
175
 
176
- {:doctype => 'm:doctype', :uri => 'm:uri', :charset => 'm:charset',
177
- :checked_by => 'm:checkedby', :validity => 'm:validity'}.each do |local_key, remote_key|
178
- if val = doc.elements["env:Envelope/env:Body/m:markupvalidationresponse/#{remote_key}"]
176
+ {:doctype => 'doctype', :uri => 'uri', :charset => 'charset',
177
+ :checked_by => 'checkedby', :validity => 'validity'}.each do |local_key, remote_key|
178
+ if val = doc.css(remote_key)
179
179
  result_params[local_key] = val.text
180
180
  end
181
181
  end
182
182
 
183
183
  results = Results.new(result_params)
184
184
 
185
- {:warning => 'm:warnings/m:warninglist/m:warning', :error => 'm:errors/m:errorlist/m:error'}.each do |local_type, remote_type|
186
- doc.elements.each("env:Envelope/env:Body/m:markupvalidationresponse/#{remote_type}") do |message|
185
+ {:warning => 'warnings warning', :error => 'errorlist error'}.each do |local_type, remote_type|
186
+ doc.css(remote_type).each do |message|
187
187
  message_params = {}
188
- message.each_element_with_text do |el|
189
- message_params[el.name.to_sym] = el.text
188
+ message.children do |el|
189
+ message_params[el.name.to_sym] = el.text unless el.blank?
190
190
  end
191
191
  results.add_message(local_type, message_params)
192
192
  end
193
193
  end
194
194
 
195
- doc.elements.each("env:Envelope/env:Body/env:Fault/env:Reason") do |message|
196
- message.elements.each("env:Text") do |m|
197
- results.add_message(:error, {:mesage => m.text})
198
- end
195
+ doc.css("Fault Reason Text").each do |message|
196
+ results.add_message(:error, {:mesage => message.text})
199
197
  end
200
-
201
- doc.elements.each("env:Envelope/env:Body/m:markupvalidationresponse/m:debug") do |debug|
198
+
199
+ doc.css("markupvalidationresponse debug").each do |debug|
202
200
  results.add_debug_message(debug.attribute('name').value, debug.text)
203
201
  end
204
202
  return results
@@ -2,7 +2,7 @@ require 'json'
2
2
 
3
3
  module W3CValidators
4
4
  class NuValidator < Validator
5
- MARKUP_VALIDATOR_URI = 'http://validator.nu/'
5
+ MARKUP_VALIDATOR_URI = 'http://html5.validator.nu/'
6
6
 
7
7
  # Create a new instance of the NuValidator.
8
8
  #
@@ -16,6 +16,7 @@ module W3CValidators
16
16
  #
17
17
  # See Validator#new for proxy server options.
18
18
  def initialize(options = {})
19
+ options[:parser] = 'html'
19
20
  if options[:validator_uri]
20
21
  @validator_uri = URI.parse(options[:validator_uri])
21
22
  options.delete(options[:validator_uri])
@@ -62,11 +63,11 @@ module W3CValidators
62
63
  protected
63
64
  def validate(options) # :nodoc:
64
65
  options = get_request_options(options)
65
-
66
+
66
67
  if options.has_key?(:doc)
67
68
  response = send_request(options, :get)
68
69
  else
69
- response = send_request(options, :post)
70
+ response = send_request(options, :post, false, :content)
70
71
  end
71
72
 
72
73
  @results = parse_json_response(response.body)
@@ -1,7 +1,8 @@
1
1
  require 'cgi'
2
2
  require 'net/http'
3
3
  require 'uri'
4
- require 'rexml/document'
4
+ require 'json'
5
+ require 'nokogiri'
5
6
 
6
7
  require 'w3c_validators/exceptions'
7
8
  require 'w3c_validators/constants'
@@ -39,7 +40,7 @@ module W3CValidators
39
40
  # +request_mode+ must be either <tt>:get</tt>, <tt>:head</tt> or <tt>:post</tt>.
40
41
  #
41
42
  # Returns Net::HTTPResponse.
42
- def send_request(options, request_mode = :get)
43
+ def send_request(options, request_mode = :get, following_redirect = false, params_to_post = nil)
43
44
  response = nil
44
45
  results = nil
45
46
 
@@ -60,13 +61,30 @@ module W3CValidators
60
61
  response = http.get(@validator_uri.path + '?' + query)
61
62
  when :post
62
63
  # send a multipart form request
63
- query, boundary = create_multipart_data(options)
64
- response = http.post2(@validator_uri.path, query, "Content-type" => "multipart/form-data; boundary=" + boundary)
64
+ if params_to_post
65
+ post = {}
66
+ [params_to_post].flatten.each do |param|
67
+ post[param] = options.delete(param)
68
+ end
69
+ else
70
+ post = options
71
+ options = {}
72
+ end
73
+
74
+ qs = create_query_string_data(options)
75
+
76
+ query, boundary = create_multipart_data(post)
77
+ response = http.post2(@validator_uri.path + '?' + qs, query, "Content-type" => "multipart/form-data; boundary=" + boundary)
65
78
  else
66
79
  raise ArgumentError, "request_mode must be either :get, :head or :post"
67
80
  end
68
81
  end
69
82
 
83
+ if response.kind_of?(Net::HTTPRedirection) and response['location'] and not following_redirect
84
+ options[:url] = response['location']
85
+ return send_request(options, request_mode, true)
86
+ end
87
+
70
88
  response.value
71
89
  return response
72
90
 
@@ -135,7 +153,7 @@ module W3CValidators
135
153
  when Net::HTTPServerException, SocketError
136
154
  msg = "unable to connect to the validator at #{@validator_uri} (response was #{e.message})."
137
155
  raise ValidatorUnavailable, msg, caller
138
- when REXML::ParseException
156
+ when JSON::ParserError, Nokogiri::XML::SyntaxError
139
157
  msg = "unable to parse the response from the validator."
140
158
  raise ParsingError, msg, caller
141
159
  else
@@ -1,7 +1,5 @@
1
- $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__), '../'))
2
- $LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__), '../lib/'))
3
-
4
- require 'rubygems'
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+ require 'iconv'
5
3
  require 'test/unit'
6
4
  require 'w3c_validators'
7
5
 
metadata CHANGED
@@ -1,36 +1,47 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.0
3
- specification_version: 1
4
2
  name: w3c_validators
5
3
  version: !ruby/object:Gem::Version
6
- version: 1.0.2
7
- date: 2009-11-20 00:00:00 -08:00
8
- summary: Wrapper for the World Wide Web Consortium's online validation services.
9
- require_paths:
10
- - lib
11
- email: code@dunae.ca
12
- homepage: http://code.dunae.ca/w3c_validators
13
- rubyforge_project:
14
- description: W3C Validators is a Ruby wrapper for the World Wide Web Consortium's online validation services.
15
- autorequire:
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ hash: 13
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 1
9
+ version: "1.1"
25
10
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
11
  authors:
30
12
  - Alex Dunae
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-20 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: nokogiri
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: W3C Validators is a Ruby wrapper for the World Wide Web Consortium's online validation services.
35
+ email: code@dunae.ca
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - README.rdoc
42
+ - CHANGELOG
43
+ - LICENSE
31
44
  files:
32
- - lib/w3c_validators
33
- - lib/w3c_validators.rb
34
45
  - lib/w3c_validators/constants.rb
35
46
  - lib/w3c_validators/css_validator.rb
36
47
  - lib/w3c_validators/exceptions.rb
@@ -40,10 +51,10 @@ files:
40
51
  - lib/w3c_validators/nu_validator.rb
41
52
  - lib/w3c_validators/results.rb
42
53
  - lib/w3c_validators/validator.rb
54
+ - lib/w3c_validators.rb
43
55
  - README.rdoc
44
56
  - CHANGELOG
45
57
  - LICENSE
46
- test_files:
47
58
  - test/test_css_validator.rb
48
59
  - test/test_exceptions.rb
49
60
  - test/test_feed_validator.rb
@@ -51,21 +62,49 @@ test_files:
51
62
  - test/test_html5_validator.rb
52
63
  - test/test_markup_validator.rb
53
64
  - test/test_proxy.rb
65
+ has_rdoc: true
66
+ homepage: http://code.dunae.ca/w3c_validators
67
+ licenses: []
68
+
69
+ post_install_message:
54
70
  rdoc_options:
55
71
  - --all
56
72
  - --inline-source
57
73
  - --line-numbers
58
74
  - --charset
59
75
  - utf-8
60
- extra_rdoc_files:
61
- - README.rdoc
62
- - CHANGELOG
63
- - LICENSE
64
- executables: []
65
-
66
- extensions: []
67
-
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 3
93
+ segments:
94
+ - 0
95
+ version: "0"
68
96
  requirements: []
69
97
 
70
- dependencies: []
71
-
98
+ rubyforge_project:
99
+ rubygems_version: 1.3.7
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Wrapper for the World Wide Web Consortium's online validation services.
103
+ test_files:
104
+ - test/test_css_validator.rb
105
+ - test/test_exceptions.rb
106
+ - test/test_feed_validator.rb
107
+ - test/test_helper.rb
108
+ - test/test_html5_validator.rb
109
+ - test/test_markup_validator.rb
110
+ - test/test_proxy.rb