forked from the4dpatrick/possible-email
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpossible_email.rb
56 lines (40 loc) · 1.47 KB
/
possible_email.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'possible_email/error'
require 'possible_email/version'
require 'possible_email/permutator'
require 'possible_email/rapportive_requester'
require 'httpi'
require 'json'
HTTPI.log = false
EMAIL_REGEX = /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i
DOMAIN_REGEX = /^([\w\-]+\.)+([\w]{2,})$/
NAME_REGEX = /^\b[a-zA-Z]+\b$/
module PossibleEmail
class InvalidNameFormat < ArgumentError; end
class InvalidEmailFormat < ArgumentError; end
module_function
def search(first_name, last_name, *domain)
assign_instance_variables first_name, last_name, domain
fail InvalidNameFormat, "Name arguments were not formatted correctly #{[@first_name, @last_name, *@domain].inspect}" unless valid_names?
permutations = Permutator.call(@first_name, @last_name, @domain)
RapportiveRequester.request(permutations)
end
def find_profile(*emails)
@emails = emails.flatten
fail InvalidEmailFormat, "Email arguments were not formatted correctly #{@emails.inspect}" if invalid_emails?
RapportiveRequester.request(@emails)
end
private_class_method
def assign_instance_variables(first_name, last_name, domain)
@first_name = first_name
@last_name = last_name
@domain = domain.flatten
end
def invalid_emails?
@emails.any? { |email| email !~ EMAIL_REGEX }
end
def valid_names?
valid_domain = @domain.all? { |d| d =~ DOMAIN_REGEX }
valid_names = [@first_name, @last_name].all? { |n| n =~ NAME_REGEX }
valid_domain && valid_names
end
end