Skip to content

Commit

Permalink
improve error handling, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
NARKOZ committed Jan 29, 2012
1 parent 5c19cbf commit 731d082
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
6 changes: 1 addition & 5 deletions bin/whoaz
Expand Up @@ -35,11 +35,7 @@ domain = ARGV.first

begin
query = Whoaz.whois domain
rescue Whoaz::EmptyDomain => e
abort e.message
rescue Whoaz::InvalidDomain => e
abort e.message
rescue Whoaz::UnknownError => e
rescue Whoaz::Error => e
abort e.message
end

Expand Down
7 changes: 4 additions & 3 deletions lib/whoaz/errors.rb
@@ -1,5 +1,6 @@
module Whoaz
class EmptyDomain < ArgumentError; end
class InvalidDomain < ArgumentError; end
class UnknownError < StandardError; end
class Error < StandardError; end
class EmptyDomain < Error; end
class InvalidDomain < Error; end
class ServerError < Error; end
end
10 changes: 4 additions & 6 deletions lib/whoaz/whois.rb
Expand Up @@ -5,7 +5,7 @@ class Whois
def initialize(domain)
post_domain = domain.split('.', 2)
raise InvalidDomain, "Invalid domain specified" unless
MAIN_TLD.include?(post_domain.last) || REGIONAL_TLD.include?(post_domain.last)
[MAIN_TLD, REGIONAL_TLD].any? {|a| a.include? post_domain.last}

url = URI WHOIS_URL
req = Net::HTTP::Post.new(url.path, 'Referer' => WHOIS_REFERER)
Expand All @@ -15,10 +15,10 @@ def initialize(domain)
if res.code.to_i == 200
doc = Nokogiri::HTML(res.body)
else
raise UnknownError, "Server responded with code #{res.code}"
raise ServerError, "Server responded with code #{res.code}"
end

if doc.at_xpath('//table[4]/tr/td[2]/table[2]/tr[3]/td[1]').try(:text).try(:strip) == 'This domain is free.'
if doc.at_xpath('//table[4]/tr/td[2]/table[2]/tr[3]/td').try(:text).try(:strip) == 'This domain is free.'
@free = true
end

Expand All @@ -31,9 +31,6 @@ def initialize(domain)
@email = registrant.at_xpath('td[3]/table/tr[4]/td[2]').try(:text)
end

@name ||= @organization
@organization = nil if @name.eql? @organization

doc.xpath('//table[4]/tr/td[2]/table[2]/td/table/tr/td[4]/table').each do |nameserver|
@nameservers = [
nameserver.at_xpath('tr[2]/td[2]').try(:text),
Expand All @@ -48,6 +45,7 @@ def initialize(domain)
end

@nameservers.try(:compact!)
@name, @organization = @organization, nil if @name.nil?
end

def free?
Expand Down

0 comments on commit 731d082

Please sign in to comment.