class ValidEmail2::Dns
Constants
- CACHE
- CacheEntry
- MAX_CACHE_SIZE
Public Class Methods
Source
# File lib/valid_email2/dns.rb, line 20 def initialize(dns_timeout = 5, dns_nameserver = nil) @dns_timeout = dns_timeout @dns_nameserver = dns_nameserver end
Source
# File lib/valid_email2/dns.rb, line 10 def self.prune_cache entries_sorted_by_cached_at_asc = CACHE.sort_by { |key, data| data.cached_at } entries_to_remove = entries_sorted_by_cached_at_asc.first(CACHE.size - MAX_CACHE_SIZE) entries_to_remove.each { |key, _value| CACHE.delete(key) } end
Public Instance Methods
Source
# File lib/valid_email2/dns.rb, line 29 def a_servers(domain) fetch(domain, Resolv::DNS::Resource::IN::A) end
Source
# File lib/valid_email2/dns.rb, line 25 def mx_servers(domain) fetch(domain, Resolv::DNS::Resource::IN::MX) end
Private Instance Methods
Source
# File lib/valid_email2/dns.rb, line 39 def fetch(domain, type) prune_cache if CACHE.size > MAX_CACHE_SIZE domain = domain.downcase cache_key = [domain, type] cache_entry = CACHE[cache_key] if cache_entry && Time.now - cache_entry.cached_at < cache_entry.ttl return cache_entry.records else CACHE.delete(cache_key) end records = Resolv::DNS.open(resolv_config) do |dns| dns.timeouts = @dns_timeout dns.getresources(domain, type) end if records.any? ttl = records.map(&:ttl).min CACHE[cache_key] = CacheEntry.new(records, Time.now, ttl) end records end
Source
# File lib/valid_email2/dns.rb, line 35 def prune_cache self.class.prune_cache end
Source
# File lib/valid_email2/dns.rb, line 65 def resolv_config config = Resolv::DNS::Config.default_config_hash config[:nameserver] = @dns_nameserver if @dns_nameserver config end