Class: ValidateWebsite::Core

Inherits:
Object
  • Object
show all
Includes:
ColorfulMessages
Defined in:
lib/validate_website/core.rb

Overview

Core class for static or website validation

Constant Summary collapse

EXIT_SUCCESS =
0
EXIT_FAILURE_MARKUP =
64
EXIT_FAILURE_NOT_FOUND =
65
EXIT_FAILURE_MARKUP_NOT_FOUND =
66
PING_URL =
'http://www.google.com/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ColorfulMessages

#color, #error, #info, #note, #success, #warning

Constructor Details

#initialize(options = {}, validation_type = :crawl) ⇒ Core

Returns a new instance of Core.



28
29
30
31
32
33
34
# File 'lib/validate_website/core.rb', line 28

def initialize(options = {}, validation_type = :crawl)
  @not_founds_count = 0
  @errors_count = 0
  @options = Parser.parse(options, validation_type)
  @site = @options[:site]
  puts color(:note, "validating #{@site}\n", @options[:color])
end

Instance Attribute Details

#crawler ⇒ Object (readonly)

Returns the value of attribute crawler.



17
18
19
# File 'lib/validate_website/core.rb', line 17

def crawler
  @crawler
end

#errors_count ⇒ Object (readonly)

Returns the value of attribute errors_count.



17
18
19
# File 'lib/validate_website/core.rb', line 17

def errors_count
  @errors_count
end

#host ⇒ Object (readonly)

Returns the value of attribute host.



17
18
19
# File 'lib/validate_website/core.rb', line 17

def host
  @host
end

#not_founds_count ⇒ Object (readonly)

Returns the value of attribute not_founds_count.



17
18
19
# File 'lib/validate_website/core.rb', line 17

def not_founds_count
  @not_founds_count
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/validate_website/core.rb', line 17

def options
  @options
end

#site ⇒ Object

Returns the value of attribute site.



16
17
18
# File 'lib/validate_website/core.rb', line 16

def site
  @site
end

Instance Method Details

#crawl(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {}) —

    :color [Boolean] color output (true, false) :exclude [String] a String used by Regexp.new :markup [Boolean] Check the markup validity :not_found [Boolean] Check for not found page (404)



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/validate_website/core.rb', line 42

def crawl(options = {})
  @options = @options.to_hash.merge(options)
  @options.merge!(ignore_links: @options[:exclude]) if @options[:exclude]
  puts color(:warning, "No internet connection") unless internet_connection?

  @crawler = spidr_crawler(@site, @options)
  print_status_line(@crawler.history.size,
                    @crawler.failures.size,
                    @not_founds_count,
                    @errors_count)
end

#crawl_static(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/validate_website/core.rb', line 56

def crawl_static(options = {})
  @options = @options.to_hash.merge(options)
  @site = @options[:site]

  files = Dir.glob(@options[:pattern])
  files.each do |f|
    next unless File.file?(f)

    response = fake_httpresponse(open(f).read)
    page = Spidr::Page.new(URI.join(@site, URI.encode(f)), response)

    validate(page.doc, page.body, f) if @options[:markup]
    check_static_not_found(page.links) if @options[:not_found]
  end
  print_status_line(files.size, 0, @not_founds_count, @errors_count)
end

#default_cookies ⇒ Object



93
94
95
96
97
98
# File 'lib/validate_website/core.rb', line 93

def default_cookies
  WEBrick::Cookie.parse(@options[:cookies]).each_with_object({}) do |c, h|
    h[c.name] = c.value
    h
  end
end

#errors? ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/validate_website/core.rb', line 73

def errors?
  @errors_count > 0
end

#exit_status ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/validate_website/core.rb', line 81

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? ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/validate_website/core.rb', line 77

def not_founds?
  @not_founds_count > 0
end