require 'domain_expiry/version' require 'whois' require 'whois-parser' # # Docs to follow # module DomainExpiry # # docs to follow # class DomainExpiry def self.registered?(domain) whois = Whois::Client.new r = whois.lookup(domain).parser r.registered? end def self.available?(domain) whois = Whois::Client.new r = whois.lookup(domain).parser r.available? end def self.domain_details(domains, date_format = '%d %b %Y') results = {} domains = domains.split(',') unless domains.is_a? whois = Whois::Client.new domains.each do |domain| begin whois_result = whois.lookup(domain).parser rescue Timeout::Error, Errno::ECONNRESET, Whois::ConnectionError results[domain] = { 'status' => 400, 'error' => 'Connection error' } next rescue Whois::ServerNotFound results[domain] = { 'status' => 400, 'error' => 'Server not found error' } next end begin if whois_result.registered? expires_on = DateTime.parse(whois_result.expires_on.to_s) num_days = (expires_on - DateTime.now).to_i results[domain] = { 'status' => 200, 'expires_on' => expires_on.strftime(date_format), 'expires_in' => num_days } else results[domain] = { 'status' => 400, 'error' => 'Unregistered domain' } end rescue StandardError results[domain] = results = { 'status' => 400, 'error' => 'Parsing error' } end end results.sort end def self.display_results(results, width = 120) delim = '-' * width puts(delim) printf(' %-30s | %s\n', 'Domain', status: 'Status') puts(delim) results.each do |domain, details| status = if details['status'] == 400 details['error'] else format('expires on %s (in %s days)', expires_in: details['expires_on'], expires_on: details['expires_in']) end printf(' %-30s | %s\n', domain, status) end puts(delim) end end end