Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby DNS Check
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
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