Skip to content

Instantly share code, notes, and snippets.

@HeroicEric
Created October 12, 2011 15:11
Show Gist options
  • Select an option

  • Save HeroicEric/1281461 to your computer and use it in GitHub Desktop.

Select an option

Save HeroicEric/1281461 to your computer and use it in GitHub Desktop.
require 'csv'
require 'ruby_regex'
class Cleaner
def initialize
@rejects = get_reject_csvs
@role_addresses = CSV.read('./role_addresses.csv')
end
# Removes rows from CSV containing problematic emails
#
# path - Path to a CSV that has email addresses in the last column
#
# Returns an Array of cleaned rows from the CSV
def clean(path)
name = File.basename(path, '.csv')
original = CSV.read(path)
clean = []
num_rejected = 0
original.each do |row|
if check_for_rejects(row) == false
clean << row
else
num_rejected += 1
end
end
puts "Number of Rejected Emails Found: " + num_rejected.to_s
clean = remove_invalid_emails(clean)
clean = remove_duplicate_emails(clean)
clean = remove_role_addresses(clean)
output_to_csv(clean, name)
end
# Loads in all of the CSVs with emails that should be removed from the CSV.
# The last column of the CSVs should be emails.
#
# Returns array of the paths to files in rejects folder.
def get_reject_csvs
rejects = []
Dir.glob('./rejects/*.csv') do |csv|
rejects << CSV.read(csv)
end
rejects
end
# Checks if the email in a row matches any of the reject emails
#
# row - An array of strings. The last column of the row should be an email.
#
# Returns Boolean value for if the row contains a rejected email.
def check_for_rejects(row)
is_reject = false
@rejects.each do |reject_list|
reject_list.each do |email|
if row.last.downcase.include?(email[0])
is_reject = true
puts "Rejected: " + email[0]
end
end
end
is_reject
end
# Removes any rows with duplicate email addresses.
#
# rows - An Array of the rows of a CSV
#
# Returns the same Array without rows containing duplicate emails.
def remove_duplicate_emails(rows)
clean_rows = []
emails = []
num_of_duplicates = 0
rows.each do |row|
if emails.include?(row.last.downcase.strip)
num_of_duplicates += 1
puts "Removed: " + row.last
else
emails << row.last.downcase.strip
clean_rows << row
end
end
puts "Duplicates Removed: " + num_of_duplicates.to_s
clean_rows
end
# Removes any rows containing role email addresses
#
# rows - An Array of the rows of a CSV
#
# Returns the same Array without rows containing role addresses
def remove_role_addresses(rows)
clean_rows = []
num_of_role_addresses = 0
rows.each do |row|
if role_address?(row.last) == false
clean_rows << row
else
num_of_role_addresses += 1
end
end
puts "Role Addresses Found: " + num_of_role_addresses.to_s
clean_rows
end
# Determines whether or not an email is a "role address"
#
# address - Email address
#
# Returns a Boolean
def role_address?(address)
is_role_address = false
@role_addresses.each do |role_address|
regex = /#{role_address[0]}@/
if regex.match(address).nil? == false
# the email matches the role_address
is_role_address = true
end
end
is_role_address
end
# Determines whether or not the email format is real
#
# email - and Email in String form
#
# Returns a Boolean
def is_valid_email?(email)
if RubyRegex::Email.match(email).nil?
return false
else
return true
end
end
# Removes any rows containing invalid email addresses
#
# rows - An Array of the rows of a CSV
#
# Returns the same Array without rows containing invalid addresses
def remove_invalid_emails(rows)
valid_rows = []
rows.each do |row|
if is_valid_email?(row.last)
valid_rows << row
end
end
valid_rows
end
# Outputs the clean results to a CSV
#
# file_path - The path to the original csv
# clean - A cleaned version of the original CSV as an Array of rows
#
# Returns nothing.
def output_to_csv(clean, name)
CSV.open('./clean-lists/' + name + '-clean.csv', "wb") do |csv|
clean.each do |row|
csv << row
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment