Class: SiteValidator::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/site_validator/page.rb

Overview

A page has an URL to be validated, and a collection of errors In case of an exception happens when validating, it is tracked

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}) ⇒ Page

Returns a new instance of Page.



15
16
17
18
19
20
21
22
# File 'lib/site_validator/page.rb', line 15

def initialize(url, options = {})
  options = defaults.merge(options)

  @url           = url
  @timeout       = options[:timeout]
  @validator_uri = options[:validator_uri]
  @user_agent    = options[:user_agent]
end

Instance Attribute Details

#exceptionObject

Returns the value of attribute exception.



13
14
15
# File 'lib/site_validator/page.rb', line 13

def exception
  @exception
end

#timeoutObject

Returns the value of attribute timeout.



13
14
15
# File 'lib/site_validator/page.rb', line 13

def timeout
  @timeout
end

#urlObject

Returns the value of attribute url.



13
14
15
# File 'lib/site_validator/page.rb', line 13

def url
  @url
end

#user_agentObject

Returns the value of attribute user_agent.



13
14
15
# File 'lib/site_validator/page.rb', line 13

def user_agent
  @user_agent
end

#validator_uriObject

Returns the value of attribute validator_uri.



13
14
15
# File 'lib/site_validator/page.rb', line 13

def validator_uri
  @validator_uri
end

Instance Method Details

#errorsObject

Returns the collection of errors from the validations of this page. If it has no validation errors, it will be an empty array. It an exception occurs, it will be nil.



38
39
40
41
42
43
44
45
46
47
# File 'lib/site_validator/page.rb', line 38

def errors
  @errors ||= validations.errors
                .select {|e| e.message_id && !e.message_id.empty?}
                .map do |e|
    SiteValidator::Message.new(e.message_id, e.line, e.col, e.message, :error, e.source, prepare_w3c_explanation(e))
  end
rescue Exception => e
  @exception = e.to_s
  nil
end

#valid?Boolean

Checks for errors and returns true if none found, false otherwise. Warnings are not considered as validation errors so a page with warnings but without errors will return true. If the validation goes well, errors should be an array. Otherwise it will still be nil, which will not be considered validated.

Returns:

  • (Boolean)


30
31
32
# File 'lib/site_validator/page.rb', line 30

def valid?
  !errors.nil? && errors.empty?
end

#warningsObject

Returns the collection of warnings from the validations of this page. If it has no validation warnings, it will be an empty array. It an exception occurs, it will be nil.



53
54
55
56
57
58
59
60
61
62
# File 'lib/site_validator/page.rb', line 53

def warnings
  @warnings ||= validations.warnings
                 .select {|w| w.message_id && !w.message_id.empty?}
                 .map do |w|
    SiteValidator::Message.new(w.message_id, w.line, w.col, w.message, :warning, w.source, prepare_w3c_explanation(w))
  end
rescue Exception => e
  @exception = e.to_s
  nil
end