Class: Whois::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/whois/client.rb

Constant Summary collapse

DEFAULT_TIMEOUT =

The maximum time to run a WHOIS query, expressed in seconds.

Returns:

  • (Fixnum)

    Timeout value in seconds.

10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) {|self| ... } ⇒ Client

Initializes a new Whois::Client with settings.

If block is given, yields self.

Examples:

Creating a new Client

client = Whois::Client.new
client.lookup("google.com")

Creating a new Client with custom settings

client = Whois::Client.new(:timeout => nil)
client.lookup("google.com")

Creating a new Client an yield the instance

Whois::Client.new do |c|
  c.lookup("google.com")
end

Binding the requests to a custom local IP

client = Whois::Client.new(:bind_host => "127.0.0.1", :bind_port => 80)
client.lookup("google.com")

Parameters:

  • settings (Hash) (defaults to: {})

    Hash of settings to customize the client behavior.

Options Hash (settings):

  • :timeout (Integer, nil) — default: DEFAULT_TIMEOUT

    The timeout for a WHOIS query, expressed in seconds.

  • :bind_host (String) — default: nil

    Providing an IP address or hostname will bind the Socket connection to the specific local host.

  • :bind_port (Fixnum) — default: nil

    Providing port number will bind the Socket connection to the specific local port.

  • :host (String, nil) — default: nil

    The server host to query. Leave it blank for intelligent detection.

  • :referral (Boolean, nil) — default: nil

    Set to false to disable queries to referral WHOIS servers.

Yields:

  • (self)

68
69
70
71
72
73
74
75
# File 'lib/whois/client.rb', line 68

def initialize(settings = {})
  settings = settings.dup

  self.timeout  = settings.key?(:timeout) ? settings.delete(:timeout) : DEFAULT_TIMEOUT
  self.settings = settings

  yield(self) if block_given?
end

Instance Attribute Details

#settingsHash

Returns The current client settings.

Returns:

  • (Hash)

    The current client settings.


28
29
30
# File 'lib/whois/client.rb', line 28

def settings
  @settings
end

#timeoutFixnum?

Returns The current timeout value, expressed in seconds.

Returns:

  • (Fixnum, nil)

    The current timeout value, expressed in seconds.


25
26
27
# File 'lib/whois/client.rb', line 25

def timeout
  @timeout
end

Instance Method Details

#lookup(object) ⇒ Whois::Record

Lookups the right WHOIS server for object and returns the response from the server.

Examples:


client.lookup("google.com")
# => #<Whois::Record>

Parameters:

  • object (#to_s)

    The string to be sent as lookup parameter.

Returns:

Raises:

  • (Timeout::Error)

91
92
93
94
95
96
97
98
# File 'lib/whois/client.rb', line 91

def lookup(object)
  string = object.to_s.downcase
  Timeout.timeout(timeout) do
    @server = Server.guess(string)
    @server.configure(settings)
    @server.lookup(string)
  end
end