lib/whoaz/whois.rb in whoaz-1.0.0 vs lib/whoaz/whois.rb in whoaz-2.0.0

- old
+ new

@@ -1,66 +1,88 @@ module Whoaz class Whois - attr_accessor :name, :organization, :address, :phone, :fax, :email, :nameservers + # @return [String] The queried domain name. + attr_reader :domain - def initialize(domain) - post_domain = domain.split('.', 2) - raise InvalidDomain, "Invalid domain specified" unless - [MAIN_TLD, REGIONAL_TLD].any? {|a| a.include? post_domain.last} + # @return [String] The name of the registrant. + attr_reader :name - url = URI WHOIS_URL - req = Net::HTTP::Post.new(url.path, 'Referer' => WHOIS_REFERER) - req.set_form_data('lang' => 'en', 'domain' => post_domain.first, 'dom' => ".#{post_domain.last}") - res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)} + # @return [String] The organization of the registrant. + attr_reader :organization - if res.code.to_i == 200 - doc = Nokogiri::HTML(res.body) - else - raise ServerError, "Server responded with code #{res.code}" - end + # @return [String] The address of the registrant. + attr_reader :address - 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 + # @return [String] The phone of the registrant. + attr_reader :phone - 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 + # @return [String] The fax of the registrant. + attr_reader :fax - 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) - @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) - @phone = registrant.at_xpath('td[3]/table/tr[2]/td[2]').try(:text) - @fax = registrant.at_xpath('td[3]/table/tr[3]/td[2]').try(:text) - @email = registrant.at_xpath('td[3]/table/tr[4]/td[2]').try(:text) - end + # @return [String] The email of the registrant. + attr_reader :email - 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), - nameserver.at_xpath('tr[3]/td[2]').try(:text), - nameserver.at_xpath('tr[4]/td[2]').try(:text), - nameserver.at_xpath('tr[5]/td[2]').try(:text), - nameserver.at_xpath('tr[6]/td[2]').try(:text), - nameserver.at_xpath('tr[7]/td[2]').try(:text), - nameserver.at_xpath('tr[8]/td[2]').try(:text), - nameserver.at_xpath('tr[9]/td[2]').try(:text) - ] - end + # @return [Array] An array of nameservers. + attr_reader :nameservers - @nameservers.try(:compact!) - @name, @organization = @organization, nil if @name.nil? - end + # @return [Boolean] Availability of the domain. + attr_reader :free + alias_method :free?, :free + alias_method :available?, :free? - def free? - @free ? true : false + # Initializes a new Whois object. + # + # @param [String] domain The domain name required to query. + # @return [Whoaz::Whois] + def initialize(domain) + @domain = domain + + if raw_domain_info.include?('is not exists') || raw_domain_info.include?('cannot be registered') + @free = true + else + @free = false + domain_info = raw_domain_info.split("\n") + @nameservers = domain_info[3].sub('Name Servers:', '').strip.split(',') + @organization = domain_info[16].sub('Organisation:', '').strip + @name = domain_info[17].sub('Name:', '').strip + @address = domain_info[19..23].join("\n").strip + @phone = domain_info[12].sub('Voice phone:', '').strip + @fax = domain_info[13].sub('Fax:', '').strip + @email = domain_info[11].sub('Email:', '').strip + end end + # Checks if the domain name is registered or not. + # + # @return [Boolean] def registered? !free? + end + + # Returns raw domain info as responded by WHOIS server. + # + # @return [String] + def raw_domain_info + @raw_domain_info ||= query + end + + private + + def query + post_domain = @domain.split('.', 2) + raise InvalidDomain, "Invalid domain name is specified" unless + (MAIN_TLD + REGIONAL_TLD).include? post_domain.last + + url = URI WHOIS_URL + req = Net::HTTP::Post.new(url.path, 'Referer' => WHOIS_REFERER) + req.set_form_data(lang: 'en', domain: post_domain.first, dom: ".#{post_domain.last}") + res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req)} + + if res.code.to_i == 200 + page = Nokogiri::HTML res.body + page.at_xpath('//table[4]/tr/td[2]/table[2]/td[1]/pre').text.strip + else + raise ServerError, "WHOIS server responded with status code #{res.code}" + end end end end