Last active
October 11, 2016 21:36
-
-
Save moisesnarvaez/7c312e7c8433ec5d0a1a0bfe35fc816b to your computer and use it in GitHub Desktop.
Finds a valid email giving a Fullname and a URL. Example: e = EmailFinder.new("Moises Narvaez", "www.koombea.com"); e.valid_email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EmailFinder | |
# TODO: para TopSpeed: Buscar si hay una "@" en el author, si es así... | |
# Buscar si en el texto de author hay algún texto extre paréntesis "()" | |
# si ese texto es un email, quitarlo (con los parentesis), sino cogerlo como nombre | |
def initialize(full_name, url) | |
@full_name = I18n.transliterate(full_name.downcase) | |
@url = url | |
@words = @full_name.split(' ') | |
end | |
def options | |
@options ||= begin | |
names.map do |name| | |
[ | |
"#{name[0]}.#{name[1]}@#{domain}", | |
"#{name[0]}#{name[1]}@#{domain}", | |
"#{name[0]}_#{name[1]}@#{domain}", | |
"#{name[0][0]}#{name[1]}@#{domain}", | |
"#{name[0]}@#{domain}", | |
"#{name[1]}@#{domain}" | |
] | |
end.flatten | |
end | |
end | |
def valid_email | |
@valid_email ||= begin | |
final_email = nil | |
options.each do |email| | |
begin | |
final_email = email and break if EmailVerifier.check(email) | |
sleep 1 | |
rescue | |
final_email = nil | |
end | |
end | |
final_email | |
end | |
end | |
private | |
def names | |
@names ||= begin | |
@words = @words.first(4) if @words.length > 4 | |
@words = [@words[0], @words[2]] if @words.length == 4 | |
@words = [@words[0], ''] if @words.length == 1 | |
return [@words] if @words.length == 2 | |
[ [@words[0], @words[1]], [@words[0], @words[2]] ] | |
end | |
end | |
def domain | |
@domain ||= begin | |
@url = "http://#{@url}" unless @url.start_with?('http') | |
uri = URI.parse(@url) | |
host = uri.host.downcase | |
host.start_with?('www.') ? host[4..-1] : host | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment