Class: LinkChecker::IncludeItem

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

Overview

Helper class for dealing with each included item in config

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ IncludeItem

Returns a new instance of IncludeItem.



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/markdown-helpers/link_checker.rb', line 50

def initialize(config)
  @config = config
  @broken_links = []
  @config['replacements'] ||= {}
  @config['private_github'] ||= false
  return unless @config['private_github']
  unless ENV['GITHUB_OAUTH_TOKEN']
    puts "Must specify 'GITHUB_OAUTH_TOKEN' env variable to use 'private_github' config option"
    exit(1)
  end
  @github_client = Octokit::Client.new(access_token: ENV['GITHUB_OAUTH_TOKEN'])
end

Instance Attribute Details

Returns the value of attribute broken_links.



48
49
50
# File 'lib/markdown-helpers/link_checker.rb', line 48

def broken_links
  @broken_links
end

Instance Method Details



124
125
126
127
128
129
130
131
132
133
# File 'lib/markdown-helpers/link_checker.rb', line 124

def check_external_link(link)
  uri = URI(link)
  begin
    response = Net::HTTP.get_response(uri)
  rescue *HTTP_ERRORS
    puts "Error querying #{link}".red
    return false
  end
  response.is_a?(Net::HTTPSuccess) ? true : false
end

#check_file(filename) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/markdown-helpers/link_checker.rb', line 78

def check_file(filename)
  file = File.open(filename, 'r')
  file.each_with_index do |line, index|
    return false if @config['exclude_comment'] && line.include?(@config['exclude_comment'])
    links = line.scan(@config['pattern']).flatten
    check_links(links, filename, index + 1) if links.any?
  end
end


112
113
114
115
116
117
118
119
120
121
122
# File 'lib/markdown-helpers/link_checker.rb', line 112

def check_github_link(link)
  repo = link.match(%r{github\.com/([^/]*/[^/]*)(/.*)?})[1]
  path_match = link.match(%r{github\.com/.*/.*/blob/[^/]*/(.*)})
  path = path_match ? path_match[1] : '/'
  begin
    @github_client.contents(repo, path: path)
  rescue Octokit::NotFound
    return false
  end
  true
end


140
141
142
# File 'lib/markdown-helpers/link_checker.rb', line 140

def check_internal_link(link, file)
  File.exist?(File.join(File.dirname(file), link)) ? true : false
end


100
101
102
103
104
105
106
107
108
109
110
# File 'lib/markdown-helpers/link_checker.rb', line 100

def check_link(link, file)
  if link.match(%r{https://github.com}) && @config['private_github']
    check_github_link(link)
  elsif link.match(/^http.*/)
    check_external_link(link)
  elsif link.match(/^#.*/)
    check_section_link(link, file)
  else
    check_internal_link(link, file)
  end
end


87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/markdown-helpers/link_checker.rb', line 87

def check_links(links, file, line_number)
  links.each do |link|
    link = replace_values(link)
    link = link.sub(/#.*$/, '') # scrub the anchor
    next if check_link(link, file)
    @broken_links << {
      'link' => link,
      :file => file,
      :line_number => line_number
    }
  end
end

#check_path(path) ⇒ Object

glob each path



71
72
73
74
75
76
# File 'lib/markdown-helpers/link_checker.rb', line 71

def check_path(path)
  files = Dir.glob(path).select { |f| File.file?(f) } # only want files
  files.each do |filename|
    check_file(filename)
  end
end

#check_pathsObject

iterate over list of paths



64
65
66
67
68
# File 'lib/markdown-helpers/link_checker.rb', line 64

def check_paths
  @config['paths'].each do |path|
    check_path(path)
  end
end


135
136
137
138
# File 'lib/markdown-helpers/link_checker.rb', line 135

def check_section_link(link, file)
  section = link.sub(%r{/#*/}, '').split('-').each(&:capitalize).join(' ')
  File.readlines(file).grep(/#{section}/i).size > 0
end

#replace_values(link) ⇒ Object



144
145
146
147
148
149
# File 'lib/markdown-helpers/link_checker.rb', line 144

def replace_values(link)
  @config['replacements'].each do |pair|
    link = link.sub(pair['match'], pair['value'])
  end
  link
end