Permalink
Browse files

The class starts to take form. Creates basic server class, added one …

…simple benchmark and some preliminary tests.
  • Loading branch information...
1 parent afb4065 commit 0f8cc86c86ec944506a1a21c843a823bf1a35ad4 @weppos committed Jul 1, 2009
Showing with 153 additions and 18 deletions.
  1. +9 −1 lib/whois.rb
  2. +12 −16 lib/whois/client.rb
  3. +31 −0 lib/whois/errors.rb
  4. +66 −0 lib/whois/server.rb
  5. +1 −1 lib/whois/version.rb
  6. +20 −0 test/client_test.rb
  7. +14 −0 utils/bm_shell_vs_pure.rb
View
10 lib/whois.rb
@@ -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 <weppos@weppos.net>']
+
+end
+module Kernel
+ def whois(string)
+ Whois::Client.new.query(string)
+ end
end
View
28 lib/whois/client.rb
@@ -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
@@ -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
View
31 lib/whois/errors.rb
@@ -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
View
66 lib/whois/server.rb
@@ -0,0 +1,66 @@
+#
+# = Ruby Whois
+#
+# A pure Ruby WHOIS client.
+#
+#
+# Category:: Net
+# Package:: WWW::Delicious
+# Author:: Simone Carletti <weppos@weppos.net>
+# 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
View
2 lib/whois/version.rb
@@ -14,7 +14,7 @@
#++
-class Whois
+module Whois
module Version
MAJOR = 0
View
20 test/client_test.rb
@@ -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("weppos@weppos.net") }
+ end
+
+ def test_query_with_domain
+ response = @client.query("weppos.it")
+ assert_match /Domain:\s+weppos\.it/, response
+ assert_match /Created:/, response
+ end
+
+end
View
14 utils/bm_shell_vs_pure.rb
@@ -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.