class Emailist

Constants

INVALID_END_WITH
INVALID_START_WITH
VALID_TLDS
VERSION

Public Class Methods

new(verify_hosts: false, verify_profiles: false) click to toggle source
# File lib/emailist.rb, line 23
def initialize(verify_hosts: false, verify_profiles: false)
        @verify_hosts = verify_hosts
        @verify_profiles = verify_profiles
end

Public Instance Methods

<<(email) click to toggle source

alias_method :<<, :push alias_method :add, :push

# File lib/emailist.rb, line 37
def <<(email)
        push(email)
end
_delete(email)
Alias for: delete
_include?(email)
Alias for: include?
_push(email)
Alias for: push
_unshift(email)
Alias for: unshift
add(email) click to toggle source
# File lib/emailist.rb, line 41
def add(email)
        push(email)
end
clean(email) click to toggle source
# File lib/emailist.rb, line 66
def clean(email)
        return if email.nil?

        # strip email first
        email = email.strip
        
        # convert >1 consecutive period to 1 period
        email.gsub!(/\.{2,}/, '.')

        # remove invalid start/end with
        INVALID_START_WITH.each do |with|
                email = email[(with.length)..email.length-1] if email.start_with?(with)
        end
        INVALID_END_WITH.each do |with|
                email = email[0..(email.length-with.length)] if email.end_with?(with)
        end

        # get local/domain parts separated
        local, domain = email.split('@')

        # remove leading/trailing periods
        local = local[1..local.length-1] if local[0] == '.'
        domain = domain[0..domain.length-2] if domain[domain.length-1] == '.'

        # remove invalid leading text
        new_local = ''
        local.chars.each do |char|
                if char =~ /[A-Z0-9!#\$%&'\*\+\-\/=\?\^_`\{\|\}~\.]/i
                        new_local += char
                else
                        new_local = ''
                end
        end
        local = new_local

        # remove invalid trailing text
        new_domain = ''
        domain.chars.each do |char|
                if char =~ /[A-Z0-9\-\.]/i
                        new_domain += char
                else
                        break
                end
        end
        domain = new_domain

        # remove invalid TLDs
        domain_split = domain.split('.').reverse
        if !VALID_TLDS.include?(domain_split.first)
                new_domain = []
                found_tld = false
                domain_split.each do |part|
                        if !found_tld && VALID_TLDS.include?(part.strip.upcase)
                                found_tld = true
                                new_domain.unshift(part)
                        elsif found_tld
                                new_domain.unshift(part)
                        end
                end
                raise Emailist::InvalidTLD if !found_tld
        else
                new_domain = domain_split.reverse
        end
        domain = new_domain.join('.')

        email = "#{local}@#{domain}".downcase

        #if @verify_profiles
        #      raise Emailist::ProfileNotFound if !profile_found?(email)
        #end
        if @verify_hosts
                raise Emailist::HostDead if !host_alive?(email)
        end

        email
end
delete(email) click to toggle source
# File lib/emailist.rb, line 52
def delete(email)
        _delete(clean(email))
        refresh!
end
Also aliased as: _delete
include?(email) click to toggle source
# File lib/emailist.rb, line 62
def include?(email)
        _include?(clean(email))
end
Also aliased as: _include?
push(email) click to toggle source
# File lib/emailist.rb, line 30
def push(email)
        _push(clean(email))
        refresh!
end
Also aliased as: _push
remove(email) click to toggle source
# File lib/emailist.rb, line 57
def remove(email)
        delete(email)
end
unshift(email) click to toggle source
# File lib/emailist.rb, line 46
def unshift(email)
        _unshift(clean(email))
        refresh!
end
Also aliased as: _unshift

Private Instance Methods

dead_hosts() click to toggle source
# File lib/emailist.rb, line 187
def dead_hosts
        @dead_hosts ||= []
end
host_alive?(email) click to toggle source
# File lib/emailist.rb, line 151
def host_alive?(email)
        domain = email.split('@').last

        return true if live_hosts.include?(domain)
        return false if dead_hosts.include?(domain)

        resolv = Resolv::DNS.new(
                nameserver: ['8.8.8.8'],
                search: [],
                ndots: 1
        )
        ip_addr = begin
                resolv.getaddress(domain).to_s
        rescue Resolv::ResolvError
        end

        dns_result = ip_addr ? true : false
        ping_ip_result = dns_result ? Net::Ping::External.new(ip_addr).ping : nil
        ping_domain_result = !ping_ip_result ? Net::Ping::External.new(domain).ping : nil

        result = (dns_result || ping_ip_result || ping_domain_result)
  if result
       live_hosts.push(domain)
       live_hosts.uniq!
  else
       dead_hosts.push(domain)
       dead_hosts.uniq!
  end

  result
end
live_hosts() click to toggle source
# File lib/emailist.rb, line 183
def live_hosts
        @live_hosts ||= []
end
profile_found?(email) click to toggle source
# File lib/emailist.rb, line 191
        def profile_found?(email)
                # HAVE TO TRY AND REVERSE ENGINEER RAPPORTIVE/LINKEDIN API
                # FOR POSSIBLE_EMAIL GEM TO WORK. THIS FUNCTIONALITY REQUIRES
                # THAT API, WHICH APPARENTLY HAS BEEN SHUT DOWN FOR THE PUBLIC.
=begin
                                if @verify_profiles
                                        begin
                                                response = [PossibleEmail.find_profile(email)].flatten
                                                if response.empty?
                                                        raise Emailist::CantVerifyProfile
                                                end
                                        rescue PossibleEmail::InvalidEmailFormat
                                                raise Emailist::InvalidEmailFormat
                                        end
                                end
=end
        end
refresh!() click to toggle source
# File lib/emailist.rb, line 145
def refresh!
        _delete(nil)
        uniq!
        return self
end