email_assessor 0.1 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f9f5a08d4182ed68bdc232439f615339e0f5158
4
- data.tar.gz: 76309795d2dad14078647902e21191fc90af9934
3
+ metadata.gz: c2e86b0145f0a1eadce4c8df87b7c1172974e284
4
+ data.tar.gz: b226714891f60a08facb4c93f4185ad723d3d9be
5
5
  SHA512:
6
- metadata.gz: 01529449a629cec7b20880b109574d9cc90d4ea1d8a3bd82908a09a82d66a7c5fbf98510246167ab9eab1cdb40e869873b276e78ea41606386b92a2bf8af87b5
7
- data.tar.gz: 683e9ac09cd34379d909558672886f044d5277bda2573e9c0278b1233993731195d555278072b54d914e5f64ac64cf4d003c3613272116da511d573b20d220ac
6
+ metadata.gz: f0e0af20e1ec8ae109b88120c97cdb754a453771d6eecb90e88bff09897ffd2f1354ee4564353c4034923ad6ddc6b66e3db39d6d0d4f04f13df6fab689bcf93c
7
+ data.tar.gz: 459aa3cb6b2234535c5470930f08549064fab9eb079cf590b287cb38df26027a32df16b16771f4e73c30ea5e327da5b1302a08962e611c524912cd144db52061
@@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
24
24
 
25
25
  spec.add_development_dependency "bundler", "~> 1.3"
26
26
  spec.add_development_dependency "rake"
27
- spec.add_development_dependency "rspec", "~> 2.14.1"
27
+ spec.add_development_dependency "rspec", "~> 3.4"
28
28
  spec.add_runtime_dependency "mail", "~> 2.5"
29
29
  spec.add_runtime_dependency "activemodel", ">= 3.2"
30
30
  end
@@ -1,26 +1,40 @@
1
1
  require "email_assessor/email_validator"
2
2
 
3
3
  module EmailAssessor
4
- DISPOSABLE_DOMAINS_FILE = File.expand_path("../../vendor/disposable_domains.txt", __FILE__)
5
- BLACKLISTED_DOMAINS_FILE = File.expand_path("../../vendor/blacklisted_domains.txt", __FILE__)
6
-
7
4
  def self.domain_is_disposable?(domain)
8
- domain_in_file?(domain, DISPOSABLE_DOMAINS_FILE)
5
+ domain_in_file?(domain, configuration.disposable_domains_file_name)
9
6
  end
10
7
 
11
8
  def self.domain_is_blacklisted?(domain)
12
- domain_in_file?(domain, BLACKLISTED_DOMAINS_FILE)
9
+ domain_in_file?(domain, configuration.blacklisted_domains_file_name)
10
+ end
11
+
12
+ def self.configuration
13
+ @@configuration ||= Configuration.new
14
+ end
15
+
16
+ def self.configure
17
+ yield(configuration) if block_given?
18
+ end
19
+
20
+ class Configuration
21
+ attr_accessor :disposable_domains_file_name, :blacklisted_domains_file_name
22
+
23
+ def initialize
24
+ @disposable_domains_file_name = File.expand_path("../../vendor/disposable_domains.txt", __FILE__)
25
+ @blacklisted_domains_file_name = File.expand_path("vendor/blacklisted_domains.txt")
26
+ end
13
27
  end
14
28
 
15
29
  protected
16
30
 
17
- def self.domain_in_file?(domain, filename)
18
- return false unless File.exists?(filename)
31
+ def self.domain_in_file?(domain, file_name)
32
+ return false unless file_name.present? && File.exists?(file_name)
19
33
 
20
34
  domain = domain.downcase
21
35
  domain_matched = false
22
36
 
23
- File.open(filename).each do |line|
37
+ File.open(file_name).each do |line|
24
38
  if domain.end_with?(line.chomp)
25
39
  domain_matched = true
26
40
  break
@@ -1,3 +1,3 @@
1
1
  module EmailAssessor
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
@@ -14,8 +14,6 @@ resp = Net::HTTP.get_response(URI.parse(url))
14
14
 
15
15
  remote_domains = JSON.parse(resp.body).flatten - whitelisted_domains
16
16
 
17
- puts "New domains found: #{(remote_domains - existing_domains).join(', ')}"
18
-
19
17
  result_domains = (existing_domains + remote_domains).map { |domain| domain.strip.downcase }.uniq.sort
20
18
 
