Class: ValidateWebsite::Validator
- Inherits:
-
Object
- Object
- ValidateWebsite::Validator
- Defined in:
- lib/validate_website/validator.rb
Constant Summary collapse
- XHTML_PATH =
File.join(File.dirname(__FILE__), '..', '..', 'data', 'schemas')
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
-
#dtd ⇒ Object
readonly
Returns the value of attribute dtd.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
-
#original_doc ⇒ Object
readonly
Returns the value of attribute original_doc.
-
#xsd ⇒ Object
readonly
Returns the value of attribute xsd.
Instance Method Summary collapse
-
#initialize(original_doc, body, opts = {}) ⇒ Validator
constructor
A new instance of Validator.
- #valid? ⇒ Boolean
Constructor Details
#initialize(original_doc, body, opts = {}) ⇒ Validator
Returns a new instance of Validator.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/validate_website/validator.rb', line 14 def initialize(original_doc, body, opts={}) @original_doc = original_doc @body = body @options = opts @dtd = @original_doc.internal_subset init_namespace(@dtd) @errors = [] if @errors.empty? if @dtd_uri && @body.match(@dtd_uri.to_s) document = @body.sub(@dtd_uri.to_s, @namespace + '.dtd') else document = @body end @doc = Dir.chdir(XHTML_PATH) do Nokogiri::XML(document) { |cfg| cfg.noent.dtdload.dtdvalid } end # http://www.w3.org/TR/xhtml1-schema/ @xsd = Dir.chdir(XHTML_PATH) do if @namespace && File.exists?(@namespace + '.xsd') Nokogiri::XML::Schema(File.read(@namespace + '.xsd')) end end if @xsd # have the xsd so use it @errors = @xsd.validate(@doc) elsif document =~ /^\<!DOCTYPE html\>/i # TODO: use a local Java, Python parser... write a Ruby HTML5 parser ? require 'net/http' require 'multipart_body' url = URI.parse('http://validator.nu/') multipart = MultipartBody.new(:content => document) http = Net::HTTP.new(url.host) headers = { 'Content-Type' => "multipart/form-data; boundary=#{multipart.boundary}", 'Content-Length' => multipart.to_s.bytesize.to_s, } res = http.start {|con| con.post(url.path, multipart.to_s, headers) } @errors = Nokogiri::XML.parse(res.body).css('ol li.error').map(&:content) else # dont have xsd fall back to dtd @doc = Dir.chdir(XHTML_PATH) do Nokogiri::HTML.parse(document) end @errors = @doc.errors end end rescue Nokogiri::XML::SyntaxError => e # http://nokogiri.org/tutorials/ensuring_well_formed_markup.html @errors << e end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
9 10 11 |
# File 'lib/validate_website/validator.rb', line 9 def body @body end |
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
9 10 11 |
# File 'lib/validate_website/validator.rb', line 9 def doc @doc end |
#dtd ⇒ Object (readonly)
Returns the value of attribute dtd.
9 10 11 |
# File 'lib/validate_website/validator.rb', line 9 def dtd @dtd end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
9 10 11 |
# File 'lib/validate_website/validator.rb', line 9 def errors @errors end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
9 10 11 |
# File 'lib/validate_website/validator.rb', line 9 def namespace @namespace end |
#original_doc ⇒ Object (readonly)
Returns the value of attribute original_doc.
9 10 11 |
# File 'lib/validate_website/validator.rb', line 9 def original_doc @original_doc end |
#xsd ⇒ Object (readonly)
Returns the value of attribute xsd.
9 10 11 |
# File 'lib/validate_website/validator.rb', line 9 def xsd @xsd end |
Instance Method Details
#valid? ⇒ Boolean
73 74 75 |
# File 'lib/validate_website/validator.rb', line 73 def valid? errors.length == 0 end |