Class: NameToEmail
- Inherits:
-
Object
- Object
- NameToEmail
- Defined in:
- lib/nametoemail.rb
Instance Method Summary collapse
-
#emaillist ⇒ Object
Get list of just emails.
-
#firstinitiallastname(firstname, lastname, domain) ⇒ Object
Emails of the form firstinitiallastname@domain.
-
#firstlastdot(firstname, lastname, domain) ⇒ Object
Emails of form firstname.lastname@domain.
-
#firstunderscorelast(firstname, lastname, domain) ⇒ Object
Emails of the form firstname_lastname@domain.
-
#genemails ⇒ Object
Get emails of all formats.
-
#initialize(input, namefield, presetdomain, chosendomain, urlfield, formattype) ⇒ NameToEmail
constructor
A new instance of NameToEmail.
-
#jsonwithemails ⇒ Object
Get JSON with emails appended.
-
#justlast(lastname, domain) ⇒ Object
Emails of the form lastname@domain.
-
#lastnamefirstinitial(firstname, lastname, domain) ⇒ Object
Emails of the form lastnamefirstinitial.
-
#parsename(name) ⇒ Object
Gets first and last name and removes certain characters.
-
#parseurl(url) ⇒ Object
Parse url to get just the domain.
-
#tryallformats(name, domain) ⇒ Object
Get emails in all formats.
-
#tryoneformat(name, domain, format) ⇒ Object
Gets all emails of one format.
Constructor Details
#initialize(input, namefield, presetdomain, chosendomain, urlfield, formattype) ⇒ NameToEmail
Returns a new instance of NameToEmail.
4 5 6 7 8 9 10 11 12 |
# File 'lib/nametoemail.rb', line 4 def initialize(input, namefield, presetdomain, chosendomain, urlfield, formattype) @input = JSON.parse(input) @namefield = namefield @urlfield = urlfield @presetdomain = presetdomain @chosendomain = chosendomain @emaillist = Array.new @formattype = formattype end |
Instance Method Details
#emaillist ⇒ Object
Get list of just emails
126 127 128 |
# File 'lib/nametoemail.rb', line 126 def emaillist return JSON.pretty_generate(@emaillist) end |
#firstinitiallastname(firstname, lastname, domain) ⇒ Object
Emails of the form firstinitiallastname@domain
98 99 100 |
# File 'lib/nametoemail.rb', line 98 def firstinitiallastname(firstname, lastname, domain) return firstname[0] + lastname + "@" + domain end |
#firstlastdot(firstname, lastname, domain) ⇒ Object
Emails of form firstname.lastname@domain
88 89 90 |
# File 'lib/nametoemail.rb', line 88 def firstlastdot(firstname, lastname, domain) return firstname + "." + lastname + "@" + domain end |
#firstunderscorelast(firstname, lastname, domain) ⇒ Object
Emails of the form firstname_lastname@domain
103 104 105 |
# File 'lib/nametoemail.rb', line 103 def firstunderscorelast(firstname, lastname, domain) return firstname + "_" + lastname + "@" + domain end |
#genemails ⇒ Object
Get emails of all formats
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 |
# File 'lib/nametoemail.rb', line 15 def genemails nametrack = Array.new @input.each do |i| # Parse domain if @presetdomain == false domain = parseurl(i[@urlfield]) else domain = @chosendomain end if i[@namefield] && domain name = parsename(i[@namefield]) # Check if combination already exists if name && domain namedomain = name[0] + name[1] + domain if !(nametrack.include? namedomain) nametrack.push(namedomain) if @formattype == "all" itememails = tryallformats(name, domain) else itememails = tryoneformat(name, domain, @formattype) end i["emails"] = itememails end end end end end |
#jsonwithemails ⇒ Object
Get JSON with emails appended
131 132 133 |
# File 'lib/nametoemail.rb', line 131 def jsonwithemails return JSON.pretty_generate(@input) end |
#justlast(lastname, domain) ⇒ Object
Emails of the form lastname@domain
83 84 85 |
# File 'lib/nametoemail.rb', line 83 def justlast(lastname, domain) return lastname + "@" + domain end |
#lastnamefirstinitial(firstname, lastname, domain) ⇒ Object
Emails of the form lastnamefirstinitial
93 94 95 |
# File 'lib/nametoemail.rb', line 93 def lastnamefirstinitial(firstname, lastname, domain) return lastname + firstname[0] + "@" + domain end |
#parsename(name) ⇒ Object
Gets first and last name and removes certain characters
117 118 119 120 121 122 123 |
# File 'lib/nametoemail.rb', line 117 def parsename(name) splitn = name.split(" ") first = splitn.first.gsub("'", "").gsub("-", "").gsub(".", "") last = splitn.last.gsub("'", "").gsub("-", "").gsub(".", "") narray = [first.downcase, last.downcase] return narray end |
#parseurl(url) ⇒ Object
Parse url to get just the domain
108 109 110 111 112 113 114 |
# File 'lib/nametoemail.rb', line 108 def parseurl(url) parsed = url.gsub("http://", "").gsub("www.", "") split = parsed.split("/") if split[0] return split[0] end end |
#tryallformats(name, domain) ⇒ Object
Get emails in all formats
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/nametoemail.rb', line 49 def tryallformats(name, domain) itememails = Array.new # Generate emails itememails.push(firstlastdot(name[0], name[1], domain)) itememails.push(firstinitiallastname(name[0], name[1], domain)) itememails.push(firstunderscorelast(name[0], name[1], domain)) itememails.push(lastnamefirstinitial(name[0], name[1], domain)) itememails.push(justlast(name[1], domain)) itememails.each do |e| @emaillist.push(e) end return itememails end |
#tryoneformat(name, domain, format) ⇒ Object
Gets all emails of one format
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/nametoemail.rb', line 66 def tryoneformat(name, domain, format) email = "" case format when "firstlastdot" then email = firstlastdot(name[0], name[1], domain) when "firstinitiallastname" then email = firstinitiallastname(name[0], name[1], domain) when "firstunderscorelast" then email = firstunderscorelast(name[0], name[1], domain) when "lastnamefirstinitial" then email = lastnamefirstinitial(name[0], name[1], domain) when "justlast" then email = justlast(name[1], domain) else email = nil end @emaillist.push(email) return email end |