Class: DomainExpiry::DomainExpiry

Inherits:
Object
  • Object
show all
Defined in:
lib/domain_expiry.rb

Overview

docs to follow

Class Method Summary collapse

Class Method Details

.available?(domain) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
# 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) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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') ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/domain_expiry.rb', line 16

def self.registered?(domain)
    whois = Whois::Client.new
    r = whois.lookup(domain).parser

    r.registered?
end