Class: Emailist

Inherits:
Array
  • Object
show all
Defined in:
lib/emailist.rb,
lib/emailist/errors.rb,
lib/emailist/version.rb

Defined Under Namespace

Classes: HostDead, InvalidEmailFormat, InvalidTLD, ProfileNotFound

Constant Summary collapse

VALID_TLDS =
File.read(
	File.expand_path(File.join(File.dirname(__FILE__), '..', 'data', 'valid_tlds.txt'))
).lines.to_a.map {|tld| tld.gsub(/[^A-Z\-]/, '') }
INVALID_START_WITH =
[
	'TO.', 'To.', 'E-mail', 'AS', 'TO:', 'To:'
]
INVALID_END_WITH =
[
	'.TO', '.To', 'E'
]
VERSION =
"0.0.16"

Instance Method Summary collapse

Constructor Details

#initialize(verify_hosts: false, verify_profiles: false) ⇒ Emailist

Returns a new instance of Emailist.



23
24
25
26
# File 'lib/emailist.rb', line 23

def initialize(verify_hosts: false, verify_profiles: false)
	@verify_hosts = verify_hosts
	@verify_profiles = verify_profiles
end

Instance Method Details

#<<(email) ⇒ Object

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



37
38
39
# File 'lib/emailist.rb', line 37

def <<(email)
	push(email)
end

#_deleteObject



51
# File 'lib/emailist.rb', line 51

alias_method :_delete, :delete

#_include?Object



61
# File 'lib/emailist.rb', line 61

alias_method :_include?, :include?

#_pushObject



29
# File 'lib/emailist.rb', line 29

alias_method :_push, :push

#_unshiftObject



45
# File 'lib/emailist.rb', line 45

alias_method :_unshift, :unshift

#add(email) ⇒ Object



41
42
43
# File 'lib/emailist.rb', line 41

def add(email)
	push(email)
end

#clean(email) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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) ⇒ Object



52
53
54
55
# File 'lib/emailist.rb', line 52

def delete(email)
	_delete(clean(email))
	refresh!
end

#include?(email) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/emailist.rb', line 62

def include?(email)
	_include?(clean(email))
end

#push(email) ⇒ Object



30
31
32
33
# File 'lib/emailist.rb', line 30

def push(email)
	_push(clean(email))
	refresh!
end

#remove(email) ⇒ Object



57
58
59
# File 'lib/emailist.rb', line 57

def remove(email)
	delete(email)
end

#unshift(email) ⇒ Object



46
47
48
49
# File 'lib/emailist.rb', line 46

def unshift(email)
	_unshift(clean(email))
	refresh!
end