21
19
  File.open("vendor/disposable_domains.txt", "w") { |f| f.write result_domains.join("\n") }
@@ -17,86 +17,128 @@ class TestUserDisallowBlacklisted < TestModel
17
17
  end
18
18
 
19
19
  describe EmailAssessor do
20
+ let(:plain_user) { TestUser.new(email: "") }
21
+ let(:disposable_user) { TestUserDisallowDisposable.new(email: "foo@gmail.com") }
22
+ let(:blacklist_user) { TestUserDisallowBlacklisted.new(email: "foo@gmail.com") }
23
+ let(:mx_user) { TestUserMX.new(email: "foo@gmail.com") }
24
+
25
+ let(:blacklisted_domains_file_name) { described_class.configuration.blacklisted_domains_file_name }
26
+ let(:blacklisted_domain) { File.open(blacklisted_domains_file_name, &:readline) }
27
+
28
+ let(:disposable_domains_file_name) { described_class.configuration.disposable_domains_file_name }
29
+ let(:disposable_domain) { File.open(disposable_domains_file_name, &:readline) }
30
+
20
31
  describe "basic validation" do
21
- subject(:user) { TestUser.new(email: "") }
32
+ subject(:user) { plain_user }
22
33
 
23
34
  it "should be valid when email is empty" do
24
- user.valid?.should be_true
35
+ is_expected.to be_valid
25
36
  end
26
37
 
27
38
  it "should not be valid when domain is missing" do
28
- user = TestUser.new(email: "foo")
29
- user.valid?.should be_false
39
+ user.email = "foo"
40
+ is_expected.to be_invalid
30
41
  end
31
42
 
32
43
  it "should be invalid when email is malformed" do
33
- user = TestUser.new(email: "foo@bar")
34
- user.valid?.should be_false
44
+ user.email = "foo@bar"
45
+ is_expected.to be_invalid
35
46
  end
36
47
 
37
48
  it "should be invalid when email contains a trailing symbol" do
38
- user = TestUser.new(email: "foo@bar.com/")
39
- user.valid?.should be_false
49
+ user.email = "foo@bar.com/"
50
+ is_expected.to be_invalid
40
51
  end
41
52
 
42
53
  it "should be invalid if Mail::AddressListsParser raises exception" do
43
- user = TestUser.new(email: "foo@gmail.com")
44
- Mail::Address.stub(:new).and_raise(Mail::Field::ParseError.new(nil, nil, nil))
45
- user.valid?.should be_false
54
+ allow(Mail::Address).to receive(:new).and_raise(Mail::Field::ParseError.new(nil, nil, nil))
55
+ user.email = "foo@gmail.com"
56
+ is_expected.to be_invalid
46
57
  end
47
58
 
48
59
  it "shouldn't be valid if the domain constains consecutives dots" do
49
- user = TestUser.new(email: "foo@bar..com")
50
- user.valid?.should be_false
60
+ user.email = "foo@bar..com"
61
+ is_expected.to be_invalid
51
62
  end
52
63
  end
53
64
 
54
65
  describe "disposable domains" do
55
- let(:disposable_domain) { disposable_domain = File.open(described_class::DISPOSABLE_DOMAINS_FILE, &:readline) }
66
+ subject(:user) { disposable_user }
56
67
 
57
68
  it "should be valid when email is not in the list of disposable domains" do
58
- user = TestUserDisallowDisposable.new(email: "foo@gmail.com")
59
- user.valid?.should be_true
69
+ is_expected.to be_valid
60
70
  end
61
71
 
62
72
  it "should be invalid when email is in the list of disposable domains" do
63
- user = TestUserDisallowDisposable.new(email: "foo@#{disposable_domain}")
64
- user.valid?.should be_false
73
+ user.email = "foo@#{disposable_domain}"
74
+ is_expected.to be_invalid
65
75
  end
66
76
 
67
77
  it "should be invalid when email is in the list of disposable domains regardless of subdomain" do
68
- user = TestUserDisallowDisposable.new(email: "foo@abc123.#{disposable_domain}")
69
- user.valid?.should be_false
78
+ user.email = "foo@abc123.#{disposable_domain}"
79
+ is_expected.to be_invalid
70
80
  end
71
81
  end
72
82
 
73
83
  describe "blacklisted domains" do
74
- let(:blacklisted_domain) { File.open(described_class::BLACKLISTED_DOMAINS_FILE, &:readline) }
84
+ subject(:user) { blacklist_user }
85
+
75
86
  it "should be valid when email domain is not in the blacklist" do
