rails_email_checker 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: 8e99f8a15584efbbfe72bdd987c0b536ca53873dfab58d581c79c37107161754
4
+ data.tar.gz: 03b26be23b4bb692781c43396526df1d4ecce7cf450c0da24c90b7ad60db2286
5
+ SHA512:
6
+ metadata.gz: 8adb0084265a8b35e5c9d581f42c5691014690e9de2f8f343f164a06e26b172e75f39185abc6b3326e59c0f446030643b7d4306a18585824b53df97a578704fc
7
+ data.tar.gz: 0a3b2e0618d0bf19c99972986fe7f54a82101aebd58f53a0860011d5549acdbd313ef4b44f68e9ffef60e5d20fa31e979ed2e5bead04246f935a23d4f3c0aa0c
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # IDE
14
+ /.idea
15
+ .idea/workspace.xml
16
+ .rakeTasks
17
+ .generators
18
+
19
+ # Gem
20
+ *.gem
21
+
22
+ # Logs
23
+ spec/dummy/log/*.log
24
+ spec/dummy/tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,19 @@
1
+ Documentation:
2
+ Enabled: false
3
+ Metrics/LineLength:
4
+ IgnoredPatterns: ['(\A|\s)#']
5
+ Max: 100
6
+ Layout/EmptyLineAfterGuardClause:
7
+ Enabled: false
8
+ Layout/EmptyLinesAroundBlockBody:
9
+ Enabled: false
10
+ Style/GuardClause:
11
+ Enabled: false
12
+ Metrics/MethodLength:
13
+ Max: 35
14
+ Metrics/AbcSize:
15
+ Enabled: false
16
+ Metrics/CyclomaticComplexity:
17
+ Enabled: false
18
+ Metrics/PerceivedComplexity:
19
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in rails_email_checker.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,73 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rails_email_checker (0.1.0)
5
+ activemodel (~> 4.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.2.11.1)
11
+ activesupport (= 4.2.11.1)
12
+ builder (~> 3.1)
13
+ activesupport (4.2.11.1)
14
+ i18n (~> 0.7)
15
+ minitest (~> 5.1)
16
+ thread_safe (~> 0.3, >= 0.3.4)
17
+ tzinfo (~> 1.1)
18
+ builder (3.2.4)
19
+ concurrent-ruby (1.1.6)
20
+ coveralls (0.8.23)
21
+ json (>= 1.8, < 3)
22
+ simplecov (~> 0.16.1)
23
+ term-ansicolor (~> 1.3)
24
+ thor (>= 0.19.4, < 2.0)
25
+ tins (~> 1.6)
26
+ diff-lcs (1.3)
27
+ docile (1.3.2)
28
+ i18n (0.9.5)
29
+ concurrent-ruby (~> 1.0)
30
+ json (2.3.0)
31
+ minitest (5.14.0)
32
+ rake (10.5.0)
33
+ rspec (3.9.0)
34
+ rspec-core (~> 3.9.0)
35
+ rspec-expectations (~> 3.9.0)
36
+ rspec-mocks (~> 3.9.0)
37
+ rspec-core (3.9.1)
38
+ rspec-support (~> 3.9.1)
39
+ rspec-expectations (3.9.0)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.9.0)
42
+ rspec-mocks (3.9.1)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.9.0)
45
+ rspec-support (3.9.2)
46
+ simplecov (0.16.1)
47
+ docile (~> 1.1)
48
+ json (>= 1.8, < 3)
49
+ simplecov-html (~> 0.10.0)
50
+ simplecov-html (0.10.2)
51
+ sync (0.5.0)
52
+ term-ansicolor (1.7.1)
53
+ tins (~> 1.0)
54
+ thor (1.0.1)
55
+ thread_safe (0.3.6)
56
+ tins (1.24.1)
57
+ sync
58
+ tzinfo (1.2.6)
59
+ thread_safe (~> 0.1)
60
+
61
+ PLATFORMS
62
+ ruby
63
+
64
+ DEPENDENCIES
65
+ bundler (~> 2.0)
66
+ coveralls (~> 0.8)
67
+ rails_email_checker!
68
+ rake (~> 10.0)
69
+ rspec (~> 3.0)
70
+ simplecov (~> 0.16)
71
+
72
+ BUNDLED WITH
73
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 OpenGems
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,39 @@
1
+ # RailsEmailChecker
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rails_email_checker`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'rails_email_checker'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rails_email_checker
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails_email_checker.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'rails_email_checker'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_email_checker/configuration'
4
+ require 'rails_email_checker/constant'
5
+ require 'rails_email_checker/email_validator'
6
+ require 'rails_email_checker/helper_methods'
7
+ require 'rails_email_checker/address'
8
+
9
+ module RailsEmailChecker
10
+ class << self
11
+ attr_writer :configuration
12
+
13
+ def configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ def configure
18
+ yield(configuration)
19
+ end
20
+
21
+ def address(value)
22
+ Address.new(value)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'resolv'
4
+
5
+ module RailsEmailChecker
6
+ class Address
7
+ attr_reader :address
8
+
9
+ def initialize(address)
10
+ @address = address
11
+ raise AddressArgument, "Invalid address #{address}" unless valid?(address)
12
+ end
13
+
14
+ def whitelisted?
15
+ domain_include?(configuration.whitelist_domains)
16
+ end
17
+
18
+ def blacklisted?
19
+ domain_include?(configuration.blacklist_domains)
20
+ end
21
+
22
+ def recorded?
23
+ !records.nil? && records.any?
24
+ end
25
+
26
+ def formatted?
27
+ !(address =~ configuration.regex_email).nil?
28
+ end
29
+
30
+ def sub_addressed?
31
+ address.include?('+')
32
+ end
33
+
34
+ private
35
+
36
+ def valid?(value)
37
+ value.is_a?(String) && !value.nil? && !value.empty?
38
+ end
39
+
40
+ def domain_include?(list)
41
+ list.include?(domain)
42
+ end
43
+
44
+ def domain
45
+ @domain ||= address.gsub(/.+@/, '\1')
46
+ end
47
+
48
+ def records
49
+ @records ||= dns
50
+ end
51
+
52
+ def configuration
53
+ @configuration ||= RailsEmailChecker.configuration
54
+ end
55
+
56
+ def dns
57
+ Resolv::DNS.open do |dns|
58
+ dns.timeouts = configuration.timeouts || 2
59
+ dns.getresources(domain, Resolv::DNS::Resource::IN::MX)
60
+ end
61
+ rescue Resolv::ResolvError, Resolv::ResolvTimeout
62
+ nil
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsEmailChecker
4
+ class Configuration
5
+ attr_accessor :regex_email,
6
+ :blacklist_domains,
7
+ :whitelist_domains,
8
+ :timeouts
9
+
10
+ def initialize
11
+ @regex_email = REGEX_EMAIL
12
+ @blacklist_domains = default_blacklist_domains
13
+ @whitelist_domains = default_whitelist_domains
14
+ @timeouts = 2
15
+ end
16
+
17
+ def add_blacklist_domains(path: nil, domains: nil)
18
+ raise ListArgument, 'Path & domains are nil' if valid_argument?(path, domains)
19
+ @blacklist_domains << load_domains(path) unless path.nil?
20
+ unless domains.nil?
21
+ @blacklist_domains.concat(domains) if domains.is_a?(Array)
22
+ @blacklist_domains << domains if domains.is_a?(String)
23
+ end
24
+ end
25
+
26
+ def add_whitelist_domains(path: nil, domains: nil)
27
+ raise ListArgument, 'Path & domains are nil' if valid_argument?(path, domains)
28
+ @whitelist_domains << load_domains(path) unless path.nil?
29
+ unless domains.nil?
30
+ @whitelist_domains.concat(domains) if domains.is_a?(Array)
31
+ @whitelist_domains << domains if domains.is_a?(String)
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def valid_argument?(path, domains)
38
+ path.nil? && domains.nil?
39
+ end
40
+
41
+ def default_blacklist_domains
42
+ @default_blacklist_domains ||= load_domains(BLACKLIST_FILE)
43
+ end
44
+
45
+ def default_whitelist_domains
46
+ @default_whitelist_domains ||= load_domains(WHITELIST_FILE)
47
+ end
48
+
49
+ def load_domains(path)
50
+ domains = []
51
+ file = File.open(path, 'r')
52
+ file.each_line do |line|
53
+ domain = line.strip
54
+ domains << domain unless domain.nil?
55
+ end
56
+ domains
57
+ rescue StandardError => e
58
+ raise FileNotFound, "File not found: #{e}"
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsEmailChecker
4
+ FileNotFound = Class.new(StandardError)
5
+ ListArgument = Class.new(StandardError)
6
+ AddressArgument = Class.new(StandardError)
7
+
8
+ REGEX_EMAIL = /\b[A-Z0-9._%a-z\-]+@(?:[A-Z0-9a-z\-]+\.)+[A-Za-z]{2,6}\z/.freeze
9
+
10
+ BLACKLIST_FILE = 'vendor/blacklist.txt'
11
+ WHITELIST_FILE = 'vendor/whitelist.txt'
12
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_model'
4
+ require 'active_model/validations'
5
+ require 'rails_email_checker'
6
+
7
+ class EmailValidator < ActiveModel::EachValidator
8
+ def validate_each(record, attribute, value)
9
+ if !value.present? || value.nil?
10
+ add_error(record, attribute, :blank)
11
+ return
12
+ end
13
+
14
+ address = RailsEmailChecker::Address.new(value)
15
+
16
+ return if address.whitelisted?
17
+
18
+ if options[:formatted] && !address.formatted?
19
+ add_error(record, attribute)
20
+ return
21
+ end
22
+
23
+ if options[:blacklisted] && address.blacklisted?
24
+ add_error(record, attribute)
25
+ return
26
+ end
27
+
28
+ if options[:no_sub_addressed] && address.sub_addressed?
29
+ add_error(record, attribute)
30
+ return
31
+ end
32
+
33
+ if options[:recorded] && !address.recorded?
34
+ add_error(record, attribute)
35
+ nil
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def add_error(record, attribute, key = :invalid)
42
+ record.errors.add(attribute, options[:message] || key)
43
+ end
44
+ end