class ValidateWebsite::Core

Core class for static or website validation

Constants

EXIT_FAILURE_MARKUP
EXIT_FAILURE_MARKUP_NOT_FOUND
EXIT_FAILURE_NOT_FOUND
EXIT_SUCCESS
START_MESSAGE

Attributes

errors_count[R]
host[R]
not_founds_count[R]
options[R]
site[RW]

Public Class Methods

new(options, validation_type) click to toggle source

Initialize core ValidateWebsite class @example

new({ site: "https://example.com/" }, :crawl)

@param [Hash] options @param [Symbol] validation_type ‘crawl` for web or `static` for local @return [NilClass]

# File lib/validate_website/core.rb, line 39
def initialize(options, validation_type)
  @not_founds_count = 0
  @errors_count = 0
  @options = Parser.parse(options, validation_type).to_h
  @site = @options[:site]
  @service_url = @options[:html5_validator_service_url]
  Validator.html5_validator_service_url = @service_url if @service_url
end

Public Instance Methods

default_cookies() click to toggle source
# File lib/validate_website/core.rb, line 68
def default_cookies
  WEBrick::Cookie.parse(@options[:cookies]).each_with_object({}) do |c, h|
    h[c.name] = c.value
    h
  end
end
errors?() click to toggle source
# File lib/validate_website/core.rb, line 48
def errors?
  @errors_count.positive?
end
exit_status() click to toggle source
# File lib/validate_website/core.rb, line 56
def exit_status
  if errors? && not_founds?
    EXIT_FAILURE_MARKUP_NOT_FOUND
  elsif errors?
    EXIT_FAILURE_MARKUP
  elsif not_founds?
    EXIT_FAILURE_NOT_FOUND
  else
    EXIT_SUCCESS
  end
end
not_founds?() click to toggle source
# File lib/validate_website/core.rb, line 52
def not_founds?
  @not_founds_count.positive?
end

Private Instance Methods

any_css_errors?(nodes) click to toggle source
# File lib/validate_website/core.rb, line 88
def any_css_errors?(nodes)
  nodes.any? do |node|
    if node[:children]
      any_css_errors? node.delete(:children)
    elsif node[:tokens]
      any_css_errors? node.delete(:tokens)
    else
      node[:node] == :error || node[:error] == true
    end
  end
end
check_css_syntax(page) click to toggle source
# File lib/validate_website/core.rb, line 81
def check_css_syntax(page)
  nodes = Crass::Parser.parse_stylesheet(page.body)
  return unless any_css_errors?(nodes)

  handle_validation_error(page.url)
end
handle_html_validation_error(validator, url) click to toggle source
# File lib/validate_website/core.rb, line 129
def handle_html_validation_error(validator, url)
  handle_validation_error(url)
  return unless options[:verbose]

  puts color(:error, validator.errors.join(', '), options[:color])
end
handle_validation_error(url) click to toggle source
# File lib/validate_website/core.rb, line 136
def handle_validation_error(url)
  @errors_count += 1
  puts "\n"
  puts color(:error, "* #{url}", options[:color])
end
not_found_error(location) click to toggle source
# File lib/validate_website/core.rb, line 108
def not_found_error(location)
  puts "\n"
  puts color(:error, "#{location} linked but not exist", options[:color])
  @not_founds_count += 1
end
print_status_line(total, failures, not_founds, errors) click to toggle source
start_message(type) click to toggle source
# File lib/validate_website/core.rb, line 77
def start_message(type)
  puts color(:note, "#{START_MESSAGE} #{type}\n", @options[:color])
end
validate(doc, body, url, options) click to toggle source

@param [Nokogiri::HTML::Document] original_doc @param [String] The raw HTTP response body of the page @param [String] url @param [Hash] Validator options

# File lib/validate_website/core.rb, line 120
def validate(doc, body, url, options)
  validator = Validator.new(doc, body, **options)
  if validator.valid?
    print color(:success, '.', options[:color]) # rspec style
  else
    handle_html_validation_error(validator, url)
  end
end