Class: Rowr::LinkProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/rowr/link_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src_dir, old_domain = nil, new_base_path = nil, check_external_urls = true, cached = {}) ⇒ LinkProcessor

Returns a new instance of LinkProcessor.



15
16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#cachedObject

Returns the value of attribute cached.



10
11
12
# File 'lib/rowr/link_processor.rb', line 10

def cached
  @cached
end

#containing_fileObject

Returns the value of attribute containing_file.



12
13
14
# File 'lib/rowr/link_processor.rb', line 12

def containing_file
  @containing_file
end

Returns the value of attribute link_to_check.



11
12
13
# File 'lib/rowr/link_processor.rb', line 11

def link_to_check
  @link_to_check
end

#local_site_dirObject (readonly)

Returns the value of attribute local_site_dir.



7
8
9
# File 'lib/rowr/link_processor.rb', line 7

def local_site_dir
  @local_site_dir
end

#new_base_pathObject

Returns the value of attribute new_base_path.



9
10
11
# File 'lib/rowr/link_processor.rb', line 9

def new_base_path
  @new_base_path
end

#old_domainObject (readonly)

Returns the value of attribute old_domain.



8
9
10
# File 'lib/rowr/link_processor.rb', line 8

def old_domain
  @old_domain
end

#target_fileObject (readonly)

Returns the value of attribute target_file.



13
14
15
# File 'lib/rowr/link_processor.rb', line 13

def target_file
  @target_file
end

Instance Method Details

#add_to_cache(new_link) ⇒ Object



112
113
114
# File 'lib/rowr/link_processor.rb', line 112

def add_to_cache(new_link)
  @cached[link_key] = new_link
end


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/rowr/link_processor.rb', line 215

def ask_new_link
  new_link = @prompt.ask('Enter the replacement:')
  unless is_valid_replacement?(new_link)
    if uri?(new_link)
      @prompt.error("Sorry, the url you've provided is not returning a 200 status code")
    else
      @prompt.error('Sorry, that file does not exist')
    end
    new_link = ask_new_link
  end

  if uri?(new_link)
    new_link
  else
    prepend_new_base_path(new_link)
  end
end

Asks



172
173
174
175
176
177
178
179
180
181
# File 'lib/rowr/link_processor.rb', line 172

def ask_recommended_files
  @printer.print_line ' I found some matching files ', '+', :blue
  recommended_files = recommend_files
  choice = @prompt.select(
      'Would you like to replace the broken link with any of the following?',
      recommended_files + ['None of these match'],
      per_page: 10
  )
  choice == 'None of these match' ? nil : prepend_new_base_path(choice)
end

#ask_to_cache(new_link) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
# 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_wtdObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# 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

#broken_external_link?Boolean

Returns:



90
91
92
93
# File 'lib/rowr/link_processor.rb', line 90

def broken_external_link?
  res = response_code(@link_to_check)
  res > 399 || res < 200
end

#external?(link) ⇒ Boolean

Checkers

Returns:



53
54
55
# File 'lib/rowr/link_processor.rb', line 53

def external?(link)
  !old_uri?(link) && uri?(link) ? true : false
end

#in_cache?Boolean

Returns:



69
70
71
# File 'lib/rowr/link_processor.rb', line 69

def in_cache?
  @cached.key?(link_key)
end

#is_valid_replacement?(link) ⇒ Boolean

Returns:



95
96
97
98
99
100
101
102
# 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

Misc



108
109
110
# File 'lib/rowr/link_processor.rb', line 108

def link_key
  @link_to_check.to_sym
end

#old_uri?(link) ⇒ Boolean

Returns:



57
58
59
60
61
62
63
# 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_regexObject



45
46
47
# 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) ⇒ Object



122
123
124
125
126
127
# 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) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
# 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


137
138
139
140
141
142
143
144
145
# File 'lib/rowr/link_processor.rb', line 137

def process_broken_link
  return cached[link_key] if in_cache?
  replacement = nil
  @printer.print_broken_link_warning @containing_file, @link_to_check
  replacement = ask_recommended_files unless recommend_files.empty?
  replacement = ask_wtd unless replacement
  ask_to_cache(replacement)
  replacement
end

#process_externalObject



147
148
149
150
151
152
153
# 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

Processors



133
134
135
# File 'lib/rowr/link_processor.rb', line 133

def process_link
  @new_base_path + @link_to_check if target_file_exists?
end

#recommend_filesObject



116
117
118
119
120
# 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) ⇒ Object



73
74
75
76
77
78
79
80
# 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?Boolean

Returns:



86
87
88
# File 'lib/rowr/link_processor.rb', line 86

def target_file_exists?
  File.exist?(trim_hash(@target_file))
end

#trim_hash(file) ⇒ Object



82
83
84
# File 'lib/rowr/link_processor.rb', line 82

def trim_hash(file)
  file.sub(/#(.*?)$/,'')
end

#uri?(link) ⇒ Boolean

Returns:



65
66
67
# File 'lib/rowr/link_processor.rb', line 65

def uri?(link)
  link =~ %r{^(https?:|//)}i
end