Class: DisavowTool::List

Inherits:
Object
  • Object
show all
Defined in:
lib/disavow_tool/list.rb

Direct Known Subclasses

DisavowList, ImportedLinks, WhiteList

Instance Method Summary collapse

Constructor Details

#initialize(import_files) ⇒ List

Returns a new instance of List.



10
11
12
13
14
15
16
17
# File 'lib/disavow_tool/list.rb', line 10

def initialize(import_files)
  @list = Set.new
  @list_files_imported = []
  @verbose = OPTIONS.verbose
  @verbose_hard = OPTIONS.hardcore_verbose
  import import_files
  @original_list = @list.clone
end

Instance Method Details

#add_domain(domain, list = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/disavow_tool/list.rb', line 39

def add_domain(domain, list=nil)
  list = list || @list
  if list.add? domain
    color_domain = domain.on_yellow
    puts add_url_message(color_domain).blue if @verbose
  else
    puts "Not adding #{domain}. Already in the list.".red if @verbose
  end
end

#add_url(url, list = nil) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/disavow_tool/list.rb', line 49

def add_url(url, list=nil)
  list = list || @list
  if list.add? url
    puts add_url_message(url).blue if @verbose
  else
    puts "Not adding #{url}. Already in the list.".red if @verbose
  end
end

#clean_line!Object



8
# File 'lib/disavow_tool/list.rb', line 8

def clean_line!; raise "SubclassResponsibility" ; end

#comment?(line) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/disavow_tool/list.rb', line 131

def comment?(line)
  line.match(/^#/) ? true : false
end

#countObject



145
146
147
# File 'lib/disavow_tool/list.rb', line 145

def count
  @list.count
end

#delete_url(url, options = {verbose:true}) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/disavow_tool/list.rb', line 58

def delete_url(url, options={verbose:true})
  if @list.delete? url
    color_url = url.on_yellow
    puts delete_url_message(color_url).red if (@verbose && options[:verbose])
  else
    puts "Not removing #{url}. It wasn't on the list.".red if (@verbose && options[:verbose])
  end
end

#delete_urls_if_domains(domains) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/disavow_tool/list.rb', line 77

def delete_urls_if_domains(domains)
  domains = [domains] if domains.class != Set
  init_count = self.count
  domains.each do |domain|
    puts "Analysing #{domain}" if OPTIONS.hardcore_verbose
    self.each do |link|
      if link.match(/.+#{Regexp.escape(domain)}.+/)
        self.delete_url(link, verbose:true)
      end
    end
  end
  puts "Deleted #{init_count - self.count} elements.".blue if @verbose
end

#eachObject



135
136
137
138
139
# File 'lib/disavow_tool/list.rb', line 135

def each
  @list.each do |element|
    yield(element)
  end
end

#export(export_file = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/disavow_tool/list.rb', line 117

def export(export_file=nil)
  now = Time.now.strftime("%Y%m%d%H")
  file_name = self.class.to_s.underscore + "_" + now
  export_file = export_file || EXPORT_PATH + file_name
  FileUtils.mkdir_p File.dirname(export_file)
  file = File.new(export_file, "w")
  file.puts "# Exporting #{self.class.to_s.underscore} #{ now }"
  export_write(file)
  file.close
  if @verbose
    puts "Exported #{file_name} in #{EXPORT_PATH}".red
  end
end

#import(import_files) ⇒ Object



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

def import(import_files)
  import_files = [import_files] if import_files.class != Array
  import_files.each do |file|
    @list_files_imported << file
    puts "Importing file: #{file}"
    File.readlines(file).each do |line|
      line.chomp!
      if comment?(line) || line.blank?
        puts "cleaning comment or empty line: #{line}" if @verbose_hard
        next
      end
      unmodified_line = line
      clean_line!(line)
      puts import_message(unmodified_line).light_blue if @verbose_hard
      @list.add line
    end
  end
  self.try(:finished_import_hook)
end

#mass_delete_urls(urls_to_delete) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/disavow_tool/list.rb', line 67

def mass_delete_urls(urls_to_delete)
  puts mass_delete_message if @verbose_hard
  urls_to_delete.each do |link|
    color_link = link.on_yellow
    puts delete_url_message(color_link).red if @verbose
  end
  @list = @list - urls_to_delete
  puts "Deleted #{@list.count} elements.".blue if @verbose
end

#newObject



7
# File 'lib/disavow_tool/list.rb', line 7

def new; raise 'abstract!' end

#restoreObject



113
114
115
# File 'lib/disavow_tool/list.rb', line 113

def restore
   @list = @original_list.clone
end

#summary(list = nil, original_list = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/disavow_tool/list.rb', line 91

def summary(list=nil, original_list=nil)
  list = list || @list
  original_list = original_list || @original_list
  puts "Files imported:"
  puts @list_files_imported

  puts "#{message_sumary_imported} #{original_list.count}".blue
  puts "#{mensaje_sumary_before_export} #{list.count}".blue

  if @verbose
    total_imported = list - original_list
    puts "Total elements found after analysis: #{total_imported.count}".blue
    if( total_imported.count > 0) # It's a list with new links
      puts "Showing the #{total_imported.count} imported elements:".blue
      (total_imported).each_with_index do |line, i|
        puts "#{i+1}. #{line.light_blue}"
      end
    end
  end

end

#to_aObject



141
142
143
# File 'lib/disavow_tool/list.rb', line 141

def to_a
  @list.to_a
end