Class: Html5Validator::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/html5_validator/validator.rb

Constant Summary collapse

BASE_URI =
'http://validator.nu'
HEADERS =
{ 'Content-Type' => 'text/html; charset=utf-8', 'Content-Encoding' => 'UTF-8' }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proxy = nil) ⇒ Validator

Returns a new instance of Validator.



10
11
12
# File 'lib/html5_validator/validator.rb', line 10

def initialize(proxy = nil)
  RestClient.proxy = proxy unless proxy.nil?
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/html5_validator/validator.rb', line 8

def errors
  @errors
end

Instance Method Details

#inspectObject



33
34
35
36
37
# File 'lib/html5_validator/validator.rb', line 33

def inspect
  @errors.map do |err|
    "- Error: #{err['message']}"
  end.join("\n")
end

#valid?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/html5_validator/validator.rb', line 39

def valid?
  @errors.length == 0
end

#validate_file(file) ⇒ Object

TODO - Flesh out the file upload method Validate the markup of a file



30
31
# File 'lib/html5_validator/validator.rb', line 30

def validate_file(file)
end

#validate_text(text) ⇒ Object

Validate the markup of a String



15
16
17
18
19
# File 'lib/html5_validator/validator.rb', line 15

def validate_text(text)
  response = RestClient.post "#{BASE_URI}/?out=json", text, HEADERS
  @json = JSON.parse(response.body)
  @errors = retrieve_errors
end

#validate_uri(uri) ⇒ Object

Validate the markup of a URI



22
23
24
25
26
# File 'lib/html5_validator/validator.rb', line 22

def validate_uri(uri)
  response = RestClient.get BASE_URI, :params => { :doc => uri, :out => 'json' }
  @json = JSON.parse(response.body)
  @errors = retrieve_errors
end