Skip to content

Instantly share code, notes, and snippets.

@samnissen
Created October 17, 2017 10:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samnissen/12962b78df0404cf774a89a94f792085 to your computer and use it in GitHub Desktop.
Save samnissen/12962b78df0404cf774a89a94f792085 to your computer and use it in GitHub Desktop.
Get WHOIS record data from both registry and registrar
require 'whois'
require 'whois-parser'
require 'addressable/uri'
require 'public_suffix'
class Registrar
class << self
def lookup(domain, timeout = 30)
tld = tld_domain(domain)
base_tld_definition = Whois::Server.definitions(:tld).map{|s| s if s[0] == tld}.compact.first
client = Whois::Client.new(timeout: timeout)
payload = {}
payload[:registry_results] = client.lookup(domain)
registrar_server = parse_whois_registrar_server(payload[:registry_results])
return payload unless registrar_server
Whois::Server.define :tld, tld, registrar_server
payload[:registrar_results] = client.lookup(domain)
return payload
ensure
Whois::Server.define :tld, tld, base_tld_definition[1] if base_tld_definition
end
private
def tld_domain(domain)
PublicSuffix.parse(extract_host(domain)).tld
end
def scheme_domain(domain)
return "https://#{domain}" unless Addressable::URI.parse(domain).scheme
domain
end
def extract_host(domain)
host = Addressable::URI.parse(scheme_domain("#{domain}")).host
return host if host
domain
end
def parse_whois_registrar_server(results)
results.parser.record.to_s.scan(/Registrar WHOIS Server: [^\s+]*/i).first.gsub(/Registrar WHOIS Server: /i,'').strip
rescue
nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment