Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

improve error handling, refactor

  • Loading branch information...
commit 731d08228af618dd67e313add24b8fbce681c498 1 parent 5c19cbf
@NARKOZ authored
Showing with 9 additions and 14 deletions.
  1. +1 −5 bin/whoaz
  2. +4 −3 lib/whoaz/errors.rb
  3. +4 −6 lib/whoaz/whois.rb
View
6 bin/whoaz
@@ -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
View
7 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
View
10 lib/whoaz/whois.rb
@@ -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)
@@ -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
@@ -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),
@@ -48,6 +45,7 @@ def initialize(domain)
end
@nameservers.try(:compact!)
+ @name, @organization = @organization, nil if @name.nil?
end
def free?
Please sign in to comment.
Something went wrong with that request. Please try again.