class DomainExpiry::DomainExpiry

docs to follow

Public Class Methods

available?(domain) click to toggle source
# File lib/domain_expiry.rb, line 23
def self.available?(domain)
    whois = Whois::Client.new
    r = whois.lookup(domain).parser

    r.available?
end
display_results(results, width = 120) click to toggle source
# File lib/domain_expiry.rb, line 63
def self.display_results(results, width = 120)
    delim = '-' * width

    puts(delim)
    printf(" %-30<header1>s | %<header2>s\n", header1: 'Domain', header2: 'Status')
    puts(delim)

    results.each do |domain, details|
        status = if details['status'] == 400
                     details['error']
                 else
                     format('expires on %<expires_in>s (in %<expires_on>s days)', expires_in: details['expires_on'], expires_on: details['expires_in'])
                 end
        printf(" %-30<header1>s | %<header2>s\n", header1: domain, header2: status)
    end
    puts(delim)
end
domain_details(domains, date_format = '%d %b %Y') click to toggle source
# File lib/domain_expiry.rb, line 30
def self.domain_details(domains, date_format = '%d %b %Y')
    results = {}

    domains = domains.split(',') unless domains.is_a?(Array)
    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
registered?(domain) click to toggle source
# File lib/domain_expiry.rb, line 16
def self.registered?(domain)
    whois = Whois::Client.new
    r = whois.lookup(domain).parser

    r.registered?
end