Basic Usage
Performing a WHOIS query
This is the simplest way to send a WHOIS request is using the query method.
c = Whois::Client.new
c.query("google.com")
# => #<Whois::Answer>
The method returns a Whois::Answer instance which essentially looks and behaves like a String but it’s way more powerful than a string.
You can print, compare and modify it exactly as a string.
c = Whois::Client.new
a = c.query("google.com")
puts a
# the full whois record response
# ...
# ...
Whois provides the ability to get WHOIS information for domains, IPv4 and IPv6 IP addresses. The client is smart enough to guess the best WHOIS server according to given query, send the request and return the response.
c = Whois::Client.new
c.query("google.com")
# => #<Whois::Answer ...>
# IPv4 Whois
c.query("74.125.67.100")
# => #<Whois::Answer ...>
# IPv6 Whois
c.query("2001:db8::1428:57ab")
# => #<Whois::Answer ...>
The query method is stateless. For this reason, you can safely re-use the same client instance for multiple queries.
w = Whois::Client.new
w.query("google.com")
w.query("74.125.67.100")
w.query("2001:db8::1428:57ab")
w.query("google.it")
Configuring the Client
The standard Whois::Client works fine for most of the cases but if you want more control over the request, you can access the underlying Whois::Client object and use it directly.
# Use a Whois::Client
c = Whois::Client.new
c.query("google.com")
# Use a Whois::Client
# passing a custom timeout value
c = Whois::Client.new(:timeout => 30)
c.query("google.com")
Shortcuts
The Whois module provides some convenient shortcuts. One of these is the Whois.query method. It creates a new client and calls the #query method on it. The following two statements are equivalent:
# WHOIS request with an explicit Whois::Client
c = Whois::Client.new
c.query("google.com")
# WHOIS request with an implicit Whois::Client
c = Whois.query("google.com")
Read the Whois::Client shortcuts section to learn more about all available shortcuts.
Consuming the Answer
A WHOIS query returns a Whois::Answer instance and this is just the beginning of the story.
Whois::Answer is a super powerful object. It encapsulates the full WHOIS answer and it provides you the ability to parse the WHOIS response with a full object oriented notation.
a = Whois.whois("google.it")
# => #<Whois::Answer ...>
a.available?
# => false
a.registered?
# => true
a.created_on
# => Fri Dec 10 00:00:00 +0100 1999
t = a.technical
# => #<Whois::Answer::Contact ...>
t.id
# => "TS7016-ITNIC"
t.name
# => "Technical Services"
a.nameservers.each do |nameserver|
puts nameserver
end
This feature is made possible by the Whois::Answer::Parser. Unfortunately, due to the lack of a global standard, each WHOIS server requires a specific parser. For this reason, the library doesn’t currently support all existing WHOIS servers.
If you create a new parser, please consider releasing it to the public so that it can be included in a next version.