Class: Async::DNS::System::Hosts

Inherits:
Object
  • Object
show all
Defined in:
lib/async/dns/system.rb

Overview

This code is very experimental

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHosts

Returns a new instance of Hosts.



43
44
45
46
# File 'lib/async/dns/system.rb', line 43

def initialize
	@addresses = {}
	@names = {}
end

Instance Attribute Details

#addressesObject (readonly)

Returns the value of attribute addresses.



48
49
50
# File 'lib/async/dns/system.rb', line 48

def addresses
  @addresses
end

#namesObject (readonly)

Returns the value of attribute names.



49
50
51
# File 'lib/async/dns/system.rb', line 49

def names
  @names
end

Class Method Details

.localObject



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/async/dns/system.rb', line 87

def self.local
	hosts = self.new

	path = System::hosts_path

	if path and File.exist?(path)
		File.open(path) do |file|
			hosts.parse_hosts(file)
		end
	end

	return hosts
end

Instance Method Details

#add(address, names) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/async/dns/system.rb', line 68

def add(address, names)
	@addresses[address] ||= []
	@addresses[address] += names

	names.each do |name|
		@names[name] ||= []
		@names[name] << address
	end
end

#call(name) ⇒ Object

This is used to match names against the list of known hosts:



52
53
54
# File 'lib/async/dns/system.rb', line 52

def call(name)
	@names.include?(name)
end

#lookup(name) ⇒ Object Also known as: []



56
57
58
59
60
61
62
63
64
# File 'lib/async/dns/system.rb', line 56

def lookup(name)
	addresses = @names[name]

	if addresses
		addresses.last
	else
		nil
	end
end

#parse_hosts(io) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/async/dns/system.rb', line 78

def parse_hosts(io)
	io.each do |line|
		line.sub!(/#.*/, '')
		address, hostname, *aliases = line.split(/\s+/)

		add(address, [hostname] + aliases)
	end
end