email_assessor 0.8.3 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 78962333d6f1cd4adf5b69ecd450e347c067a69bd0867b591c37eddd89e1efd8
4
- data.tar.gz: 3c8addeb187281929edbeb69c61364218466a13699c04b16ad88ac928fccc7ab
3
+ metadata.gz: c3d58f2ccff5295b5ca3a6a340906e4bf434ebe786e3c60018595ef28872070d
4
+ data.tar.gz: cba2edd82f57fa711dbc2a8e0d8762913be44a9b3853cea44dd67dc412f26a8e
5
5
  SHA512:
6
- metadata.gz: 4c4af7254cbf9c82c9ac67bcda174f3a557ace08d52a8771af886ca633cc47ddaa6b7099085c7fbc343b0740777a426e2ce4595b125809cacac3123702c42d34
7
- data.tar.gz: d2a8afe9bcd700139d0842798d76d38f7dfb41ae3084f38ffcc29932d98ebb312ed6bd8540209ce77d2e1e1957ea8bbe595498ca8e2d4978f66aa4ddce5b4b3d
6
+ metadata.gz: e7e563e8586f5fbcc4979957b259d0af04ce624d907110edc23b605bec5d7dcffa099f1889a8b15b3dff4c62b023527130029f584d4e1819c935b84271c3002c
7
+ data.tar.gz: 6432e40e499237ba0c4a0a1a94b095a74bdaf702d3d84f1aeeeea24248da4b344bee645dc6cf6e5bc1ed8a976b28f80fcc3daf7e95322c1aa4852e45c270c4af
@@ -5,7 +5,7 @@ require "mail"
5
5
 
6
6
  module EmailAssessor
7
7
  class Address
8
- attr_accessor :address
8
+ attr_accessor :parsed
9
9
 
10
10
  PROHIBITED_DOMAIN_PREFIXES = [
11
11
  '.',
@@ -83,12 +83,16 @@ module EmailAssessor
83
83
  end
84
84
  end
85
85
 
86
- def initialize(address)
86
+ def initialize(raw_address)
87
87
  @parse_error = false
88
- @raw_address = address
88
+ @raw_address = raw_address
89
+ @address = nil
90
+ @valid = nil
91
+ @mx_servers = nil
92
+ @domain_tokens = nil
89
93
 
90
94
  begin
91
- @address = Mail::Address.new(address)
95
+ @address = Mail::Address.new(raw_address)
92
96
  rescue Mail::Field::ParseError
93
97
  @parse_error = true
94
98
  end
@@ -99,12 +103,12 @@ module EmailAssessor
99
103
  return false if @parse_error
100
104
 
101
105
  @valid =
102
- if address.domain && address.address == @raw_address
103
- domain = address.domain
106
+ if @address.domain && @address.address == @raw_address
107
+ domain = @address.domain
104
108
 
105
109
  domain.include?('.') &&
106
110
  !domain.match?(self.class.prohibited_domain_regex) &&
107
- !address.local.match?(self.class.prohibited_local_regex)
111
+ !@address.local.match?(self.class.prohibited_local_regex)
108
112
  else
109
113
  false
110
114
  end
@@ -127,7 +131,7 @@ module EmailAssessor
127
131
  end
128
132
 
129
133
  def domain_in_file?(filename)
130
- valid? && EmailAssessor.domain_in_file?(address.domain, filename)
134
+ valid? && EmailAssessor.any_token_in_file?(domain_tokens, filename)
131
135
  end
132
136
 
133
137
  def valid_mx?
@@ -145,10 +149,16 @@ module EmailAssessor
145
149
 
146
150
  def mx_servers
147
151
  @mx_servers ||= Resolv::DNS.open do |dns|
148
- mx_servers = dns.getresources(address.domain, Resolv::DNS::Resource::IN::MX)
152
+ mx_servers = dns.getresources(@address.domain, Resolv::DNS::Resource::IN::MX)
149
153
  (mx_servers.any? && mx_servers) ||
150
- dns.getresources(address.domain, Resolv::DNS::Resource::IN::A)
154
+ dns.getresources(@address.domain, Resolv::DNS::Resource::IN::A)
151
155
  end
152
156
  end
157
+
158
+ private
159
+
160
+ def domain_tokens
161
+ @domain_tokens ||= EmailAssessor.tokenize_domain(@address.domain)
162
+ end
153
163
  end
154
164
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module EmailAssessor
3
- VERSION = "0.8.3"
3
+ VERSION = "0.9.1"
4
4
  end
@@ -16,13 +16,26 @@ module EmailAssessor
16
16
  end
17
17
 
18
18
  def self.domain_in_file?(domain, file_name)
19
+ any_token_in_file?(tokenize_domain(domain), file_name)
20
+ end
21
+
22
+ def self.any_token_in_file?(domain_tokens, file_name)
19
23
  file_name ||= ""
20
- domain = domain.downcase
21
24
 
22
25
  File.foreach(file_name, chomp: true).any? do |line|
23
- # String#end_with? is used as a cheaper initial check but due to potential false positives
24
- # (hotmail.com is valid but tmail.com is not) regex is also necessary.
25
- domain.end_with?(line) && domain.match?(%r{\A(?:.*\.)?#{Regexp.escape(line)}\z}i)
26
+ domain_tokens.key?(line)
26
27
  end
27
28
  end
29
+
30
+ def self.tokenize_domain(domain)
31
+ parts = domain.downcase.split(".")
32
+ tokens = {}
33
+
34
+ parts.length.times do
35
+ tokens[parts.join(".")] = nil
36
+ parts.shift
37
+ end
38
+
39
+ tokens
40
+ end
28
41
  end