truemail 0.1.0

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
+ SHA256:
3
+ metadata.gz: bc4500ca7447975045d91b3fbc817b59abe4f358b64f067e3c879c4c359d7806
4
+ data.tar.gz: a61c85ee9c24a6d28aa297a70028a9a761a716068def74a34128bcbcf563a955
5
+ SHA512:
6
+ metadata.gz: 7e858716ea5bcffdc7992fcb749636fda1546a4f9af80b3f57824aeb520a881a26aa194549d2c03e5092e7b526712d928e838815846398d5bd0df5fecb7f6cdd
7
+ data.tar.gz: 34ae408c10bb8a3c4fdf47a7bdf5fdcf199c2e12ba8b5ee124deac6c1efc78dcf5a647bf509908a3c0247aface8fe39e3f074bec31f223718363c4d9a39ac2bd
@@ -0,0 +1,60 @@
1
+ defaults: &defaults
2
+ working_directory: ~/truemail
3
+ docker:
4
+ - image: circleci/ruby:2.5.0-node
5
+
6
+ references:
7
+ restore_bundle_cache: &restore_bundle_cache
8
+ restore_cache:
9
+ keys:
10
+ - truemail-{{ checksum "truemail.gemspec" }}
11
+
12
+ bundle_install: &bundle_install
13
+ run:
14
+ name: Installing gems
15
+ command: bundle install --path vendor/bundle
16
+
17
+ save_bundle_cache: &save_bundle_cache
18
+ save_cache:
19
+ key: truemail-{{ checksum "truemail.gemspec" }}
20
+ paths:
21
+ - vendor/bundle
22
+
23
+ version: 2
24
+ jobs:
25
+ linters:
26
+ <<: *defaults
27
+
28
+ steps:
29
+ - checkout
30
+
31
+ - <<: *restore_bundle_cache
32
+ - <<: *bundle_install
33
+ - <<: *save_bundle_cache
34
+
35
+ - run:
36
+ name: Running overcommit
37
+ command: |
38
+ bundle exec overcommit -s
39
+ SKIP=AuthorEmail,AuthorName bundle exec overcommit -r
40
+
41
+ tests:
42
+ <<: *defaults
43
+
44
+ steps:
45
+ - checkout
46
+
47
+ - <<: *restore_bundle_cache
48
+ - <<: *bundle_install
49
+ - <<: *save_bundle_cache
50
+
51
+ - run:
52
+ name: Running tests
53
+ command: bundle exec rspec
54
+
55
+ workflows:
56
+ version: 2
57
+ build:
58
+ jobs:
59
+ - linters
60
+ - tests
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ /.rspec_status
2
+ /pkg
3
+ /coverage/
data/.overcommit.yml ADDED
@@ -0,0 +1,29 @@
1
+ PreCommit:
2
+ AuthorEmail:
3
+ enabled: true
4
+ required: false
5
+
6
+ AuthorName:
7
+ enabled: false
8
+
9
+ BundleAudit:
10
+ enabled: true
11
+
12
+ Brakeman:
13
+ enabled: true
14
+ flags: ['--exit-on-warn', '--quiet', '--summary']
15
+
16
+ RuboCop:
17
+ enabled: true
18
+ flags: ['--format=emacs', '--force-exclusion', '--display-cop-names']
19
+
20
+ Reek:
21
+ enabled: true
22
+ flags: ['--force-exclusion']
23
+
24
+ PostCheckout:
25
+ ALL:
26
+ quiet: true
27
+
28
+ IndexTags:
29
+ enabled: true
data/.reek.yml ADDED
@@ -0,0 +1,31 @@
1
+ detectors:
2
+ IrresponsibleModule:
3
+ enabled: false
4
+
5
+ NestedIterators:
6
+ exclude:
7
+ - Truemail::ConfigurationHelper#configuration_block
8
+
9
+ TooManyStatements:
10
+ exclude:
11
+ - Truemail::Validate::Smtp::Request#run
12
+ - Truemail::Validate::Smtp#run
13
+
14
+ TooManyInstanceVariables:
15
+ exclude:
16
+ - Truemail::Configuration
17
+
18
+ UtilityFunction:
19
+ exclude:
20
+ - Truemail::Validate::Mx#mx_records
21
+ - Truemail::Validate::Smtp::Request#configuration
22
+ - Truemail::Validate::Smtp::Request#compose_from
23
+ - Truemail::Validator#select_validation_type
24
+
25
+ ControlParameter:
26
+ exclude:
27
+ - Truemail::GenerateEmailHelper#calculate_email_size
28
+
29
+ NilCheck:
30
+ exclude:
31
+ - Truemail::Validator#select_validation_type
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --format documentation
data/.rubocop.yml ADDED
@@ -0,0 +1,40 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+ StyleGuideCopsOnly: true
5
+ TargetRubyVersion: 2.5
6
+
7
+ Metrics/LineLength:
8
+ Max: 140
9
+
10
+ Documentation:
11
+ Enabled: false
12
+
13
+ FrozenStringLiteralComment:
14
+ Enabled: false
15
+
16
+ Style/ParallelAssignment:
17
+ Enabled: false
18
+
19
+ Style/DoubleNegation:
20
+ Enabled: false
21
+
22
+ require: rubocop-rspec
23
+
24
+ RSpec/ExampleLength:
25
+ Enabled: false
26
+
27
+ RSpec/NestedGroups:
28
+ Enabled: false
29
+
30
+ RSpec/MultipleExpectations:
31
+ Enabled: false
32
+
33
+ RSpec/MessageChain:
34
+ Enabled: false
35
+
36
+ RSpec/ContextWording:
37
+ Enabled: false
38
+
39
+ RSpec/AnyInstance:
40
+ Enabled: false
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ truemail
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.5.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at admin@bestweb.com.ua. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,117 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ truemail (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ axiom-types (0.1.1)
11
+ descendants_tracker (~> 0.0.4)
12
+ ice_nine (~> 0.11.0)
13
+ thread_safe (~> 0.3, >= 0.3.1)
14
+ bundler-audit (0.6.1)
15
+ bundler (>= 1.2.0, < 3)
16
+ thor (~> 0.18)
17
+ byebug (11.0.0)
18
+ childprocess (0.9.0)
19
+ ffi (~> 1.0, >= 1.0.11)
20
+ codeclimate-engine-rb (0.4.1)
21
+ virtus (~> 1.0)
22
+ coderay (1.1.2)
23
+ coercible (1.0.0)
24
+ descendants_tracker (~> 0.0.1)
25
+ descendants_tracker (0.0.4)
26
+ thread_safe (~> 0.3, >= 0.3.1)
27
+ diff-lcs (1.3)
28
+ docile (1.3.1)
29
+ equalizer (0.0.11)
30
+ ffaker (2.10.0)
31
+ ffi (1.10.0)
32
+ ice_nine (0.11.2)
33
+ iniparse (1.4.4)
34
+ jaro_winkler (1.5.2)
35
+ json (2.2.0)
36
+ kwalify (0.7.2)
37
+ method_source (0.9.2)
38
+ overcommit (0.46.0)
39
+ childprocess (~> 0.6, >= 0.6.3)
40
+ iniparse (~> 1.4)
41
+ parallel (1.14.0)
42
+ parser (2.6.0.0)
43
+ ast (~> 2.4.0)
44
+ powerpack (0.1.2)
45
+ pry (0.12.2)
46
+ coderay (~> 1.1.0)
47
+ method_source (~> 0.9.0)
48
+ pry-byebug (3.7.0)
49
+ byebug (~> 11.0)
50
+ pry (~> 0.10)
51
+ psych (3.1.0)
52
+ rainbow (3.0.0)
53
+ rake (12.3.2)
54
+ reek (5.3.1)
55
+ codeclimate-engine-rb (~> 0.4.0)
56
+ kwalify (~> 0.7.0)
57
+ parser (>= 2.5.0.0, < 2.7, != 2.5.1.1)
58
+ psych (~> 3.1.0)
59
+ rainbow (>= 2.0, < 4.0)
60
+ rspec (3.8.0)
61
+ rspec-core (~> 3.8.0)
62
+ rspec-expectations (~> 3.8.0)
63
+ rspec-mocks (~> 3.8.0)
64
+ rspec-core (3.8.0)
65
+ rspec-support (~> 3.8.0)
66
+ rspec-expectations (3.8.2)
67
+ diff-lcs (>= 1.2.0, < 2.0)
68
+ rspec-support (~> 3.8.0)
69
+ rspec-mocks (3.8.0)
70
+ diff-lcs (>= 1.2.0, < 2.0)
71
+ rspec-support (~> 3.8.0)
72
+ rspec-support (3.8.0)
73
+ rubocop (0.65.0)
74
+ jaro_winkler (~> 1.5.1)
75
+ parallel (~> 1.10)
76
+ parser (>= 2.5, != 2.5.1.1)
77
+ powerpack (~> 0.1)
78
+ psych (>= 3.1.0)
79
+ rainbow (>= 2.2.2, < 4.0)
80
+ ruby-progressbar (~> 1.7)
81
+ unicode-display_width (~> 1.4.0)
82
+ rubocop-rspec (1.32.0)
83
+ rubocop (>= 0.60.0)
84
+ ruby-progressbar (1.10.0)
85
+ simplecov (0.16.1)
86
+ docile (~> 1.1)
87
+ json (>= 1.8, < 3)
88
+ simplecov-html (~> 0.10.0)
89
+ simplecov-html (0.10.2)
90
+ thor (0.20.3)
91
+ thread_safe (0.3.6)
92
+ unicode-display_width (1.4.1)
93
+ virtus (1.0.5)
94
+ axiom-types (~> 0.1)
95
+ coercible (~> 1.0)
96
+ descendants_tracker (~> 0.0, >= 0.0.3)
97
+ equalizer (~> 0.0, >= 0.0.9)
98
+
99
+ PLATFORMS
100
+ ruby
101
+
102
+ DEPENDENCIES
103
+ bundler (~> 1.16)
104
+ bundler-audit
105
+ ffaker
106
+ overcommit
107
+ pry-byebug
108
+ rake
109
+ reek
110
+ rspec
111
+ rubocop
112
+ rubocop-rspec
113
+ simplecov
114
+ truemail!
115
+
116
+ BUNDLED WITH
117
+ 1.16.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Vladislav Trotsenko
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,231 @@
1
+ # Truemail
2
+
3
+ The main idea of this gem is to validate emails by regex pattern, presence of domain mx-records, and real existence of email account on a current email server.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'truemail'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install truemail
20
+
21
+ ## Email Validation Methods
22
+
23
+ Email validation is a tricky thing to do properly. There are a number of different ways to validate an email address. All checking mechanisms conform to best practices, and provide confident validation.
24
+
25
+ **Syntax Checking**: This checks the email addresses via regex pattern.
26
+
27
+ **Mail Server Existence Check**: This checks the availability of the email address domain using DNS MX records.
28
+
29
+ **Mail Existence Check**: This checks if the email address really exists and can receive email via SMTP connections and email-sending emulation techniques.
30
+
31
+ ## Usage
32
+
33
+ ### Configuration features
34
+
35
+ #### Set configuration
36
+
37
+ To have access for Truemail.configuration and gem features you must configure it first. Just do it with block like in example below.
38
+
39
+ ```ruby
40
+ Truemail.configure do |config|
41
+ # Required parameter. Should be an existing email on behalf of which verification will be performed
42
+ config.verifier_email = 'verifier@example.com'
43
+
44
+ # Optional parameter. Should be an existing domain on behalf of which verification will be performed.
45
+ # By default verifier domain based on verifier email
46
+ config.verifier_domain = 'somedomain.com'
47
+
48
+ # Optional parameter. You can override default regex pattern
49
+ config.email_pattern = /regex_pattern/
50
+
51
+ # Optional parameter. By default connection timeout is equal to 2 ms
52
+ config.connection_timeout = 1
53
+
54
+ # Optional parameter. By default smtp response timeout is equal to 2 ms
55
+ config.response_timeout = 1
56
+
57
+ # Optional parameter. You can to predefined which validation use for some domains
58
+ # Available validation types: :regex, :mx, :smtp
59
+ # This configuration will be used over current or default validation type parameter
60
+ # All of validations for 'somedomain.com' will be processed with mx validation only
61
+ config.validation_type_for = { 'somedomain.com' => :mx }
62
+ end
63
+ ```
64
+
65
+ #### Read configuration
66
+
67
+ After successful configuration you can read current Truemail configuration instance anywhere in your application.
68
+
69
+ ```ruby
70
+ Truemail.configuration
71
+
72
+ => #<Truemail::Configuration:0x000055590cb17b40
73
+ @connection_timeout=1,
74
+ @email_pattern=/regex_pattern/,
75
+ @response_timeout=1,
76
+ @validation_type_by_domain={},
77
+ @verifier_domain="somedomain.com",
78
+ @verifier_email="verifier@example.com">
79
+ ```
80
+
81
+ #### Update configuration
82
+
83
+ ```ruby
84
+ Truemail.configuration.connection_timeout = 3
85
+ => 3
86
+ Truemail.configuration.response_timeout = 4
87
+ => 4
88
+
89
+ Truemail.configuration
90
+ => #<Truemail::Configuration:0x000055590cb17b40
91
+ @connection_timeout=3,
92
+ @email_pattern=/regex_pattern/,
93
+ @response_timeout=4,
94
+ @validation_type_by_domain={},
95
+ @verifier_domain="somedomain.com",
96
+ @verifier_email="verifier@example.com">
97
+ ```
98
+
99
+ #### Reset configuration
100
+
101
+ Also you can reset Truemail configuration.
102
+
103
+ ```ruby
104
+ Truemail.reset_configuration!
105
+ => nil
106
+ Truemail.configuration
107
+ => nil
108
+ ```
109
+
110
+ ### Validation features
111
+
112
+ #### Regex validation
113
+
114
+ Validation with regex pattern is first validation level. You can redefine regex pattern in gem configuration.
115
+
116
+ Examples of using:
117
+
118
+ 1. With default regex pattern
119
+
120
+ ```ruby
121
+ Truemail.configure do |config|
122
+ config.verifier_email = 'verifier@example.com'
123
+ end
124
+
125
+ Truemail.validate('email@example.com', with: :regex)
126
+
127
+ => #<Truemail::Validator:0x000055590cc9bdb8
128
+ @result=<struct Truemail::Validator::Result success=true, email="email@example.com", domain=nil, mail_servers=[], errors={}, smtp_debug=nil>,
129
+ @validation_type=:regex>
130
+ ```
131
+
132
+ 2. With custom regex pattern
133
+
134
+ ```ruby
135
+ Truemail.configure do |config|
136
+ config.verifier_email = 'verifier@example.com'
137
+ config.config.email_pattern = /regex_pattern/
138
+ end
139
+
140
+ Truemail.validate('email@example.com', with: :regex)
141
+
142
+ => #<Truemail::Validator:0x000055590ca8b3e8
143
+ @result=<struct Truemail::Validator::Result success=true, email="email@example.com", domain=nil, mail_servers=[], errors={}, smtp_debug=nil>, @validation_type=:regex>
144
+ ```
145
+
146
+ #### MX validation
147
+
148
+ Validation by MX records is second validation level. It use Regex validation before run. When regex validation has completed successfully then starts itself.
149
+
150
+ Example of using:
151
+
152
+ ```ruby
153
+ Truemail.configure do |config|
154
+ config.verifier_email = 'verifier@example.com'
155
+ end
156
+
157
+ Truemail.validate('email@example.com', with: :mx)
158
+
159
+ => #<Truemail::Validator:0x000055590c9c1c50
160
+ @result=<struct Truemail::Validator::Result success=true, email="email@example.com", domain="example.com", mail_servers=["mx1.example.com", "mx2.example.com"], errors={}, smtp_debug=nil>,
161
+ @validation_type=:mx>
162
+ ```
163
+
164
+ #### SMTP validation
165
+
166
+ SMTP validation is a final, third validation level. It try to check real existence of email account on a current email server. This validation runs chain of previous validations, and if they complete successfully runs itself.
167
+
168
+ ```code
169
+ [Regex validation] -> [MX validation] -> [SMTP validation]
170
+ ```
171
+
172
+ By default you don't need pass with-parameter to use it. Example of using:
173
+
174
+ ```ruby
175
+ Truemail.configure do |config|
176
+ config.verifier_email = 'verifier@example.com'
177
+ end
178
+
179
+ Truemail.validate('email@example.com')
180
+
181
+ # Successful SMTP validation
182
+ => #<Truemail::Validator:0x000055590c4dc118
183
+ @result=<struct Truemail::Validator::Result success=true, email="email@example.com", domain="example.com", mail_servers=["mx1.example.com", "mx2.example.com"], errors={}, smtp_debug=nil>,
184
+ @validation_type=:smtp>
185
+
186
+ # SMTP validation failed
187
+ => #<Truemail::Validator:0x000055590cc88150
188
+ @result=
189
+ #<struct Truemail::Validator::Result
190
+ success=false,
191
+ email="email@example.com",
192
+ domain="example.com",
193
+ mail_servers=["mx1.example.com", "mx2.example.com"],
194
+ errors={:smtp=>"smtp error"},
195
+ smtp_debug=
196
+ [#<struct Truemail::Validate::Smtp::Request
197
+ host="mx1.example.com",
198
+ email="email@example.com",
199
+ response=
200
+ #<struct Truemail::Validate::Smtp::Response
201
+ port_opened=true,
202
+ connection=true,
203
+ helo=#<Net::SMTP::Response:0x000055590cc74678 @status="250", @string="250 mx1.example.com\n">,
204
+ mailfrom=#<Net::SMTP::Response:0x000055590cc74308 @status="250", @string="250 2.1.0 <verifier@example.com> ok\n">,
205
+ rcptto=false,
206
+ errors={:rcptto=>"550 5.7.1 No such user!\n"}>>]>,
207
+ @validation_type=:smtp>
208
+ ```
209
+
210
+ ## ToDo
211
+
212
+ 1. Gem compatibility with Ruby 2.3
213
+ 2. Fail validations logger
214
+ 3. The ability to use a proxy
215
+
216
+ ## Contributing
217
+
218
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rubygarage/truemail. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
219
+
220
+ ## License
221
+
222
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
223
+
224
+ ## Code of Conduct
225
+
226
+ Everyone interacting in the Truemail project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/truemail/blob/master/CODE_OF_CONDUCT.md).
227
+
228
+ ---
229
+ <a href="https://rubygarage.org/"><img src="https://rubygarage.s3.amazonaws.com/assets/assets/rg_color_logo_horizontal-919afc51a81d2e40cb6a0b43ee832e3fcd49669d06785156d2d16fd0d799f89e.png" alt="RubyGarage Logo" width="415" height="128"></a>
230
+
231
+ RubyGarage is a leading software development and consulting company in Eastern Europe. Our main expertise includes Ruby and Ruby on Rails, but we successfully employ other technologies to deliver the best results to our clients. [Check out our portfolio](https://rubygarage.org/portfolio) for even more exciting works!
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'truemail'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/truemail.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'truemail/version'
2
+ require 'truemail/core'
3
+ require 'truemail/configuration'
4
+ require 'truemail/validator'
5
+
6
+ module Truemail
7
+ INCOMPLETE_CONFIG = 'verifier_email is required parameter'.freeze
8
+ NOT_CONFIGURED = 'use Truemail.configure before'.freeze
9
+
10
+ class << self
11
+ def configuration
12
+ @configuration ||= begin
13
+ return unless block_given?
14
+ configuration = Truemail::Configuration.new
15
+ yield(configuration)
16
+ raise ConfigurationError, INCOMPLETE_CONFIG unless configuration.complete?
17
+ configuration
18
+ end
19
+ end
20
+
21
+ def configure(&block)
22
+ configuration(&block)
23
+ end
24
+
25
+ def reset_configuration!
26
+ @configuration = nil
27
+ end
28
+
29
+ def validate(email, **options)
30
+ raise ConfigurationError, NOT_CONFIGURED unless configuration
31
+ Truemail::Validator.new(email, **options).run
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,72 @@
1
+ module Truemail
2
+ class Configuration
3
+ DEFAULT_CONNECTION_TIMEOUT = 2
4
+ DEFAULT_RESPONSE_TIMEOUT = 2
5
+
6
+ attr_reader :email_pattern,
7
+ :verifier_email,
8
+ :verifier_domain,
9
+ :connection_timeout,
10
+ :response_timeout,
11
+ :validation_type_by_domain
12
+
13
+ def initialize
14
+ @email_pattern = Truemail::RegexConstant::REGEX_EMAIL_PATTERN
15
+ @connection_timeout = DEFAULT_CONNECTION_TIMEOUT
16
+ @response_timeout = DEFAULT_RESPONSE_TIMEOUT
17
+ @validation_type_by_domain = {}
18
+ end
19
+
20
+ def email_pattern=(regex_pattern)
21
+ raise Truemail::ArgumentError.new(regex_pattern, Regexp) unless regex_pattern.is_a?(Regexp)
22
+ @email_pattern = regex_pattern
23
+ end
24
+
25
+ def verifier_email=(email)
26
+ validate_arguments(email, __method__)
27
+ @verifier_email = email
28
+ default_verifier_domain
29
+ end
30
+
31
+ def verifier_domain=(domain)
32
+ validate_arguments(domain, __method__)
33
+ @verifier_domain = domain
34
+ end
35
+
36
+ %i[connection_timeout response_timeout].each do |method|
37
+ define_method("#{method}=") do |argument|
38
+ raise ArgumentError.new(argument, __method__) unless argument.is_a?(Integer) && argument.positive?
39
+ instance_variable_set(:"@#{method}", argument)
40
+ end
41
+ end
42
+
43
+ def validation_type_for=(settings)
44
+ validate_validation_type(settings)
45
+ validation_type_by_domain.merge!(settings)
46
+ end
47
+
48
+ def complete?
49
+ !!verifier_email
50
+ end
51
+
52
+ private
53
+
54
+ def validate_arguments(argument, method)
55
+ constant = Truemail::RegexConstant.const_get("regex_#{method[/\A.+_(.+)\=\z/, 1]}_pattern".upcase)
56
+ raise Truemail::ArgumentError.new(argument, method) unless constant.match?(argument.to_s)
57
+ end
58
+
59
+ def validate_validation_type(settings)
60
+ settings.each do |domain, validation_type|
61
+ raise Truemail::ArgumentError.new(domain, 'domain') unless
62
+ Truemail::RegexConstant::REGEX_DOMAIN_PATTERN.match?(domain.to_s)
63
+ raise Truemail::ArgumentError.new(validation_type, 'validation type') unless
64
+ Truemail::Validator::VALIDATION_TYPES.include?(validation_type)
65
+ end
66
+ end
67
+
68
+ def default_verifier_domain
69
+ self.verifier_domain ||= verifier_email[Truemail::RegexConstant::REGEX_EMAIL_PATTERN, 3]
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,24 @@
1
+ module Truemail
2
+ class ConfigurationError < StandardError; end
3
+
4
+ class ArgumentError < StandardError
5
+ def initialize(current_param, class_name)
6
+ super("#{current_param} is not a valid #{class_name}")
7
+ end
8
+ end
9
+
10
+ module RegexConstant
11
+ REGEX_DOMAIN = /[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,7}/
12
+ REGEX_EMAIL_PATTERN = /(?=\A.{6,255}\z)(\A([\w|\-|\.]+)@(#{REGEX_DOMAIN})\z)/
13
+ REGEX_DOMAIN_PATTERN = /(?=\A.{4,255}\z)(\A#{REGEX_DOMAIN}\z)/
14
+ end
15
+
16
+ module Validate
17
+ require 'truemail/validate/base'
18
+ require 'truemail/validate/regex'
19
+ require 'truemail/validate/mx'
20
+ require 'truemail/validate/smtp'
21
+ require 'truemail/validate/smtp/response'
22
+ require 'truemail/validate/smtp/request'
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ module Truemail
2
+ module Validate
3
+ class Base
4
+ attr_reader :result
5
+
6
+ def initialize(result)
7
+ @result = result
8
+ end
9
+
10
+ def self.check(result)
11
+ new(result).run
12
+ end
13
+
14
+ private
15
+
16
+ def success(condition)
17
+ result.success = condition
18
+ end
19
+
20
+ def add_error(message)
21
+ result.errors[self.class.name.split('::').last.downcase.to_sym] = message
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ module Truemail
2
+ module Validate
3
+ class Mx < Truemail::Validate::Base
4
+ require 'resolv'
5
+
6
+ ERROR = 'mx records not found'.freeze
7
+
8
+ def run
9
+ return false unless Truemail::Validate::Regex.check(result)
10
+ result.domain = result.email[Truemail::RegexConstant::REGEX_EMAIL_PATTERN, 3]
11
+ return true if success(!result.mail_servers.push(*mx_records(result.domain)).empty?)
12
+ add_error(Truemail::Validate::Mx::ERROR)
13
+ false
14
+ end
15
+
16
+ private
17
+
18
+ def mx_records(domain)
19
+ mx_records = Resolv::DNS.open { |dns| dns.getresources(domain, Resolv::DNS::Resource::IN::MX) }
20
+ mx_records.sort_by(&:preference).map { |mx_record| mx_record.exchange.to_s }
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ module Truemail
2
+ module Validate
3
+ class Regex < Truemail::Validate::Base
4
+ ERROR = 'email does not match the regular expression'.freeze
5
+
6
+ def run
7
+ return true if success(Truemail.configuration.email_pattern.match?(result.email))
8
+ add_error(Truemail::Validate::Regex::ERROR)
9
+ false
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,45 @@
1
+ module Truemail
2
+ module Validate
3
+ class Smtp < Truemail::Validate::Base
4
+ ERROR = 'smtp error'.freeze
5
+
6
+ attr_reader :smtp_results
7
+
8
+ def initialize(result)
9
+ super(result)
10
+ @smtp_results = []
11
+ end
12
+
13
+ def run
14
+ return false unless Truemail::Validate::Mx.check(result)
15
+ establish_smtp_connection
16
+ return true if success(success_response?)
17
+ result.smtp_debug = smtp_results
18
+ add_error(Truemail::Validate::Smtp::ERROR)
19
+ false
20
+ end
21
+
22
+ private
23
+
24
+ def request
25
+ smtp_results.last
26
+ end
27
+
28
+ def rcptto_error
29
+ request.response.errors[:rcptto]
30
+ end
31
+
32
+ def establish_smtp_connection
33
+ result.mail_servers.each do |mail_server|
34
+ smtp_results << Truemail::Validate::Smtp::Request.new(host: mail_server, email: result.email)
35
+ next unless request.check_port
36
+ request.run || rcptto_error ? break : next
37
+ end
38
+ end
39
+
40
+ def success_response?
41
+ !!smtp_results.map(&:response).find(&:rcptto)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,81 @@
1
+ module Truemail
2
+ module Validate
3
+ class Smtp
4
+ SMTP_PORT = 25
5
+ CONNECTION_TIMEOUT_ERROR = 'connection timed out'.freeze
6
+ RESPONSE_TIMEOUT_ERROR = 'server response timeout'.freeze
7
+
8
+ Request = Struct.new(:host, :email, :response, keyword_init: true) do
9
+ require 'net/smtp'
10
+
11
+ def initialize(response: Truemail::Validate::Smtp::Response.new, **args)
12
+ super
13
+ end
14
+
15
+ def check_port
16
+ Timeout.timeout(configuration.connection_timeout) do
17
+ return response.port_opened =
18
+ !TCPSocket.new(host, Truemail::Validate::Smtp::SMTP_PORT).close
19
+ end
20
+ rescue Timeout::Error
21
+ response.port_opened = false
22
+ end
23
+
24
+ def run
25
+ session.start do |smtp_request|
26
+ response.connection = true
27
+ smtp_handshakes(smtp_request, response)
28
+ end
29
+ rescue => error
30
+ assign_error(attribute: :connection, message: compose_from(error))
31
+ end
32
+
33
+ private
34
+
35
+ def configuration
36
+ Truemail.configuration.freeze
37
+ end
38
+
39
+ def session
40
+ Net::SMTP.new(host, Truemail::Validate::Smtp::SMTP_PORT).tap do |settings|
41
+ settings.open_timeout = configuration.connection_timeout
42
+ settings.read_timeout = configuration.response_timeout
43
+ end
44
+ end
45
+
46
+ def compose_from(error)
47
+ case error.class.name
48
+ when 'Net::OpenTimeout' then Truemail::Validate::Smtp::CONNECTION_TIMEOUT_ERROR
49
+ when 'Net::ReadTimeout' then Truemail::Validate::Smtp::RESPONSE_TIMEOUT_ERROR
50
+ else error.message
51
+ end
52
+ end
53
+
54
+ def assign_error(attribute:, message:)
55
+ response.errors[attribute] = message
56
+ response.public_send(:"#{attribute}=", false)
57
+ end
58
+
59
+ def session_data
60
+ {
61
+ helo: configuration.verifier_domain,
62
+ mailfrom: configuration.verifier_email,
63
+ rcptto: email
64
+ }
65
+ end
66
+
67
+ def smtp_resolver(smtp_request, method, value)
68
+ smtp_request.public_send(method, value)
69
+ rescue => error
70
+ assign_error(attribute: method, message: compose_from(error))
71
+ end
72
+
73
+ def smtp_handshakes(smtp_request, smtp_response)
74
+ !!session_data.each do |method, value|
75
+ break unless smtp_response.public_send(:"#{method}=", smtp_resolver(smtp_request, method, value))
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,13 @@
1
+ module Truemail
2
+ module Validate
3
+ class Smtp
4
+ RESPONSE_ATTRS = %i[port_opened connection helo mailfrom rcptto errors]
5
+
6
+ Response = Struct.new(*RESPONSE_ATTRS, keyword_init: true) do
7
+ def initialize(errors: {}, **args)
8
+ super
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ module Truemail
2
+ class Validator
3
+ RESULT_ATTRS = %i[success email domain mail_servers errors smtp_debug].freeze
4
+ VALIDATION_TYPES = %i[regex mx smtp].freeze
5
+
6
+ Result = Struct.new(*RESULT_ATTRS, keyword_init: true) do
7
+ def initialize(errors: {}, mail_servers: [], **args)
8
+ super
9
+ end
10
+ alias_method :valid?, :success
11
+ end
12
+
13
+ attr_reader :validation_type, :result
14
+
15
+ def initialize(email, with: :smtp)
16
+ raise ArgumentError.new(with, :argument) unless VALIDATION_TYPES.include?(with)
17
+ @validation_type, @result = select_validation_type(email, with), Result.new(email: email)
18
+ end
19
+
20
+ def run
21
+ Truemail::Validate.const_get(validation_type.capitalize).check(result)
22
+ self
23
+ end
24
+
25
+ private
26
+
27
+ def select_validation_type(email, current_validation_type)
28
+ domain = email[Truemail::RegexConstant::REGEX_EMAIL_PATTERN, 3]
29
+ Truemail.configuration&.validation_type_by_domain[domain] || current_validation_type
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Truemail
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/truemail.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'truemail/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'truemail'
7
+ spec.version = Truemail::VERSION
8
+ spec.authors = ['Vladislav Trotsenko']
9
+ spec.email = ['admin@bestweb.com.ua']
10
+
11
+ spec.summary = %(truemail)
12
+ spec.description = %(Configurable plain ruby email validator. Validate email by regexp, mx records and real email existence)
13
+ spec.homepage = 'https://github.com/rubygarage/truemail'
14
+ spec.license = 'MIT'
15
+
16
+ spec.required_ruby_version = '>= 2.5.0'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.16'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'overcommit'
27
+ spec.add_development_dependency 'reek'
28
+ spec.add_development_dependency 'rubocop'
29
+ spec.add_development_dependency 'rubocop-rspec'
30
+ spec.add_development_dependency 'ffaker'
31
+ spec.add_development_dependency 'simplecov'
32
+ spec.add_development_dependency 'bundler-audit'
33
+ spec.add_development_dependency 'pry-byebug'
34
+ end
metadata ADDED
@@ -0,0 +1,227 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: truemail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vladislav Trotsenko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: overcommit
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: reek
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: ffaker
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: bundler-audit
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: pry-byebug
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: Configurable plain ruby email validator. Validate email by regexp, mx
168
+ records and real email existence
169
+ email:
170
+ - admin@bestweb.com.ua
171
+ executables: []
172
+ extensions: []
173
+ extra_rdoc_files: []
174
+ files:
175
+ - ".circleci/config.yml"
176
+ - ".gitignore"
177
+ - ".overcommit.yml"
178
+ - ".reek.yml"
179
+ - ".rspec"
180
+ - ".rubocop.yml"
181
+ - ".ruby-gemset"
182
+ - ".ruby-version"
183
+ - CODE_OF_CONDUCT.md
184
+ - Gemfile
185
+ - Gemfile.lock
186
+ - LICENSE.txt
187
+ - README.md
188
+ - Rakefile
189
+ - bin/console
190
+ - bin/setup
191
+ - lib/truemail.rb
192
+ - lib/truemail/configuration.rb
193
+ - lib/truemail/core.rb
194
+ - lib/truemail/validate/base.rb
195
+ - lib/truemail/validate/mx.rb
196
+ - lib/truemail/validate/regex.rb
197
+ - lib/truemail/validate/smtp.rb
198
+ - lib/truemail/validate/smtp/request.rb
199
+ - lib/truemail/validate/smtp/response.rb
200
+ - lib/truemail/validator.rb
201
+ - lib/truemail/version.rb
202
+ - truemail.gemspec
203
+ homepage: https://github.com/rubygarage/truemail
204
+ licenses:
205
+ - MIT
206
+ metadata: {}
207
+ post_install_message:
208
+ rdoc_options: []
209
+ require_paths:
210
+ - lib
211
+ required_ruby_version: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: 2.5.0
216
+ required_rubygems_version: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - ">="
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
221
+ requirements: []
222
+ rubyforge_project:
223
+ rubygems_version: 2.7.8
224
+ signing_key:
225
+ specification_version: 4
226
+ summary: truemail
227
+ test_files: []