email_repair 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2478611e83b838e0f167bdc0e787c011d603ae52
4
+ data.tar.gz: 706c5df6c7a189c2a8286ff7cb9c9b573194ea2d
5
+ SHA512:
6
+ metadata.gz: 1b989bbefba3f3ea48c011b47bda1336791e1519178df3f76cd19c41175484e986002a269948d6f184ac88b26ae1d6b92718cc8768f59aae91aa80a6a9f53109
7
+ data.tar.gz: b7ba04723f437e65108f05c1e0a33b143f39a4277b3fc7447327df5a8fcfda8092733acb42e7b18aab604068339f96131b192df6b4e3dd2dd48d222e58c05566
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Emics, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Email Repair
2
+
3
+ [![Build Status](https://travis-ci.org/ChalkSchools/email-repair.svg?branch=master)](https://travis-ci.org/ChalkSchools/email-repair)
4
+
5
+ TODO: Write a gem description
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'email_repair'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install email_repair
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/email_repair/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
@@ -0,0 +1,3 @@
1
+ require 'email_repair/constants'
2
+ require 'email_repair/mechanic'
3
+ require 'email_repair/version'
@@ -0,0 +1,12 @@
1
+ module EmailRepair
2
+ class Constants
3
+ def self.email_regex
4
+ local_part_regex = "[#{email_local_part_valid_chars}]+"
5
+ /#{local_part_regex}@(?:[a-z0-9\-]+\.)+(?:museum|travel|[a-z]{2,4})/
6
+ end
7
+
8
+ def self.email_local_part_valid_chars
9
+ 'a-z0-9_\.%\+\-\''
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,120 @@
1
+ module EmailRepair
2
+ class Mechanic
3
+ def repairs
4
+ [
5
+ CommonMistakeRepair,
6
+ CommonDomainSuffixRepair,
7
+ CommonDomainPeriodAdder,
8
+ CommonDomainAtAdder,
9
+ CommonDomainSwapRepair,
10
+ EmailRegexRepair,
11
+ ]
12
+ end
13
+
14
+ def repair(email)
15
+ return unless email
16
+
17
+ repairs.each { |repair| email = repair.repair(email) }
18
+
19
+ email
20
+ end
21
+
22
+ class CommonMistakeRepair
23
+ def self.repair(email)
24
+ email.downcase
25
+ .gsub(/\s/, '')
26
+ .sub(/@+/, '@')
27
+ .sub(/\.c0m$/, '.com')
28
+ .sub(/,[a-z]{2,4}$/) { |m| m.sub(',', '.') }
29
+ end
30
+ end
31
+
32
+ class EmailRegexRepair
33
+ def self.repair(email)
34
+ match = email.match(/(#{EmailRepair::Constants.email_regex})/)
35
+ match && match.captures.first
36
+ end
37
+ end
38
+
39
+ class CommonDomainRepair
40
+ def self.repair(*)
41
+ fail 'not implemented'
42
+ end
43
+
44
+ def self.common_domains
45
+ {
46
+ 'gmail' => 'com',
47
+ 'yahoo' => 'com',
48
+ 'hotmail' => 'com',
49
+ 'att' => 'net',
50
+ 'chalkschools' => 'com',
51
+ 'sbcglobal' => 'net',
52
+ 'aol' => 'com',
53
+ 'earthlink' => 'net',
54
+ }
55
+ end
56
+ end
57
+
58
+ class CommonDomainSuffixRepair < CommonDomainRepair
59
+ def self.repair(email)
60
+ common_domains.each do |name, suffix|
61
+ email = "#{email}.#{suffix}" if email.match(/#{name}$/)
62
+ end
63
+ email
64
+ end
65
+ end
66
+
67
+ class CommonDomainPeriodAdder < CommonDomainRepair
68
+ def self.repair(email)
69
+ common_domains.each do |name, suffix|
70
+ regex = /#{name}#{suffix}$/
71
+ email = email.sub(regex, "#{name}.#{suffix}") if email.match(regex)
72
+ end
73
+ email
74
+ end
75
+ end
76
+
77
+ class CommonDomainAtAdder < CommonDomainRepair
78
+ def self.repair(email)
79
+ common_domains.each do |name, suffix|
80
+ punc_regex = /[.#-]#{name}.#{suffix}$/
81
+ if email.match(punc_regex)
82
+ email = email.sub(punc_regex, "@#{name}.#{suffix}")
83
+ elsif email.match(/[^@]#{name}.#{suffix}$/)
84
+ email = email.sub(/#{name}.#{suffix}$/, "@#{name}.#{suffix}")
85
+ end
86
+ end
87
+ email
88
+ end
89
+ end
90
+
91
+ class CommonDomainSwapRepair < CommonDomainRepair
92
+ def self.repair(email)
93
+ swapped_names.each do |swapped, real|
94
+ suffix = common_domains[real]
95
+ regex = /#{swapped}.#{suffix}$/
96
+ email = email.sub(regex, "#{real}.#{suffix}") if email.match(regex)
97
+ end
98
+ email
99
+ end
100
+
101
+ def self.swapped_names
102
+ @domain_keys ||= common_domains.keys
103
+ @swapped_names ||= @domain_keys.each_with_object({}) do |name, result|
104
+ swap_name(name).each { |swapped_name| result[swapped_name] = name }
105
+ end
106
+ end
107
+
108
+ def self.swap_name(str)
109
+ result = []
110
+ (str.length - 1).times do |pos|
111
+ beginning = str[0...pos]
112
+ middle = "#{str[pos + 1]}#{str[pos]}"
113
+ the_end = str[(pos + 2)..-1]
114
+ result << "#{beginning}#{middle}#{the_end}"
115
+ end
116
+ result
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,3 @@
1
+ module EmailRepair
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ module EmailRepair
4
+ describe Mechanic, '#repair' do
5
+
6
+ let(:mechanic) { Mechanic.new }
7
+
8
+ it 'returns clean emails as is' do
9
+ # Rant about apostrophe in email
10
+ # http://www.stuffaboutcode.com/2013/02/validating-email-address-bloody.html
11
+ good_emails = %w(
12
+ b@b.com
13
+ lobatifricha@gmail.com
14
+ mrspicy+whocares@whatevs.com
15
+ your.mom@the.museum
16
+ martin.o'hanlon@mycompany.com
17
+ )
18
+
19
+ good_emails.each do |good_email|
20
+ expect(mechanic.repair(good_email)).to eq good_email
21
+ end
22
+ end
23
+
24
+ it 'returns nil for nil email' do
25
+ expect(mechanic.repair(nil)).to be_nil
26
+ end
27
+
28
+ it 'returns nil for strings that have no email' do
29
+ ['', ' ', 'NOT AN EMAIL', 'b at b dot com'].each do |not_email|
30
+ expect(mechanic.repair(not_email)).to be_nil
31
+ end
32
+ end
33
+
34
+ it 'returns the email with the spaces stripped out' do
35
+ space_mails = {
36
+ 'somebody@gmail.com ' => 'somebody@gmail.com',
37
+ 'email withspace@blah.com' => 'emailwithspace@blah.com',
38
+ 'emailwithspace@blah .com' => 'emailwithspace@blah.com',
39
+ }
40
+
41
+ space_mails.each do |in_mail, out_mail|
42
+ expect(mechanic.repair(in_mail)).to eq out_mail
43
+ end
44
+ end
45
+
46
+ it 'adds missing .com' do
47
+ no_com_mails = %w(blah@gmail bloo@yahoo blee@hotmail)
48
+ no_com_mails.each do |no_com_mail|
49
+ expect(mechanic.repair(no_com_mail)).to eq "#{no_com_mail}.com"
50
+ end
51
+ end
52
+
53
+ it 'trims extra @ signs' do
54
+ expect(mechanic.repair('b@@@b.com')).to eq 'b@b.com'
55
+ end
56
+
57
+ it 'changes c0m to com' do
58
+ expect(mechanic.repair('b0b@b0b.c0m')).to eq 'b0b@b0b.com'
59
+ end
60
+
61
+ it 'fixes a missing .' do
62
+ expect(mechanic.repair('who@gmailcom')).to eq 'who@gmail.com'
63
+ expect(mechanic.repair('what@hotmailcom')).to eq 'what@hotmail.com'
64
+ end
65
+
66
+ it 'fixes letter swap' do
67
+ expect(mechanic.repair('bloo@yhaoo.com')).to eq 'bloo@yahoo.com'
68
+ end
69
+
70
+ it 'adds a missing @ for common domains' do
71
+ dirty_emails = {
72
+ 'blahgmailcom' => 'blah@gmail.com',
73
+ 'blooearthlink.net' => 'bloo@earthlink.net',
74
+ 'gooberssbcglobal.net' => 'goobers@sbcglobal.net',
75
+ 'booger800yahoo.com' => 'booger800@yahoo.com',
76
+ 'butt2630att.net' => 'butt2630@att.net',
77
+ 'byjove28024aol.com' => 'byjove28024@aol.com',
78
+ }
79
+
80
+ dirty_emails.each do |in_mail, out_mail|
81
+ expect(mechanic.repair(in_mail)).to eq out_mail
82
+ end
83
+ end
84
+
85
+ it 'swaps a # for an @ for common domains' do
86
+ expect(mechanic.repair('pound#yahoo.com')).to eq 'pound@yahoo.com'
87
+ end
88
+
89
+ it 'swaps a - for an @ for common domains' do
90
+ dirty_emails = {
91
+ 'chou.sa+aea-gmail.com' => 'chou.sa+aea@gmail.com',
92
+ 'sarah+aea-chalkschools.com' => 'sarah+aea@chalkschools.com',
93
+ }
94
+ dirty_emails.each do |in_mail, out_mail|
95
+ expect(mechanic.repair(in_mail)).to eq out_mail
96
+ end
97
+ end
98
+
99
+ it 'replaces , with .' do
100
+ expect(mechanic.repair('b@b,com')).to eq 'b@b.com'
101
+ end
102
+
103
+ it 'grabs an email out of the string' do
104
+ dirty_emails = {
105
+ 'Awesome,Test Driver,aperson@gmail.com' => 'aperson@gmail.com',
106
+ 'one@email.com, another@email.com' => 'one@email.com',
107
+ 'one@email.com,another@email.com' => 'one@email.com',
108
+ 'one@email.com;another@email.com' => 'one@email.com',
109
+ '<one@email.com>' => 'one@email.com',
110
+ }
111
+
112
+ dirty_emails.each do |in_mail, out_mail|
113
+ expect(mechanic.repair(in_mail)).to eq out_mail
114
+ end
115
+ end
116
+
117
+ it 'downcases emails' do
118
+ dirty_emails = {
119
+ 'ONE@email.com, another@email.com' => 'one@email.com',
120
+ 'email WITHSPACE@blah.com' => 'emailwithspace@blah.com',
121
+ 'lobatifricha@GMAIL.com' => 'lobatifricha@gmail.com',
122
+ }
123
+
124
+ dirty_emails.each do |in_mail, out_mail|
125
+ expect(mechanic.repair(in_mail)).to eq out_mail
126
+ end
127
+ end
128
+
129
+ end
130
+ end
@@ -0,0 +1,6 @@
1
+ require_relative '../lib/email_repair'
2
+
3
+ RSpec.configure do |config|
4
+ config.filter_run focus: true
5
+ config.run_all_when_everything_filtered = true
6
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: email_repair
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Holman Gao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - holman@golmansax.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - LICENSE
21
+ - README.md
22
+ - lib/email_repair.rb
23
+ - lib/email_repair/constants.rb
24
+ - lib/email_repair/mechanic.rb
25
+ - lib/email_repair/version.rb
26
+ - spec/lib/email_repair/mechanic_spec.rb
27
+ - spec/spec_helper.rb
28
+ homepage: https://github.com/ChalkSchools/email-repair
29
+ licenses:
30
+ - MIT
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.2.2
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Library to fix invalid emails
52
+ test_files:
53
+ - spec/lib/email_repair/mechanic_spec.rb
54
+ - spec/spec_helper.rb