Class: Ddig::Nameserver

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nameservers: nil) ⇒ Nameserver

Returns a new instance of Nameserver.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ddig/nameserver.rb', line 7

def initialize(nameservers: nil)
  @nameservers = nameservers

  if @nameservers.nil?
    @servers = default_servers
  elsif @nameservers.is_a?(Array)
    @servers = @nameservers
  else
    @servers = [@nameservers]
  end

  validation_servers
end

Instance Attribute Details

#serversObject (readonly)

Returns the value of attribute servers.



5
6
7
# File 'lib/ddig/nameserver.rb', line 5

def servers
  @servers
end

Instance Method Details

#default_serversObject



29
30
31
# File 'lib/ddig/nameserver.rb', line 29

def default_servers
  Resolv::DNS::Config.default_config_hash[:nameserver]
end

#servers_ipv4Object



33
34
35
36
37
38
39
# File 'lib/ddig/nameserver.rb', line 33

def servers_ipv4
  @servers.map do |nameserver|
    if IPAddr.new(nameserver).ipv4?
      nameserver
    end
  end.compact
end

#servers_ipv6Object



41
42
43
44
45
46
47
# File 'lib/ddig/nameserver.rb', line 41

def servers_ipv6
  @servers.map do |nameserver|
    if IPAddr.new(nameserver).ipv6?
      nameserver
    end
  end.compact
end

#validation_serversObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ddig/nameserver.rb', line 49

def validation_servers
  @servers.uniq.each do |server|
    addr = IPAddr.new(server) rescue nil
    if addr.nil?
      puts "Warning: nameservers has invalid ip address (nameserver: #{server})"
      @servers.delete(server)
    end
  end

  if @servers.count.zero?
    return false
  end

  return true
end