Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
NARKOZ committed Mar 18, 2012
1 parent 9885493 commit 088b5da
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions lib/whoaz/whois.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,10 @@
module Whoaz module Whoaz
class Whois 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) def initialize(domain)
post_domain = domain.split('.', 2) post_domain = domain.split('.', 2)
Expand All @@ -13,23 +17,12 @@ def initialize(domain)
res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)} res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)}


if res.code.to_i == 200 if res.code.to_i == 200
doc = Nokogiri::HTML(res.body) Whois.page = Nokogiri::HTML(res.body)
else else
raise ServerError, "Server responded with code #{res.code}" raise ServerError, "Server responded with code #{res.code}"
end end


if doc.at_xpath('//table[4]/tr/td[2]/table[2]/tr[3]/td').try(:text).try(:strip) == 'This domain is free.' Whois.page.xpath('//table[4]/tr/td[2]/table[2]/td/table/tr').each do |registrant|
@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|
@organization = registrant.at_xpath('td[2]/table/tr[1]/td[2]').try(:text) @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) @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) @address = registrant.at_xpath('td[3]/table/tr[1]/td[2]').try(:text)
Expand All @@ -38,7 +31,7 @@ def initialize(domain)
@email = registrant.at_xpath('td[3]/table/tr[4]/td[2]').try(:text) @email = registrant.at_xpath('td[3]/table/tr[4]/td[2]').try(:text)
end 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 = [ @nameservers = [
nameserver.at_xpath('tr[2]/td[2]').try(:text), nameserver.at_xpath('tr[2]/td[2]').try(:text),
nameserver.at_xpath('tr[3]/td[2]').try(:text), nameserver.at_xpath('tr[3]/td[2]').try(:text),
Expand All @@ -53,14 +46,25 @@ def initialize(domain)


@nameservers.try(:compact!) @nameservers.try(:compact!)
@name, @organization = @organization, nil if @name.nil? @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 end


def free? 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 end


def registered? def registered?
!free? !free?
end 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
end end

0 comments on commit 088b5da

Please sign in to comment.