forked from weppos/whois
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The class starts to take form. Creates basic server class, added one …
…simple benchmark and some preliminary tests.
- Loading branch information
Showing
7 changed files
with
153 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
#++ | ||
|
||
|
||
class Whois | ||
module Whois | ||
|
||
module Version | ||
MAJOR = 0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |