A /etc/hosts parser. Also supports writing groups of data to the file.
Methods
Public Class methods
[ show source ]
# File lib/phusion_passenger/utils/hosts_file_parser.rb, line 30
30: def self.flush_dns_cache!
31: if RUBY_PLATFORM =~ /darwin/
32: system("dscacheutil -flushcache")
33: end
34: end
[ show source ]
# File lib/phusion_passenger/utils/hosts_file_parser.rb, line 36
36: def initialize(filename_or_io = "/etc/hosts")
37: if filename_or_io.respond_to?(:readline)
38: read_and_parse(filename_or_io)
39: else
40: File.open(filename_or_io, "rb") do |f|
41: read_and_parse(f)
42: end
43: end
44: end
Public Instance methods
[ show source ]
# File lib/phusion_passenger/utils/hosts_file_parser.rb, line 67
67: def add_group_data(marker, data)
68: begin_index = find_line(0, "###### BEGIN #{marker} ######")
69: end_index = find_line(begin_index + 1, "###### END #{marker} ######") if begin_index
70: if begin_index && end_index
71: @lines[begin_index + 1 .. end_index - 1] = data.split("\n")
72: else
73: @lines << "###### BEGIN #{marker} ######"
74: @lines.concat(data.split("\n"))
75: @lines << "###### END #{marker} ######"
76: end
77: end
[ show source ]
# File lib/phusion_passenger/utils/hosts_file_parser.rb, line 50
50: def host_count
51: return @host_names.size
52: end
[ show source ]
# File lib/phusion_passenger/utils/hosts_file_parser.rb, line 46
46: def ip_count
47: return @ips.size
48: end
[ show source ]
# File lib/phusion_passenger/utils/hosts_file_parser.rb, line 54
54: def resolve(host_name)
55: if host_name.downcase == "localhost"
56: return "127.0.0.1"
57: else
58: return @host_names[host_name.downcase]
59: end
60: end
[ show source ]
# File lib/phusion_passenger/utils/hosts_file_parser.rb, line 62
62: def resolves_to_localhost?(hostname)
63: ip = resolve(hostname)
64: return ip == "127.0.0.1" || ip == "::1" || ip == "0.0.0.0"
65: end
[ show source ]
# File lib/phusion_passenger/utils/hosts_file_parser.rb, line 79
79: def write(io)
80: @lines.each do |line|
81: io.puts(line)
82: end
83: end