/
client.rb
78 lines (63 loc) · 1.4 KB
/
client.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#
# = Ruby Whois
#
# An intelligent pure Ruby WHOIS client.
#
#
# Category:: Net
# Package:: Whois
# Author:: Simone Carletti <weppos@weppos.net>
# License:: MIT License
#
#--
#
#++
require 'socket'
require 'timeout'
module Whois
class Client
# The maximum time to run a whois query expressed in seconds
DEFAULT_TIMEOUT = 5
attr_accessor :timeout
#
# :call-seq:
# new { |client| ... } => client
# new(options = {}) { |client| ... } => client
#
# Initializes a new Whois::Client with <tt>options</tt>.
#
# If block is given, yields self.
#
# client = Whois::Client.new do |c|
# c.timeout = nil
# end
# client.query("google.com")
#
def initialize(options = {}, &block)
self.timeout = options[:timeout] || DEFAULT_TIMEOUT
yield(self) if block_given?
end
class Query # :nodoc
# IPv6?
# RPSL?
# email?
# NSIC?
# ASP32?
# IP?
# domain?
# network?
end
# Queries the right whois server for <tt>qstring</tt> and returns
# a <tt>Whois::Answer</tt> instance containing the response from the server.
#
# client.query("google.com")
# # => #<Whois::Answer>
#
def query(qstring)
Timeout::timeout(timeout) do
@server = Server.guess(qstring)
@server.query(qstring)
end
end
end
end