Class: EmailRepair::Mechanic::CommonDomainSwapRepair

Inherits:
CommonDomainRepair show all
Defined in:
lib/email_repair/mechanic.rb

Class Method Summary collapse

Methods inherited from CommonDomainRepair

common_domains

Class Method Details

.repair(email) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/email_repair/mechanic.rb', line 115

def self.repair(email)
  swapped_names.each do |swapped, real|
    suffix = common_domains[real]
    regex = /#{swapped}.#{suffix}$/
    email = email.sub(regex, "#{real}.#{suffix}") if email.match(regex)
  end
  email
end

.swap_name(str) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/email_repair/mechanic.rb', line 131

def self.swap_name(str)
  result = []
  (str.length - 1).times do |pos|
    beginning = str[0...pos]
    middle = "#{str[pos + 1]}#{str[pos]}"
    the_end = str[(pos + 2)..-1]
    result << "#{beginning}#{middle}#{the_end}"
  end
  result
end

.swapped_namesObject



124
125
126
127
128
129
# File 'lib/email_repair/mechanic.rb', line 124

def self.swapped_names
  @domain_keys ||= common_domains.keys
  @swapped_names ||= @domain_keys.each_with_object({}) do |name, result|
    swap_name(name).each { |swapped_name| result[swapped_name] = name }
  end
end