- 
      
 - 
        
Save tpal94/13d3249c01311c42d93e73fb549dbca7 to your computer and use it in GitHub Desktop.  
    Ruby DNS Check
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | require 'resolv' | |
| class DnsCheck | |
| attr_reader :host | |
| def initialize(host) | |
| @host = host | |
| end | |
| def a | |
| @a ||= Resolv::DNS.new.getresources(host, Resolv::DNS::Resource::IN::A) | |
| end | |
| def a? | |
| a.any? | |
| end | |
| def mx | |
| @mx ||= Resolv::DNS.new.getresources(host, Resolv::DNS::Resource::IN::MX) | |
| end | |
| def mx? | |
| mx.any? | |
| end | |
| def ns | |
| @ns ||= Resolv::DNS.new.getresources(host, Resolv::DNS::Resource::IN::NS) | |
| end | |
| def ns? | |
| ns.any? | |
| end | |
| end | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | require 'testrocket' | |
| require './dns_check' | |
| !-> { 'It returns true / record sets for legit domains' } | |
| +-> { DnsCheck.new('github.com').a? } | |
| +-> { DnsCheck.new('github.com').a.any? } | |
| +-> { DnsCheck.new('github.com').mx? } | |
| +-> { DnsCheck.new('github.com').mx.any? } | |
| +-> { DnsCheck.new('github.com').ns? } | |
| +-> { DnsCheck.new('github.com').ns.any? } | |
| !-> { 'It returns false / empty arrays for invalid domains' } | |
| --> { DnsCheck.new('boomthisisinvalid.com').a? } | |
| +-> { DnsCheck.new('boomthisisinvalid.com').a.empty? } | |
| --> { DnsCheck.new('boomthisisinvalid.com').mx? } | |
| +-> { DnsCheck.new('boomthisisinvalid.com').mx.empty? } | |
| --> { DnsCheck.new('boomthisisinvalid.com').ns? } | |
| +-> { DnsCheck.new('boomthisisinvalid.com').ns.empty? } | |
| --> { DnsCheck.new('not even a domain 💩').a? } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment