Skip to content

Commit

Permalink
The class starts to take form. Creates basic server class, added one …
Browse files Browse the repository at this point in the history
…simple benchmark and some preliminary tests.
  • Loading branch information
weppos committed Jul 1, 2009
1 parent afb4065 commit 0f8cc86
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 18 deletions.
10 changes: 9 additions & 1 deletion lib/whois.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@


require 'whois/version'
require 'whois/errors'
require 'whois/client'
require 'whois/server'


class Whois
module Whois

NAME = 'Whois'
GEM = 'whois'
AUTHORS = ['Simone Carletti <[email protected]>']

end

module Kernel
def whois(string)
Whois::Client.new.query(string)
end
end
28 changes: 12 additions & 16 deletions lib/whois/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,29 @@
require 'timeout'


class Whois
module Whois

class Client

class Error < StandardError; end
class ServerNotFound < Error; end
class Query
# IPv6?
# RPSL?
# email?
# NSIC?
# ASP32?
# IP?
# domain?
# network?
end


def query(string)
server = find_server(string)
server = Server.whichwhois(string)
ask_the_socket(string, server, 43)
end

private

def find_server(string)
self.class.servers.each do |ext, server|
return server if string.slice(-ext.size, ext.size) == ext
end
raise ServerNotFound, "Unable to find a WHOIS server for `#{string}'"
end

def ask_the_socket(query, server, port = 43)
client = TCPSocket.open(server, port)
client.write("#{query}\n") # I could use put(foo) and forget the \n
Expand All @@ -48,11 +49,6 @@ def ask_the_socket(query, server, port = 43)
client.close
end


def self.servers
@@servers ||= YAML.load_file(File.dirname(__FILE__) + "/servers.yml")
end

end

end
31 changes: 31 additions & 0 deletions lib/whois/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Whois

class Error < StandardError
end


# Generic Server exception class.
class ServerError < StandardError
end

# Raised when the class hasn't been able to select a valid server
# probably because definitions are outdated.
class ServerNotFound < ServerError
end

# Raised when a server is known to not be available for this kind of object
# or because this specific object doesn't support whois.
# This is the case of some specific domains that only provides
# a web–based whois interface. (\x03)
class ServerNotAvailable < ServerError
end

# Raised when no whois server is known for this kind of object. (\x05)
class ServerNotSupported < ServerError
end

# Raised when unknown AS numer of IP network. (\x06)
class ServerUnknown < ServerError
end

end
66 changes: 66 additions & 0 deletions lib/whois/server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#
# = Ruby Whois
#
# A pure Ruby WHOIS client.
#
#
# Category:: Net
# Package:: WWW::Delicious
# Author:: Simone Carletti <[email protected]>
# License:: MIT License
#
#--
#
#++


require 'yaml'


module Whois

class Server

# Parses an user-supplied string and tries to guess the right server.
def self.whichwhois(string)
# TODO: IPv6 address
# TODO: RPSL hierarchical objects

# Email Address
if string =~ /@/
find_for_email(string)
end

# TODO: NSI NIC
# TODO: ASN32
# TODO: IP

# Domain
find_for_domain(string)

# TODO: guessing
# TODO: game-over
end


def self.find_for_email(string)
raise ServerNotSupported, "No whois server is known for email objects"
end

def self.find_for_domain(string)
servers.each do |ext, server|
return server if string.slice(-ext.size, ext.size) == ext
end
# you might want to remove this raise statement
# to make the guessing more clever.
raise ServerNotFound, "Unable to find a WHOIS server for `#{string}'"
end

def self.servers
@@servers ||= YAML.load_file(File.dirname(__FILE__) + "/servers.yml")
end


end

end
2 changes: 1 addition & 1 deletion lib/whois/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#++


class Whois
module Whois

module Version
MAJOR = 0
Expand Down
20 changes: 20 additions & 0 deletions test/client_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'test_helper'

class ClientTest < Test::Unit::TestCase
include Whois

def setup
@client = Client.new
end

def test_query_with_email_address_should_raise
assert_raise(ServerNotSupported) { @client.query("[email protected]") }
end

def test_query_with_domain
response = @client.query("weppos.it")
assert_match /Domain:\s+weppos\.it/, response
assert_match /Created:/, response
end

end
14 changes: 14 additions & 0 deletions utils/bm_shell_vs_pure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
$:.unshift(File.dirname(__FILE__) + '/../lib')
require 'benchmark'
require 'whois'

DOMAINS = %w(weppos.it) * 5

Benchmark.bmbm do |x|
x.report("shell") do
DOMAINS.each { |d| `whois #{d}` }
end
x.report("pure") do
DOMAINS.each { |d| Whois::Client.new.query(d) }
end
end

0 comments on commit 0f8cc86

Please sign in to comment.