Class: LinkChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown-helpers/link_checker.rb

Overview

Class to help verify local and external links

Defined Under Namespace

Classes: IncludeItem

Constant Summary collapse

HTTP_ERRORS =
[
  EOFError,
  Errno::ECONNRESET,
  Errno::EINVAL,
  Net::HTTPBadResponse,
  Net::HTTPHeaderSyntaxError,
  Net::ProtocolError,
  Timeout::Error,
  SocketError
]

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ LinkChecker

Returns a new instance of LinkChecker.



17
18
19
20
21
22
23
24
# File 'lib/markdown-helpers/link_checker.rb', line 17

def initialize(config_file)
  @config = YAML.load_file(config_file)
  unless @config['include'] && @config['include'].any?
    puts "Must declare files in 'include' of config".red
    exit 1
  end
  @broken_links = []
end

Instance Method Details

#checkObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/markdown-helpers/link_checker.rb', line 26

def check
  @config['include'].each do |included|
    included_object = IncludeItem.new(included)
    included_object.check_paths
    @broken_links << included_object.broken_links.flatten
  end
  @broken_links.flatten!
  if @broken_links.any?
    @broken_links.each do |link_hash|
      output = "Broken link: '#{link_hash['link']}'\n" \
        " in file: '#{link_hash[:file]}'\n" \
        " on line '#{link_hash[:line_number]}'"
      puts output.red
    end
    exit(1)
  else
    puts 'No broken links :)'
  end
end