email_domain_checker 0.1.2 → 0.1.3
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 +4 -4
- data/CHANGELOG.md +10 -0
- data/README.md +27 -112
- data/docs/configuration/cache.md +165 -0
- data/docs/configuration/options.md +68 -0
- data/docs/configuration/test-mode.md +40 -0
- data/docs/contributing.md +9 -0
- data/docs/getting-started/installation.md +28 -0
- data/docs/getting-started/quick-start.md +47 -0
- data/docs/index.md +37 -0
- data/docs/usage/activemodel-integration.md +68 -0
- data/docs/usage/checker-class.md +60 -0
- data/docs/usage/module-methods.md +95 -0
- data/docs-templates/index.html +71 -0
- data/lib/email_domain_checker/cache/base_adapter.rb +67 -0
- data/lib/email_domain_checker/cache/memory_adapter.rb +91 -0
- data/lib/email_domain_checker/cache/redis_adapter.rb +66 -0
- data/lib/email_domain_checker/cache.rb +54 -0
- data/lib/email_domain_checker/config.rb +116 -2
- data/lib/email_domain_checker/dns_resolver.rb +26 -3
- data/lib/email_domain_checker/domain_validator.rb +2 -1
- data/lib/email_domain_checker/version.rb +1 -1
- data/lib/email_domain_checker.rb +32 -0
- data/mkdocs.yml +65 -0
- data/requirements.txt +2 -0
- metadata +18 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 16e187af255690a6a15e34fdfd3d034443ae6d335781f1b499ec0c100da6731b
|
|
4
|
+
data.tar.gz: 790adc384696330eadd419fed2cb46ff23fe0f874b59a28d7b74afc0a0bdb37b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fbd0732d8fccc016ca9ff9d302abcdc8e19f8fe2cbf392f671bb726306727cee4f5b0d2393f38736a6f9d1d4ed4db42359df667579eb6f7116d1691723b09115
|
|
7
|
+
data.tar.gz: 45cf7b78d7a22cafca5e1b236ea66426c375345acebc48b389d2230fda733d5a353a5e4cb72a18de17cc6bf6f8b21ee5ad532becd3566d010c514fbb262608f8
|
data/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.3] - 2025-11-06
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- DNS validation result caching functionality with memory and Redis adapters, custom adapter support, and Rails-style `cache.with` method ([#7](https://github.com/tatematsu-k/email_domain_checker/pull/7))
|
|
14
|
+
|
|
15
|
+
### Documentation
|
|
16
|
+
- Added comprehensive documentation site with MkDocs including installation guide, usage examples, configuration options, and ActiveModel integration documentation
|
|
17
|
+
|
|
8
18
|
## [0.1.2] - 2025-11-05
|
|
9
19
|
|
|
10
20
|
### Added
|
data/README.md
CHANGED
|
@@ -18,9 +18,7 @@ Or install it yourself as:
|
|
|
18
18
|
|
|
19
19
|
$ gem install email_domain_checker
|
|
20
20
|
|
|
21
|
-
##
|
|
22
|
-
|
|
23
|
-
### Module-level convenience methods
|
|
21
|
+
## Quick Start
|
|
24
22
|
|
|
25
23
|
```ruby
|
|
26
24
|
require 'email_domain_checker'
|
|
@@ -28,137 +26,54 @@ require 'email_domain_checker'
|
|
|
28
26
|
# Quick validation
|
|
29
27
|
EmailDomainChecker.valid?("user@example.com", validate_domain: false) # => true
|
|
30
28
|
|
|
31
|
-
#
|
|
32
|
-
EmailDomainChecker.format_valid?("user@example.com") # => true
|
|
33
|
-
|
|
34
|
-
# Domain validation only
|
|
29
|
+
# Domain validation with MX records
|
|
35
30
|
EmailDomainChecker.domain_valid?("user@example.com", check_mx: true) # => true/false
|
|
36
31
|
|
|
37
32
|
# Normalize email
|
|
38
33
|
EmailDomainChecker.normalize("User@Example.COM") # => "user@example.com"
|
|
39
|
-
|
|
40
|
-
# Configure default options globally
|
|
41
|
-
EmailDomainChecker.configure(timeout: 10, check_mx: true)
|
|
42
|
-
|
|
43
|
-
# Enable test mode (skips DNS checks)
|
|
44
|
-
EmailDomainChecker.configure do |config|
|
|
45
|
-
config.test_mode = true
|
|
46
|
-
end
|
|
47
34
|
```
|
|
48
35
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
```ruby
|
|
52
|
-
require 'email_domain_checker'
|
|
53
|
-
|
|
54
|
-
# Basic validation
|
|
55
|
-
checker = EmailDomainChecker::Checker.new("user@example.com")
|
|
56
|
-
if checker.valid?
|
|
57
|
-
puts "Valid email with valid domain"
|
|
58
|
-
end
|
|
36
|
+
## Documentation
|
|
59
37
|
|
|
60
|
-
|
|
61
|
-
checker = EmailDomainChecker::Checker.new("user@example.com", validate_domain: false)
|
|
62
|
-
checker.format_valid? # => true
|
|
38
|
+
📖 **[Full Documentation](https://tatematsu-k.github.io/email_domain_checker/latest/)** - Complete usage guide, API reference, and examples
|
|
63
39
|
|
|
64
|
-
|
|
65
|
-
checker = EmailDomainChecker::Checker.new("user@example.com", check_mx: true)
|
|
66
|
-
checker.domain_valid? # => true if MX records exist
|
|
40
|
+
## Features
|
|
67
41
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
42
|
+
- ✅ Email format validation
|
|
43
|
+
- ✅ Domain existence validation
|
|
44
|
+
- ✅ MX record checking
|
|
45
|
+
- ✅ A record checking
|
|
46
|
+
- ✅ Email normalization
|
|
47
|
+
- ✅ ActiveModel/ActiveRecord integration
|
|
48
|
+
- ✅ DNS result caching (memory and Redis)
|
|
49
|
+
- ✅ Test mode for development
|
|
71
50
|
|
|
72
|
-
|
|
73
|
-
checker = EmailDomainChecker::Checker.new("user.name+tag@gmail.com")
|
|
74
|
-
checker.canonical_email # => "username@gmail.com"
|
|
75
|
-
|
|
76
|
-
# Get redacted email (for privacy)
|
|
77
|
-
checker = EmailDomainChecker::Checker.new("user@example.com")
|
|
78
|
-
checker.redacted_email # => "{hash}@example.com"
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
### ActiveModel/ActiveRecord Integration
|
|
82
|
-
|
|
83
|
-
When ActiveModel is available, you can use the `DomainCheckValidator` for easy validation and normalization in your models:
|
|
84
|
-
|
|
85
|
-
```ruby
|
|
86
|
-
class User < ActiveRecord::Base
|
|
87
|
-
validates :email, domain_check: { check_mx: true, timeout: 3 }, normalize: true
|
|
88
|
-
end
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
#### Options
|
|
92
|
-
|
|
93
|
-
- `domain_check`: Hash of options for domain validation
|
|
94
|
-
- `check_mx`: Check MX records (default: `true`)
|
|
95
|
-
- `check_a`: Check A records (default: `false`)
|
|
96
|
-
- `timeout`: DNS query timeout in seconds (default: `5`)
|
|
97
|
-
- `validate_format`: Validate email format (default: `true`)
|
|
98
|
-
- `validate_domain`: Validate domain (default: `true`)
|
|
99
|
-
- `normalize`: Normalize email before validation (default: `false`)
|
|
100
|
-
- `message`: Custom error message
|
|
51
|
+
## Development
|
|
101
52
|
|
|
102
|
-
|
|
53
|
+
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.
|
|
103
54
|
|
|
104
|
-
|
|
105
|
-
# Basic validation with domain check
|
|
106
|
-
class User < ActiveRecord::Base
|
|
107
|
-
validates :email, domain_check: { check_mx: true, timeout: 3 }
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
# Format validation only (skip domain check)
|
|
111
|
-
class User < ActiveRecord::Base
|
|
112
|
-
validates :email, domain_check: { validate_domain: false }
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
# With automatic normalization
|
|
116
|
-
class User < ActiveRecord::Base
|
|
117
|
-
validates :email, domain_check: { check_mx: true }, normalize: true
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
# With custom error message
|
|
121
|
-
class User < ActiveRecord::Base
|
|
122
|
-
validates :email, domain_check: { check_mx: true }, message: "Invalid email address"
|
|
123
|
-
end
|
|
124
|
-
```
|
|
55
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
125
56
|
|
|
126
|
-
###
|
|
57
|
+
### Building Documentation Locally
|
|
127
58
|
|
|
128
|
-
|
|
129
|
-
- `validate_domain`: Validate domain existence (default: true)
|
|
130
|
-
- `check_mx`: Check MX records for domain (default: true)
|
|
131
|
-
- `check_a`: Check A records for domain (default: false)
|
|
132
|
-
- `timeout`: DNS lookup timeout in seconds (default: 5)
|
|
133
|
-
- `test_mode`: Skip DNS checks (useful for testing with dummy data) (default: false)
|
|
59
|
+
To build and preview the documentation locally:
|
|
134
60
|
|
|
135
|
-
|
|
61
|
+
```bash
|
|
62
|
+
# Install Python dependencies
|
|
63
|
+
pip install -r requirements.txt
|
|
136
64
|
|
|
137
|
-
|
|
65
|
+
# Start development server (with live reload)
|
|
66
|
+
mkdocs serve
|
|
138
67
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
EmailDomainChecker.configure do |config|
|
|
142
|
-
config.test_mode = true
|
|
143
|
-
end
|
|
144
|
-
|
|
145
|
-
# Or in a before block
|
|
146
|
-
before do
|
|
147
|
-
EmailDomainChecker::Config.test_mode = true
|
|
148
|
-
end
|
|
68
|
+
# Build static site
|
|
69
|
+
mkdocs build
|
|
149
70
|
```
|
|
150
71
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
## Development
|
|
154
|
-
|
|
155
|
-
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.
|
|
156
|
-
|
|
157
|
-
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
72
|
+
The documentation will be available at `http://127.0.0.1:8000` when using `mkdocs serve`. The built files will be in the `site/` directory when using `mkdocs build`.
|
|
158
73
|
|
|
159
74
|
## Contributing
|
|
160
75
|
|
|
161
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
|
76
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tatematsu-k/email_domain_checker.
|
|
162
77
|
|
|
163
78
|
## License
|
|
164
79
|
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# Cache Configuration
|
|
2
|
+
|
|
3
|
+
DNS validation results are cached by default using in-memory storage to improve performance when validating the same domains multiple times. The cache supports both in-memory storage (default) and Redis (optional).
|
|
4
|
+
|
|
5
|
+
!!! note "Production Recommendation"
|
|
6
|
+
While memory cache is enabled by default and works well for development and small applications, we recommend using Redis cache in production environments for better scalability, persistence across application restarts, and shared cache across multiple application instances.
|
|
7
|
+
|
|
8
|
+
## Memory Cache (Default - Enabled by Default)
|
|
9
|
+
|
|
10
|
+
Cache is enabled by default - no configuration needed. Just use EmailDomainChecker normally and DNS results will be cached.
|
|
11
|
+
|
|
12
|
+
### Customize Cache TTL
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
EmailDomainChecker.configure do |config|
|
|
16
|
+
config.cache_ttl = 1800 # Cache entries expire after 30 minutes
|
|
17
|
+
end
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
### Disable Cache
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
EmailDomainChecker.configure do |config|
|
|
24
|
+
config.cache_enabled = false
|
|
25
|
+
end
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Redis Cache (Recommended for Production)
|
|
29
|
+
|
|
30
|
+
For production environments, Redis cache is recommended for the following reasons:
|
|
31
|
+
|
|
32
|
+
- **Persistence**: Cache survives application restarts
|
|
33
|
+
- **Scalability**: Shared cache across multiple application instances
|
|
34
|
+
- **Performance**: Better memory management for large-scale applications
|
|
35
|
+
- **Reliability**: Handles cache eviction and expiration more efficiently
|
|
36
|
+
|
|
37
|
+
### Setup
|
|
38
|
+
|
|
39
|
+
First, add the `redis` gem to your Gemfile:
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
gem 'redis'
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Then configure:
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
# Enable Redis cache
|
|
49
|
+
EmailDomainChecker.configure do |config|
|
|
50
|
+
config.cache_enabled = true
|
|
51
|
+
config.cache_type = :redis
|
|
52
|
+
config.cache_ttl = 3600
|
|
53
|
+
config.redis_client = Redis.new(url: "redis://localhost:6379")
|
|
54
|
+
end
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
!!! tip "Fallback Behavior"
|
|
58
|
+
If Redis is not available, the gem will automatically fall back to memory cache.
|
|
59
|
+
|
|
60
|
+
## Cache Management
|
|
61
|
+
|
|
62
|
+
### Clear All Cache
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
# Clear all cached DNS validation results
|
|
66
|
+
EmailDomainChecker.clear_cache
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Clear Cache for Specific Domain
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
# Clear cache for a specific domain
|
|
73
|
+
EmailDomainChecker.clear_cache_for_domain("example.com")
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### Cache Keys
|
|
77
|
+
|
|
78
|
+
Cache keys are automatically managed by the gem:
|
|
79
|
+
|
|
80
|
+
- MX records: `mx:example.com`
|
|
81
|
+
- A records: `a:example.com`
|
|
82
|
+
|
|
83
|
+
## Using `with` Method (Rails-style Cache Interface)
|
|
84
|
+
|
|
85
|
+
The cache adapters support a Rails-like `with` method (similar to `Rails.cache.fetch`) for convenient cache access. Cache is enabled by default, so you can use it immediately:
|
|
86
|
+
|
|
87
|
+
```ruby
|
|
88
|
+
# Method 1: Direct access via EmailDomainChecker.cache (recommended)
|
|
89
|
+
result = EmailDomainChecker.cache.with("my_key", ttl: 3600) do
|
|
90
|
+
# This block executes only when cache misses
|
|
91
|
+
expensive_computation
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Method 2: Using convenience method
|
|
95
|
+
result = EmailDomainChecker.with_cache("my_key", ttl: 3600) do
|
|
96
|
+
expensive_computation
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Force cache refresh
|
|
100
|
+
result = EmailDomainChecker.cache.with("my_key", force: true) do
|
|
101
|
+
# This block always executes
|
|
102
|
+
updated_computation
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Or using convenience method
|
|
106
|
+
result = EmailDomainChecker.with_cache("my_key", force: true) do
|
|
107
|
+
updated_computation
|
|
108
|
+
end
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Custom Cache Adapter
|
|
112
|
+
|
|
113
|
+
You can implement your own cache adapter by inheriting from `EmailDomainChecker::Cache::BaseAdapter`:
|
|
114
|
+
|
|
115
|
+
```ruby
|
|
116
|
+
# Define a custom cache adapter
|
|
117
|
+
class MyCustomCacheAdapter < EmailDomainChecker::Cache::BaseAdapter
|
|
118
|
+
def initialize
|
|
119
|
+
@store = {} # Your custom storage
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def get(key)
|
|
123
|
+
# Your custom get logic
|
|
124
|
+
@store[key]
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def set(key, value, ttl: nil)
|
|
128
|
+
# Your custom set logic
|
|
129
|
+
@store[key] = value
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def delete(key)
|
|
133
|
+
# Your custom delete logic
|
|
134
|
+
@store.delete(key)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def clear
|
|
138
|
+
# Your custom clear logic
|
|
139
|
+
@store.clear
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Using Custom Adapter Instance
|
|
145
|
+
|
|
146
|
+
```ruby
|
|
147
|
+
# Use custom adapter instance
|
|
148
|
+
EmailDomainChecker.configure do |config|
|
|
149
|
+
config.cache_enabled = true
|
|
150
|
+
config.cache_adapter_instance = MyCustomCacheAdapter.new
|
|
151
|
+
end
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Using Custom Adapter Class
|
|
155
|
+
|
|
156
|
+
```ruby
|
|
157
|
+
# Or use custom adapter class (will be instantiated automatically)
|
|
158
|
+
EmailDomainChecker.configure do |config|
|
|
159
|
+
config.cache_enabled = true
|
|
160
|
+
config.cache_type = MyCustomCacheAdapter
|
|
161
|
+
end
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
!!! note "Priority"
|
|
165
|
+
When both `cache_adapter_instance` and `cache_type` are set, the instance takes priority.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Configuration Options
|
|
2
|
+
|
|
3
|
+
EmailDomainChecker supports various configuration options to customize its behavior.
|
|
4
|
+
|
|
5
|
+
## Available Options
|
|
6
|
+
|
|
7
|
+
### Validation Options
|
|
8
|
+
|
|
9
|
+
- `validate_format`: Validate email format using email_address gem (default: `true`)
|
|
10
|
+
- `validate_domain`: Validate domain existence (default: `true`)
|
|
11
|
+
- `check_mx`: Check MX records for domain (default: `true`)
|
|
12
|
+
- `check_a`: Check A records for domain (default: `false`)
|
|
13
|
+
- `timeout`: DNS lookup timeout in seconds (default: `5`)
|
|
14
|
+
|
|
15
|
+
### Test Mode
|
|
16
|
+
|
|
17
|
+
- `test_mode`: Skip DNS checks (useful for testing with dummy data) (default: `false`)
|
|
18
|
+
|
|
19
|
+
When test mode is enabled, domain validation will always return `true` without making any DNS requests, allowing you to use dummy email addresses in your tests without external dependencies.
|
|
20
|
+
|
|
21
|
+
### Cache Options
|
|
22
|
+
|
|
23
|
+
- `cache_enabled`: Enable/disable DNS validation result caching (default: `true` - memory cache is enabled by default)
|
|
24
|
+
- `cache_type`: Cache adapter type (`:memory`, `:redis`, or a custom `BaseAdapter` class) (default: `:memory`)
|
|
25
|
+
- `cache_adapter_instance`: Custom cache adapter instance (must inherit from `Cache::BaseAdapter`)
|
|
26
|
+
- `cache_ttl`: Cache time-to-live in seconds (default: `3600`)
|
|
27
|
+
- `redis_client`: Custom Redis client instance (only used when `cache_type` is `:redis`)
|
|
28
|
+
|
|
29
|
+
## Configuration Examples
|
|
30
|
+
|
|
31
|
+
### Basic Configuration
|
|
32
|
+
|
|
33
|
+
```ruby
|
|
34
|
+
EmailDomainChecker.configure(timeout: 10, check_mx: true)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Block Configuration
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
EmailDomainChecker.configure do |config|
|
|
41
|
+
config.timeout = 10
|
|
42
|
+
config.check_mx = true
|
|
43
|
+
config.check_a = false
|
|
44
|
+
config.cache_ttl = 1800
|
|
45
|
+
end
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Test Mode Configuration
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
EmailDomainChecker.configure do |config|
|
|
52
|
+
config.test_mode = true
|
|
53
|
+
end
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Cache Configuration
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
EmailDomainChecker.configure do |config|
|
|
60
|
+
config.cache_enabled = true
|
|
61
|
+
config.cache_type = :memory
|
|
62
|
+
config.cache_ttl = 3600
|
|
63
|
+
end
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Note on Cache Adapter Priority
|
|
67
|
+
|
|
68
|
+
When both `cache_adapter_instance` and `cache_type` are set, the instance takes priority.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Test Mode
|
|
2
|
+
|
|
3
|
+
When writing tests, you may want to skip DNS checks to avoid external requests. Enable test mode to skip all DNS validations (MX and A record checks).
|
|
4
|
+
|
|
5
|
+
## Enabling Test Mode
|
|
6
|
+
|
|
7
|
+
### In spec_helper.rb or test_helper.rb
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
EmailDomainChecker.configure do |config|
|
|
11
|
+
config.test_mode = true
|
|
12
|
+
end
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### In a before block
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
before do
|
|
19
|
+
EmailDomainChecker::Config.test_mode = true
|
|
20
|
+
end
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Behavior
|
|
24
|
+
|
|
25
|
+
When test mode is enabled, domain validation will always return `true` without making any DNS requests, allowing you to use dummy email addresses in your tests without external dependencies.
|
|
26
|
+
|
|
27
|
+
## Example
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
# In spec_helper.rb
|
|
31
|
+
EmailDomainChecker.configure do |config|
|
|
32
|
+
config.test_mode = true
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# In your specs
|
|
36
|
+
it "validates email format" do
|
|
37
|
+
expect(EmailDomainChecker.valid?("test@example.com")).to be true
|
|
38
|
+
# Domain validation is skipped, but format validation still works
|
|
39
|
+
end
|
|
40
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/tatematsu-k/email_domain_checker.
|
|
4
|
+
|
|
5
|
+
## Development
|
|
6
|
+
|
|
7
|
+
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.
|
|
8
|
+
|
|
9
|
+
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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Installation
|
|
2
|
+
|
|
3
|
+
## Using Bundler
|
|
4
|
+
|
|
5
|
+
Add this line to your application's Gemfile:
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
gem 'email_domain_checker'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
$ bundle install
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Manual Installation
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
$ gem install email_domain_checker
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Requirements
|
|
26
|
+
|
|
27
|
+
- Ruby 3.0 or higher
|
|
28
|
+
- Optional: `redis` gem for Redis cache support
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# Quick Start
|
|
2
|
+
|
|
3
|
+
## Basic Usage
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
require 'email_domain_checker'
|
|
7
|
+
|
|
8
|
+
# Quick validation
|
|
9
|
+
EmailDomainChecker.valid?("user@example.com", validate_domain: false) # => true
|
|
10
|
+
|
|
11
|
+
# Format validation only
|
|
12
|
+
EmailDomainChecker.format_valid?("user@example.com") # => true
|
|
13
|
+
|
|
14
|
+
# Domain validation only
|
|
15
|
+
EmailDomainChecker.domain_valid?("user@example.com", check_mx: true) # => true/false
|
|
16
|
+
|
|
17
|
+
# Normalize email
|
|
18
|
+
EmailDomainChecker.normalize("User@Example.COM") # => "user@example.com"
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Configure Default Options
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
# Configure default options globally
|
|
25
|
+
EmailDomainChecker.configure(timeout: 10, check_mx: true)
|
|
26
|
+
|
|
27
|
+
# Enable test mode (skips DNS checks)
|
|
28
|
+
EmailDomainChecker.configure do |config|
|
|
29
|
+
config.test_mode = true
|
|
30
|
+
end
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Cache Configuration
|
|
34
|
+
|
|
35
|
+
Cache is enabled by default (memory cache):
|
|
36
|
+
|
|
37
|
+
```ruby
|
|
38
|
+
# Cache is enabled by default - no configuration needed
|
|
39
|
+
# Just use EmailDomainChecker normally and DNS results will be cached
|
|
40
|
+
|
|
41
|
+
# Customize cache TTL if needed
|
|
42
|
+
EmailDomainChecker.configure do |config|
|
|
43
|
+
config.cache_ttl = 1800 # Cache entries expire after 30 minutes
|
|
44
|
+
end
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
For production environments, Redis cache is recommended. See [Cache Configuration](../configuration/cache.md) for details.
|
data/docs/index.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# EmailDomainChecker
|
|
2
|
+
|
|
3
|
+
Email address validation and domain checking library to prevent mail server reputation degradation.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ✅ Email format validation
|
|
8
|
+
- ✅ Domain existence validation
|
|
9
|
+
- ✅ MX record checking
|
|
10
|
+
- ✅ A record checking
|
|
11
|
+
- ✅ Email normalization
|
|
12
|
+
- ✅ ActiveModel/ActiveRecord integration
|
|
13
|
+
- ✅ DNS result caching (memory and Redis)
|
|
14
|
+
- ✅ Test mode for development
|
|
15
|
+
|
|
16
|
+
## Quick Example
|
|
17
|
+
|
|
18
|
+
```ruby
|
|
19
|
+
require 'email_domain_checker'
|
|
20
|
+
|
|
21
|
+
# Quick validation
|
|
22
|
+
EmailDomainChecker.valid?("user@example.com", validate_domain: false) # => true
|
|
23
|
+
|
|
24
|
+
# Domain validation with MX records
|
|
25
|
+
EmailDomainChecker.domain_valid?("user@example.com", check_mx: true) # => true/false
|
|
26
|
+
|
|
27
|
+
# Normalize email
|
|
28
|
+
EmailDomainChecker.normalize("User@Example.COM") # => "user@example.com"
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Documentation
|
|
32
|
+
|
|
33
|
+
Please see the [Getting Started](getting-started/installation.md) guide for installation and setup instructions.
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
|
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# ActiveModel/ActiveRecord Integration
|
|
2
|
+
|
|
3
|
+
When ActiveModel is available, you can use the `DomainCheckValidator` for easy validation and normalization in your models.
|
|
4
|
+
|
|
5
|
+
## Basic Usage
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
class User < ActiveRecord::Base
|
|
9
|
+
validates :email, domain_check: { check_mx: true, timeout: 3 }, normalize: true
|
|
10
|
+
end
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Validator Options
|
|
14
|
+
|
|
15
|
+
### `domain_check` Options
|
|
16
|
+
|
|
17
|
+
Hash of options for domain validation:
|
|
18
|
+
|
|
19
|
+
- `check_mx`: Check MX records (default: `true`)
|
|
20
|
+
- `check_a`: Check A records (default: `false`)
|
|
21
|
+
- `timeout`: DNS query timeout in seconds (default: `5`)
|
|
22
|
+
- `validate_format`: Validate email format (default: `true`)
|
|
23
|
+
- `validate_domain`: Validate domain (default: `true`)
|
|
24
|
+
|
|
25
|
+
### Other Options
|
|
26
|
+
|
|
27
|
+
- `normalize`: Normalize email before validation (default: `false`)
|
|
28
|
+
- `message`: Custom error message
|
|
29
|
+
|
|
30
|
+
## Examples
|
|
31
|
+
|
|
32
|
+
### Basic Validation with Domain Check
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
class User < ActiveRecord::Base
|
|
36
|
+
validates :email, domain_check: { check_mx: true, timeout: 3 }
|
|
37
|
+
end
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Format Validation Only
|
|
41
|
+
|
|
42
|
+
Skip domain check and only validate format:
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
class User < ActiveRecord::Base
|
|
46
|
+
validates :email, domain_check: { validate_domain: false }
|
|
47
|
+
end
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### With Automatic Normalization
|
|
51
|
+
|
|
52
|
+
Automatically normalize email addresses before validation:
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
class User < ActiveRecord::Base
|
|
56
|
+
validates :email, domain_check: { check_mx: true }, normalize: true
|
|
57
|
+
end
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### With Custom Error Message
|
|
61
|
+
|
|
62
|
+
Provide a custom error message:
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
class User < ActiveRecord::Base
|
|
66
|
+
validates :email, domain_check: { check_mx: true }, message: "Invalid email address"
|
|
67
|
+
end
|
|
68
|
+
```
|