nondisposable 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b423ab4d910c50439d17398fcb84d95013ecd20be9c281f540c7b4540f79cb22
4
+ data.tar.gz: b09ff53a1ac0cf5d47f4b597e89f30e6522836d6fbdcd0bd3de4ed564edfa69f
5
+ SHA512:
6
+ metadata.gz: 6506a82600a139b2ac20b2abb8e84e2215f08af55eacbb43fac4d4524b7feb3b338dfd4cc73e7808957bbc9243f9d72c90fa3c28bc4b248ece56ea6f7d240ba2
7
+ data.tar.gz: d840467aa4befe4c934883cc18bfc33ba847889d4808dd94e633f84ef6a5c690b5c9b93e5d1fbc0a5e45daa5d1524f3534fce9db52aea3939a9ef01a490c18ef
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-09-25
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Javi R
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,137 @@
1
+ # 🗑️ `nondisposable` - Block disposable email addresses from signing up to your Rails app
2
+
3
+ `nondisposable` is a Ruby gem that prevents users from signing up to your Rails app with disposable email addresses.
4
+
5
+ Simply add to your User model:
6
+
7
+ ```ruby
8
+ validates :email, nondisposable: true
9
+ ```
10
+
11
+ That's it! You're done.
12
+
13
+ The gem also provides a job you can run daily to keep your disposable domain list up to date.
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'nondisposable'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ ```bash
26
+ bundle install
27
+ ```
28
+
29
+ After installing the gem, run the installation generator:
30
+
31
+ ```bash
32
+ rails generate nondisposable:install
33
+ ```
34
+
35
+ This will create the necessary migration file, initializer, and a job for scheduled updates. Run the migration:
36
+
37
+ ```bash
38
+ rails db:migrate
39
+ ```
40
+
41
+ Finally, populate the initial list of disposable domains:
42
+
43
+ ```ruby
44
+ Nondisposable::DomainListUpdater.update
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ To use `nondisposable` in your models, simply add the validation:
50
+
51
+ ```ruby
52
+ class User < ApplicationRecord
53
+ validates :email, nondisposable: true
54
+ end
55
+ ```
56
+
57
+ You can customize the error message:
58
+ ```ruby
59
+ class User < ApplicationRecord
60
+ validates :email, nondisposable: { message: "is a disposable email address, please use a permanent email address." }
61
+ end
62
+ ```
63
+
64
+ The validation works seamlessly with other Rails validations:
65
+ ```ruby
66
+ class User < ApplicationRecord
67
+ validates :email,
68
+ presence: true,
69
+ format: { with: URI::MailTo::EMAIL_REGEXP },
70
+ nondisposable: true
71
+ end
72
+ ```
73
+
74
+ If you're validating a different attribute name:
75
+ ```ruby
76
+ class User < ApplicationRecord
77
+ validates :backup_email, nondisposable: true
78
+ end
79
+ ```
80
+
81
+ ### Configuration
82
+
83
+ You can customize the gem's behavior by creating an initializer:
84
+
85
+ ```ruby
86
+ # config/initializers/nondisposable.rb
87
+
88
+ Nondisposable.configure do |config|
89
+ config.error_message = "provider is not allowed. Please use a non-disposable email address."
90
+ config.additional_domains = ['custom-disposable-domain.com']
91
+ config.excluded_domains = ['false-positive-domain.com']
92
+ end
93
+ ```
94
+
95
+ ### Direct Check
96
+
97
+ You can also check if an email is disposable directly:
98
+
99
+ ```ruby
100
+ Nondisposable.disposable?('user@example.com') # => false
101
+ Nondisposable.disposable?('user@disposable-email.com') # => true
102
+ ```
103
+
104
+ ## Updating disposable domains
105
+
106
+ To manually update the list of disposable domains, run:
107
+
108
+ ```ruby
109
+ Nondisposable::DomainListUpdater.update
110
+ ```
111
+
112
+ It's important you keep your disposable domain list up to date. `nondisposable` will read from the latest version of the [`disposable-email-domains`](https://github.com/disposable-email-domains/disposable-email-domains) list, which is typically updated every few days.
113
+
114
+ For this, `nondisposable` provides you with an Active Job (`DisposableEmailDomainListUpdateJob`) that you can use to schedule daily updates. How you do that, exactly, depends on the queueing system you're using.
115
+
116
+ If you're using `solid_queue` (the Rails 8 default), you can easily add it to your schedule in the `config/recurring.yml` file like this:
117
+ ```yaml
118
+ production:
119
+ refresh_disposable_domains:
120
+ class: DisposableEmailDomainListUpdateJob
121
+ queue: default
122
+ schedule: every day at 3am US/Pacific
123
+ ```
124
+
125
+ ## Development
126
+
127
+ 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.
128
+
129
+ To install this gem onto your local machine, run `bundle exec rake install`.
130
+
131
+ ## Contributing
132
+
133
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rameerez/nondisposable. Our code of conduct is: just be nice and make your mom proud of what you do and post online.
134
+
135
+ ## License
136
+
137
+ 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,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nondisposable
4
+ class DisposableDomain < ApplicationRecord
5
+ validates :name, presence: true, uniqueness: { case_sensitive: false }
6
+
7
+ class << self
8
+ def disposable?(domain)
9
+ return false if domain.blank?
10
+ domain = domain.to_s.downcase
11
+ Nondisposable.configuration.additional_domains.include?(domain) ||
12
+ (where(name: domain).exists? && !Nondisposable.configuration.excluded_domains.include?(domain))
13
+ end
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators/base'
4
+ require 'rails/generators/active_record'
5
+
6
+ module Nondisposable
7
+ module Generators
8
+ class InstallGenerator < Rails::Generators::Base
9
+ include ActiveRecord::Generators::Migration
10
+
11
+ source_root File.expand_path('templates', __dir__)
12
+
13
+ def self.next_migration_number(dir)
14
+ ActiveRecord::Generators::Base.next_migration_number(dir)
15
+ end
16
+
17
+ def create_migration_file
18
+ migration_template 'create_nondisposable_disposable_domains.rb.erb', File.join(db_migrate_path, "create_nondisposable_disposable_domains.rb")
19
+ end
20
+
21
+ def create_initializer
22
+ template 'nondisposable.rb', 'config/initializers/nondisposable.rb'
23
+ end
24
+
25
+ def create_database_refresh_job
26
+ template 'disposable_email_domain_list_update_job.rb', 'app/jobs/disposable_email_domain_list_update_job.rb'
27
+ end
28
+
29
+ def display_post_install_message
30
+ say "\tThe `nondisposable` gem has been successfully installed!", :green
31
+ say "\nTo complete the setup:"
32
+ say " 1. Run 'rails db:migrate' to create the necessary tables."
33
+ say " 2. Run 'Nondisposable::DomainListUpdater.update' to populate the initial list of disposable domains."
34
+ say " 3. Add 'validates :email, nondisposable: true' to your User model (or any model with an email field)."
35
+ say " 4. Configure your recurrent job according to the README, and make sure you have a functional queuing system (like solid_queue) that can run jobs properly so the disposable emails list is updated regularly."
36
+ say "\nEnjoy your new `nondisposable` users!", :green
37
+ end
38
+
39
+ private
40
+
41
+ def migration_version
42
+ "[#{ActiveRecord::VERSION::STRING.to_f}]"
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,21 @@
1
+ class CreateNondisposableDisposableDomains < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ primary_key_type, foreign_key_type = primary_and_foreign_key_types
4
+
5
+ create_table :nondisposable_disposable_domains, id: primary_key_type do |t|
6
+ t.string :name, null: false, index: { unique: true }
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def primary_and_foreign_key_types
15
+ config = Rails.configuration.generators
16
+ setting = config.options[config.orm][:primary_key_type]
17
+ primary_key_type = setting || :primary_key
18
+ foreign_key_type = setting || :bigint
19
+ [primary_key_type, foreign_key_type]
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ class DisposableEmailDomainListUpdateJob < ApplicationJob
2
+ queue_as :default
3
+
4
+ def perform(*args)
5
+ Nondisposable::DomainListUpdater.update
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ Nondisposable.configure do |config|
2
+ # Customize the error message if needed
3
+ # config.error_message = "is not allowed. Please use a non-disposable email address."
4
+ #
5
+ # Add custom domains you want to be considered as disposable
6
+ # config.additional_domains = ['custom-disposable-domain.com']
7
+ #
8
+ # Exclude domains that are considered disposable but you want to allow anyways
9
+ # config.excluded_domains = ['false-positive-domain.com']
10
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open-uri'
4
+ require 'net/http'
5
+
6
+ module Nondisposable
7
+ class DomainListUpdater
8
+
9
+ def self.update
10
+ Rails.logger.info "Refreshing list of disposable domains..."
11
+
12
+ url = 'https://raw.githubusercontent.com/disposable-email-domains/disposable-email-domains/master/disposable_email_blocklist.conf'
13
+
14
+ begin
15
+ uri = URI(url)
16
+ response = Net::HTTP.get_response(uri)
17
+
18
+ if response.is_a?(Net::HTTPSuccess)
19
+ downloaded_domains = response.body.split("\n")
20
+ raise "The list is empty. This might indicate a problem with the format." if downloaded_domains.empty?
21
+
22
+ Rails.logger.info "Downloaded list of disposable domains..."
23
+
24
+ domains = (downloaded_domains + Nondisposable.configuration.additional_domains).uniq
25
+ domains -= Nondisposable.configuration.excluded_domains
26
+
27
+ ActiveRecord::Base.transaction do
28
+ Rails.logger.info "Updating disposable domains..."
29
+ Nondisposable::DisposableDomain.delete_all
30
+
31
+ domains.each { |domain| Nondisposable::DisposableDomain.create(name: domain.downcase) }
32
+ end
33
+
34
+ Rails.logger.info "Finished updating disposable domains. Total domains: #{domains.count}"
35
+ true
36
+ else
37
+ Rails.logger.error "Failed to download the list. HTTP Status: #{response.code}"
38
+ false
39
+ end
40
+ rescue SocketError => e
41
+ Rails.logger.error "Network error occurred: #{e.message}"
42
+ false
43
+ rescue StandardError => e
44
+ Rails.logger.error "An error occurred when trying to update the list of disposable domains: #{e.message}"
45
+ false
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveModel
4
+ module Validations
5
+ class NondisposableValidator < EachValidator
6
+ def validate_each(record, attribute, value)
7
+ return if value.blank?
8
+
9
+ begin
10
+ domain = value.to_s.split('@').last&.downcase
11
+ return if domain.nil? # Invalid email format
12
+
13
+ if Nondisposable::DisposableDomain.disposable?(domain)
14
+ record.errors.add(attribute, options[:message] || Nondisposable.configuration.error_message)
15
+ end
16
+ rescue StandardError => e
17
+ Rails.logger.error "Nondisposable validation error: #{e.message}"
18
+ record.errors.add(attribute, "is an invalid email address, cannot check if it's disposable")
19
+ end
20
+ end
21
+ end
22
+
23
+ module HelperMethods
24
+ # Kept for backwards compatibility
25
+ def validates_nondisposable_email(*attr_names)
26
+ validates_with NondisposableValidator, _merge_attributes(attr_names)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nondisposable
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace Nondisposable
6
+
7
+ initializer "nondisposable.assets.precompile" do |app|
8
+ app.config.assets.precompile += %w( nondisposable/application.css nondisposable/application.js )
9
+ end
10
+
11
+ config.generators do |g|
12
+ g.test_framework :rspec
13
+ g.fixture_replacement :factory_bot
14
+ g.factory_bot dir: 'spec/factories'
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module Nondisposable
2
+ class Railtie < Rails::Railtie
3
+ initializer 'nondisposable.add_validator' do
4
+ ActiveSupport.on_load(:active_record) do
5
+ include ActiveModel::Validations::NondisposableValidator
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nondisposable
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "nondisposable/version"
4
+ require_relative "nondisposable/engine"
5
+ require_relative "nondisposable/email_validator"
6
+ require_relative "nondisposable/domain_list_updater"
7
+ module Nondisposable
8
+ class Error < StandardError; end
9
+
10
+ class << self
11
+ attr_accessor :configuration
12
+ end
13
+
14
+ def self.configure
15
+ self.configuration ||= Configuration.new
16
+ yield(configuration)
17
+ end
18
+
19
+ def self.disposable?(email)
20
+ return false if email.nil? || !email.include?('@')
21
+ domain = email.to_s.split('@').last.downcase
22
+ DisposableDomain.disposable?(domain)
23
+ end
24
+
25
+ class Configuration
26
+ attr_accessor :error_message, :additional_domains, :excluded_domains
27
+
28
+ def initialize
29
+ @error_message = "provider is not allowed"
30
+ @additional_domains = []
31
+ @excluded_domains = []
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,4 @@
1
+ module Nondisposable
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nondisposable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - rameerez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.0
27
+ description: Block disposable email addresses from signing up to your Rails app. Comes
28
+ with a job so you can automatically update the database of disposable email domains
29
+ on a regular basis (daily, weekly, etc).
30
+ email:
31
+ - rubygems@rameerez.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - CHANGELOG.md
37
+ - LICENSE.txt
38
+ - README.md
39
+ - Rakefile
40
+ - app/models/nondisposable/disposable_domain.rb
41
+ - lib/generators/nondisposable/install_generator.rb
42
+ - lib/generators/nondisposable/templates/create_nondisposable_disposable_domains.rb.erb
43
+ - lib/generators/nondisposable/templates/disposable_email_domain_list_update_job.rb
44
+ - lib/generators/nondisposable/templates/nondisposable.rb
45
+ - lib/nondisposable.rb
46
+ - lib/nondisposable/domain_list_updater.rb
47
+ - lib/nondisposable/email_validator.rb
48
+ - lib/nondisposable/engine.rb
49
+ - lib/nondisposable/railtie.rb
50
+ - lib/nondisposable/version.rb
51
+ - sig/nondisposable.rbs
52
+ homepage: https://github.com/rameerez/nondisposable
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ allowed_push_host: https://rubygems.org
57
+ homepage_uri: https://github.com/rameerez/nondisposable
58
+ source_code_uri: https://github.com/rameerez/nondisposable
59
+ changelog_uri: https://github.com/rameerez/nondisposable/blob/main/CHANGELOG.md
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.0
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubygems_version: 3.5.22
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Block disposable emails from signing up to your Rails app
79
+ test_files: []