Whois Client

The Whois::Client class is the library controller. It is responsible for receiving the WHOIS query request, invoking the Whois::Server and returning the Whois::Answer.

Basics

In order to send a WHOIS query, first you need to create a WHOIS client as follows.

# Example:  Creating a Whois client

c = Whois::Client.new

Now, call the #query method to perform a WHOIS query passing the object you want to query as parameter.

# Example:  Sending a WHOIS query for google.com domain

c = Whois::Client.new
puts c.query("google.com")

The object can be any supported object, including domains, IPv4 and IPv6 addresses.

# Example:  Supported Object Types

c = Whois::Client.new

puts c.query("google.com")           # Domain
puts c.query("209.85.129.104")       # IPv4
puts c.query("2a00:1450:8006::63")   # IPv6

The Whois::Client is idempotent. You can use the same client to perform as many query as you want without any side effect.

# Example:  Executing multiple calls with the same Whois::Client

c = Whois::Client.new

puts c.query("google.com")
puts c.query("google.it")
puts c.query("google.org")
puts c.query("209.85.129.104")

Whois::Client#new also supports blocks.

# Example:  Executing multiple calls with the same Whois::Client using the block.

c = Whois::Client.new do |c|
  puts c.query("google.com")
  puts c.query("google.it")
  puts c.query("google.org")
  puts c.query("209.85.129.104")
end

Whois::Client.query always returns a Whois::Answer and success or raises an Exception on failure.

Options

Timeout

By default, each query run though the Whois::Client has a timeout value of 10 seconds. If the execution exceeds timeout limit, the client raises a Timeout::Error exception.

Off course, you can customize the timeout value setting a different value. If timeout is nil, the client will wait until the response is sent back from the server or the process is killed. Don’t disable the timeout unless you really know what you are doing!

# Example:  Changing the timeout value for a WHOIS query

c = Whois::Client.new(:timeout => 20)
c.timeout # => 20
c.timeout = 5
c.timeout # => 5

c.query("google.com")

Shortcuts

The most essential Whois features are also available as a convenient module functions in the Whois module.

Whois.query is equal to invoking the #query method on a Whois::Client instance.

# Example:  Sending a WHOIS query using an explicit or implicit WHOIS::Client

# 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")

Whois.available? and Whois.registered? are equal to performing a WHOIS request for given object and invoking the corresponding available? and registered? on the returned Whois::Answer.

# Example:  Sending a WHOIS query and checking for domain availability
#           using an explicit or implicit WHOIS::Client

# With an explicit Whois::Client
c = Whois::Client.new
c.query("google.com").available?
c.query("google.com").registered?

# With an implicit Whois::Client
Whois.available?("google.com")
Whois.registered?("google.com")