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