Browse files

cleanup code

  • Loading branch information...
1 parent 9885493 commit 088b5da0bf8ed88406d763dca5910fdf5f9d5535 @NARKOZ committed Mar 18, 2012
Showing with 20 additions and 16 deletions.
  1. +20 −16 lib/whoaz/whois.rb
View
36 lib/whoaz/whois.rb
@@ -1,6 +1,10 @@
module Whoaz
class Whois
- attr_accessor :name, :organization, :address, :phone, :fax, :email, :nameservers
+ attr_reader :name, :organization, :address, :phone, :fax, :email, :nameservers
+
+ class << self
+ attr_accessor :page
+ end
def initialize(domain)
post_domain = domain.split('.', 2)
@@ -13,23 +17,12 @@ def initialize(domain)
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)}
if res.code.to_i == 200
- doc = Nokogiri::HTML(res.body)
+ Whois.page = Nokogiri::HTML(res.body)
else
raise ServerError, "Server responded with code #{res.code}"
end
- 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
-
- if @name.nil? && @organization.nil?
- if doc.at_xpath('//table[4]/tr/td[2]/table[2]/td/p').
- try(:text).try(:strip) == 'Using of domain names contains less than 3 symbols is not allowed'
- raise DomainNameError, "Whois query for this domain name is not supported."
- end
- end
-
- doc.xpath('//table[4]/tr/td[2]/table[2]/td/table/tr').each do |registrant|
+ Whois.page.xpath('//table[4]/tr/td[2]/table[2]/td/table/tr').each do |registrant|
@organization = registrant.at_xpath('td[2]/table/tr[1]/td[2]').try(:text)
@name = registrant.at_xpath('td[2]/table/tr[2]/td[2]').try(:text)
@address = registrant.at_xpath('td[3]/table/tr[1]/td[2]').try(:text)
@@ -38,7 +31,7 @@ def initialize(domain)
@email = registrant.at_xpath('td[3]/table/tr[4]/td[2]').try(:text)
end
- doc.xpath('//table[4]/tr/td[2]/table[2]/td/table/tr/td[4]/table').each do |nameserver|
+ Whois.page.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),
nameserver.at_xpath('tr[3]/td[2]').try(:text),
@@ -53,14 +46,25 @@ def initialize(domain)
@nameservers.try(:compact!)
@name, @organization = @organization, nil if @name.nil?
+
+ if @name.nil? && @organization.nil?
+ raise DomainNameError, "Whois query for this domain name is not supported." if not_supported_domain? Whois.page
+ end
end
def free?
- @free ? true : false
+ Whois.page.at_xpath('//table[4]/tr/td[2]/table[2]/tr[3]/td').try(:text).try(:strip) == 'This domain is free.'
end
def registered?
!free?
end
+
+ private
+
+ def not_supported_domain?(page)
+ page.at_xpath('//table[4]/tr/td[2]/table[2]/td/p').try(:text).
+ try(:strip) == 'Using of domain names contains less than 3 symbols is not allowed'
+ end
end
end

0 comments on commit 088b5da

Please sign in to comment.