Class: EmailDomainChecker::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/email_domain_checker/config.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  validate_format: true,
  validate_domain: true,
  check_mx: true,
  check_a: false,
  timeout: 5
}.freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/email_domain_checker/config.rb', line 124

def initialize
  @test_mode = self.class.test_mode || false
  @cache_enabled = self.class.cache_enabled.nil? ? true : self.class.cache_enabled
  @cache_type = self.class.cache_type || :memory
  @cache_ttl = self.class.cache_ttl || 3600
  @cache_adapter_instance = self.class.cache_adapter_instance
  @redis_client = self.class.redis_client
  @blacklist_domains = self.class.blacklist_domains || []
  @whitelist_domains = self.class.whitelist_domains || []
  @domain_checker = self.class.domain_checker
end

Class Attribute Details

.blacklist_domainsObject

Returns the value of attribute blacklist_domains.



16
17
18
# File 'lib/email_domain_checker/config.rb', line 16

def blacklist_domains
  @blacklist_domains
end

.cache_adapterObject

Returns the value of attribute cache_adapter.



16
17
18
# File 'lib/email_domain_checker/config.rb', line 16

def cache_adapter
  @cache_adapter
end

.cache_adapter_instanceObject

Returns the value of attribute cache_adapter_instance.



16
17
18
# File 'lib/email_domain_checker/config.rb', line 16

def cache_adapter_instance
  @cache_adapter_instance
end

.cache_enabledObject

Returns the value of attribute cache_enabled.



16
17
18
# File 'lib/email_domain_checker/config.rb', line 16

def cache_enabled
  @cache_enabled
end

.cache_ttlObject

Returns the value of attribute cache_ttl.



16
17
18
# File 'lib/email_domain_checker/config.rb', line 16

def cache_ttl
  @cache_ttl
end

.cache_typeObject

Returns the value of attribute cache_type.



16
17
18
# File 'lib/email_domain_checker/config.rb', line 16

def cache_type
  @cache_type
end

.default_optionsObject

Returns the value of attribute default_options.



16
17
18
# File 'lib/email_domain_checker/config.rb', line 16

def default_options
  @default_options
end

.domain_checkerObject

Returns the value of attribute domain_checker.



16
17
18
# File 'lib/email_domain_checker/config.rb', line 16

def domain_checker
  @domain_checker
end

.redis_clientObject

Returns the value of attribute redis_client.



16
17
18
# File 'lib/email_domain_checker/config.rb', line 16

def redis_client
  @redis_client
end

.test_modeObject

Returns the value of attribute test_mode.



16
17
18
# File 'lib/email_domain_checker/config.rb', line 16

def test_mode
  @test_mode
end

.whitelist_domainsObject

Returns the value of attribute whitelist_domains.



16
17
18
# File 'lib/email_domain_checker/config.rb', line 16

def whitelist_domains
  @whitelist_domains
end

Instance Attribute Details

#blacklist_domainsObject

Returns the value of attribute blacklist_domains.



122
123
124
# File 'lib/email_domain_checker/config.rb', line 122

def blacklist_domains
  @blacklist_domains
end

#cache_adapter_instanceObject

Returns the value of attribute cache_adapter_instance.



122
123
124
# File 'lib/email_domain_checker/config.rb', line 122

def cache_adapter_instance
  @cache_adapter_instance
end

#cache_enabledObject

Returns the value of attribute cache_enabled.



122
123
124
# File 'lib/email_domain_checker/config.rb', line 122

def cache_enabled
  @cache_enabled
end

#cache_ttlObject

Returns the value of attribute cache_ttl.



122
123
124
# File 'lib/email_domain_checker/config.rb', line 122

def cache_ttl
  @cache_ttl
end

#cache_typeObject

Returns the value of attribute cache_type.



122
123
124
# File 'lib/email_domain_checker/config.rb', line 122

def cache_type
  @cache_type
end

#domain_checkerObject

Returns the value of attribute domain_checker.



122
123
124
# File 'lib/email_domain_checker/config.rb', line 122

def domain_checker
  @domain_checker
end

#redis_clientObject

Returns the value of attribute redis_client.



122
123
124
# File 'lib/email_domain_checker/config.rb', line 122

def redis_client
  @redis_client
end

#test_modeObject

Returns the value of attribute test_mode.



122
123
124
# File 'lib/email_domain_checker/config.rb', line 122

def test_mode
  @test_mode
end

#whitelist_domainsObject

Returns the value of attribute whitelist_domains.



122
123
124
# File 'lib/email_domain_checker/config.rb', line 122

def whitelist_domains
  @whitelist_domains
end

Class Method Details

.cache_enabled?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/email_domain_checker/config.rb', line 52

def cache_enabled?
  @cache_enabled == true
end

.clear_cacheObject



78
79
80
# File 'lib/email_domain_checker/config.rb', line 78

def clear_cache
  cache_adapter&.clear
end

.clear_cache_for_domain(domain) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/email_domain_checker/config.rb', line 82

def clear_cache_for_domain(domain)
  return unless cache_enabled?

  adapter = cache_adapter
  return unless adapter

  # Clear both MX and A record cache entries
  dns_cache_keys_for_domain(domain).each do |key|
    adapter.delete(key)
  end
end

.configure(options = {}, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/email_domain_checker/config.rb', line 18

def configure(options = {}, &block)
  if block_given?
    config_instance = new
    block.call(config_instance)
    @default_options = DEFAULT_OPTIONS.merge(options)
    config_instance
  else
    @default_options = DEFAULT_OPTIONS.merge(options)
    new
  end
end

.resetObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/email_domain_checker/config.rb', line 30

def reset
  @default_options = DEFAULT_OPTIONS.dup
  @test_mode = false
  @cache_enabled = true
  @cache_type = :memory
  @cache_ttl = 3600
  @cache_adapter = nil
  @cache_adapter_instance = nil
  @redis_client = nil
  @blacklist_domains = []
  @whitelist_domains = []
  @domain_checker = nil
end

.reset_cache_adapter_if_neededObject



94
95
96
97
98
99
# File 'lib/email_domain_checker/config.rb', line 94

def reset_cache_adapter_if_needed
  # Reset cache adapter when changing cache type (unless custom instance is set)
  @cache_adapter = nil unless @cache_adapter_instance
  # Clear cache_adapter_instance if setting a type (Symbol or String), not a Class
  @cache_adapter_instance = nil if @cache_type.is_a?(Symbol) || @cache_type.is_a?(String)
end

.reset_cache_adapter_if_redisObject



105
106
107
# File 'lib/email_domain_checker/config.rb', line 105

def reset_cache_adapter_if_redis
  @cache_adapter = nil if @cache_type == :redis
end

.reset_cache_adapter_on_enabled_change(new_value, old_value) ⇒ Object



101
102
103
# File 'lib/email_domain_checker/config.rb', line 101

def reset_cache_adapter_on_enabled_change(new_value, old_value)
  @cache_adapter = nil if new_value != old_value
end

.test_mode?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/email_domain_checker/config.rb', line 48

def test_mode?
  @test_mode == true
end

.validate_cache_adapter_instance!(instance) ⇒ Object



109
110
111
112
113
# File 'lib/email_domain_checker/config.rb', line 109

def validate_cache_adapter_instance!(instance)
  unless instance.nil? || instance.is_a?(Cache::BaseAdapter)
    raise ArgumentError, "cache_adapter_instance must be an instance of EmailDomainChecker::Cache::BaseAdapter or nil"
  end
end