Class: Utterson::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/utterson/base.rb

Overview

Base implements initialization of the checking process and handles outputting final results.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
16
17
# File 'lib/utterson/base.rb', line 11

def initialize(opts={})
  @dir = opts[:dir] || './'
  @root = opts[:root] || @dir
  @errors = {}
  @checked_urls = {}
  @stats = {errors: 0, files: 0, urls: 0}
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



9
10
11
# File 'lib/utterson/base.rb', line 9

def errors
  @errors
end

Instance Method Details

#checkObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/utterson/base.rb', line 19

def check
  bar = ProgressBar.create
  threads = []
  Dir.glob(File.join(@dir, '**/*.{html,htm}')) do |f|
    @stats[:files] += 1
    bar.total = @stats[:files]
    c = HtmlCheck.new(file: f, root: @root)
    c.when_done do |r|
      bar.increment
      @stats[:urls] = r[:urls]
      @errors.merge! r[:errors]
    end
    threads << c.run
  end
  threads.each {|t| t.join}
  print_results
end


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/utterson/base.rb', line 37

def print_results
  count = 0
  @errors.each do |file, info|
    puts file
    info.each do |url, response|
      s = response.respond_to?(:code) ? "HTTP #{response.code}" : response
      puts "\t#{url}\n\t\t#{s}"
      count += 1
    end
  end
  if count == 0
    puts "#{@stats[:files]} files with #{@stats[:urls]} urls checked."
  else
    puts "Q{#{@stats[:files]} files with #{@stats[:urls]} urls checked "+
      "and #{count} errors found."
  end
end