76
- user = TestUserDisallowBlacklisted.new(email: "foo@gmail.com")
77
- user.valid?.should be_true
87
+ is_expected.to be_valid
78
88
  end
79
89
 
80
90
  it "should be invalid when email domain is in the blacklist" do
81
- user = TestUserDisallowBlacklisted.new(email: "foo@#{blacklisted_domain}")
82
- user.valid?.should be_false
91
+ user.email = "foo@#{blacklisted_domain}"
92
+ is_expected.to be_invalid
83
93
  end
84
94
 
85
95
  it "should be invalid when email domain is in the blacklist regardless of subdomain" do
86
- user = TestUserDisallowBlacklisted.new(email: "foo@abc123.#{blacklisted_domain}")
87
- user.valid?.should be_false
96
+ user.email = "foo@abc123.#{blacklisted_domain}"
97
+ is_expected.to be_invalid
88
98
  end
89
99
  end
90
100
 
91
101
  describe "mx lookup" do
102
+ subject(:user) { mx_user }
103
+
92
104
  it "should be valid if mx records are found" do
93
- user = TestUserMX.new(email: "foo@gmail.com")
94
- user.valid?.should be_true
105
+ is_expected.to be_valid
95
106
  end
96
107
 
97
108
  it "should be invalid if no mx records are found" do
98
- user = TestUserMX.new(email: "foo@subdomain.gmail.com")
99
- user.valid?.should be_false
109
+ user.email = "foo@subdomain.gmail.com"
110
+ is_expected.to be_invalid
111
+ end
112
+ end
113
+
114
+ describe "configuration" do
115
+ subject(:configuration) { described_class.configuration }
116
+ let(:disposable_domains_file_name) { File.expand_path("../fixtures/disposable_domains.txt", __FILE__) }
117
+ let(:blacklisted_domains_file_name) { File.expand_path("../fixtures/blacklisted_domains.txt", __FILE__) }
118
+
119
+ before do
120
+ described_class.configure do |config|
121
+ config.disposable_domains_file_name = disposable_domains_file_name
122
+ config.blacklisted_domains_file_name = blacklisted_domains_file_name
123
+ end
124
+ end
125
+
126
+ it "sets the disposable domains file" do
127
+ is_expected.to have_attributes disposable_domains_file_name: disposable_domains_file_name
128
+ end
129
+
130
+ it "sets the blacklisted domains file" do
131
+ is_expected.to have_attributes blacklisted_domains_file_name: blacklisted_domains_file_name
132
+ end
133
+
134
+ it "uses the configured disposable domains file" do
135
+ disposable_user.email = disposable_domain
136
+ expect(disposable_user).to be_invalid
137
+ end
138
+
139
+ it "uses the configured blacklisted domains file" do
140
+ blacklist_user.email = blacklisted_domain
141
+ expect(blacklist_user).to be_invalid
100
142
  end
101
143
  end
102
144
  end
@@ -0,0 +1 @@
1
+ rspec-blacklisted.txt
@@ -0,0 +1 @@
1
+ rspec-disposable.txt
@@ -8,6 +8,10 @@ class TestModel
8
8
  @attributes = attributes
9
9
  end
10
10
 
11
+ def email=(email)
12
+ @attributes[:email] = email
13
+ end
14
+
11
15
  def read_attribute_for_validation(key)
12
16
  @attributes[key]
13
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: email_assessor
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Wolfe Millard
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 2.14.1
47
+ version: '3.4'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 2.14.1
54
+ version: '3.4'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: mail
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -103,6 +103,8 @@ files:
103
103
  - lib/email_assessor/version.rb
104
104
  - pull_mailchecker_emails.rb
105
105
  - spec/email_assessor_spec.rb
106
+ - spec/fixtures/blacklisted_domains.txt
107
+ - spec/fixtures/disposable_domains.txt
106
108
  - spec/spec_helper.rb
107
109
  - vendor/blacklisted_domains.txt
108
110
  - vendor/disposable_domains.txt
@@ -132,4 +134,6 @@ specification_version: 4
132
134
  summary: Advanced ActiveModel email validation
133
135
  test_files:
134
136
  - spec/email_assessor_spec.rb
137
+ - spec/fixtures/blacklisted_domains.txt
138
+ - spec/fixtures/disposable_domains.txt
135
139
  - spec/spec_helper.rb