class Rowr::LinkProcessor

Attributes

cached[RW]
containing_file[RW]
local_site_dir[R]
new_base_path[RW]
old_domain[R]
target_file[R]

Public Class Methods

new(src_dir, old_domain = nil, new_base_path = nil, check_external_urls = true, cached = {}) click to toggle source
# File lib/rowr/link_processor.rb, line 15
def initialize(src_dir, old_domain = nil, new_base_path = nil, check_external_urls = true, cached = {})
  @printer = Rowr::Printer.new
  @prompt = TTY::Prompt.new(active_color: :cyan)
  @pastel = Pastel.new
  @local_site_dir = src_dir
  @old_domain = old_domain
  @new_base_path = new_base_path
  @check_external_urls = check_external_urls
  @cached = cached
end

Public Instance Methods

add_to_cache(new_link) click to toggle source
# File lib/rowr/link_processor.rb, line 112
def add_to_cache(new_link)
  @cached[link_key] = new_link
end
ask_to_cache(new_link) click to toggle source
# File lib/rowr/link_processor.rb, line 183
def ask_to_cache(new_link)
  case new_link
  when nil
    message = "SKIP all instances of " + @pastel.green("#{@link_to_check}") + "?"
  when '#'
    message = "REMOVE all instances of " + @pastel.green("#{@link_to_check}") + "?"
  else
    message = "REPLACE all instances of " + @pastel.green("#{@link_to_check}") + " with " + @pastel.blue("#{new_link}") + "?"
  end
  add_to_cache(new_link) if @prompt.yes?(message)
end
ask_wtd() click to toggle source
# File lib/rowr/link_processor.rb, line 195
def ask_wtd
  @printer.line_break 0
  wtd = @prompt.enum_select"What would you like to do?" do |menu|
    menu.default 1

    menu.choice 'Enter a new link', 1
    menu.choice 'Remove the link', 2
    menu.choice 'Skip', 3
  end

  case wtd
  when 1
    ask_new_link
  when 2
    '#'
  when 3
    nil
  end
end
external?(link) click to toggle source

Checkers

# File lib/rowr/link_processor.rb, line 53
def external?(link)
  !old_uri?(link) && uri?(link) ? true : false
end
in_cache?() click to toggle source
# File lib/rowr/link_processor.rb, line 69
def in_cache?
  @cached.key?(link_key)
end
is_valid_replacement?(link) click to toggle source
# File lib/rowr/link_processor.rb, line 95
def is_valid_replacement?(link)
  if uri?(link)
    res = response_code(link)
    res < 400 || res > 199
  else
    File.exist?(File.join(@local_site_dir, link))
  end
end
old_uri?(link) click to toggle source
# File lib/rowr/link_processor.rb, line 57
def old_uri?(link)
  if @old_domain
    link =~ old_url_regex
  else
    false
  end
end
old_url_regex() click to toggle source
# File lib/rowr/link_processor.rb, line 45
def old_url_regex
  %r{^(https?://|//)#{@old_domain}}i if @old_domain
end
prepend_new_base_path(link) click to toggle source
# File lib/rowr/link_processor.rb, line 122
def prepend_new_base_path(link)
  check = @new_base_path[1..-1].chop
  new_link = link.sub(%r{^/?#{check}},'')
  new_link = new_link.sub(/^\//,'')
  @new_base_path + new_link
end
process(link, file = nil) click to toggle source
# File lib/rowr/link_processor.rb, line 155
def process(link, file = nil)
  @containing_file = file if file
  self.link_to_check = link

  if external?(@link_to_check)
    replacement = process_external
  else
    replacement = process_link
    replacement = process_broken_link unless replacement
  end
  replacement
end
process_external() click to toggle source
# File lib/rowr/link_processor.rb, line 147
def process_external
  return nil unless @check_external_urls && broken_external_link?
  @printer.print_broken_link_warning @containing_file, @link_to_check
  replacement = ask_wtd
  ask_to_cache(replacement)
  replacement
end
recommend_files() click to toggle source
# File lib/rowr/link_processor.rb, line 116
def recommend_files
  Dir.glob("#{@local_site_dir}/**/{#{File.basename(@target_file)}}").map! do |f|
    f.sub(@local_site_dir,'')
  end
end
response_code(link) click to toggle source
# File lib/rowr/link_processor.rb, line 73
def response_code(link)
  begin
    res = Faraday.get link
    return res.status
  rescue
    return 0
  end
end
target_file_exists?() click to toggle source
# File lib/rowr/link_processor.rb, line 86
def target_file_exists?
  File.exist?(trim_hash(@target_file))
end
trim_hash(file) click to toggle source
# File lib/rowr/link_processor.rb, line 82
def trim_hash(file)
  file.sub(/#(.*?)$/,'')
end
uri?(link) click to toggle source
# File lib/rowr/link_processor.rb, line 65
def uri?(link)
  link =~ %r{^(https?:|//)}i
end