### Home Classes Methods

### In Files

- lib/phusion_passenger/utils/hosts_file_parser.rb

### Parent

Object

### Methods

- ::flush_dns_cache!
- ::new
- #add_group_data
- #host_count
- #ip_count
- #resolve
- #resolves_to_localhost?
- #write

### Class/Module Index

Quicksearch

- PhusionPassenger
- PhusionPassenger::AbstractInstaller
- PhusionPassenger::AbstractInstaller::Abort
- PhusionPassenger::AbstractInstaller::CommandError
- PhusionPassenger::AdminTools
- PhusionPassenger::AdminTools::MemoryStats
- PhusionPassenger::AdminTools::MemoryStats::Process
- PhusionPassenger::AdminTools::ServerInstance
- PhusionPassenger::AdminTools::ServerInstance::CorruptedDirectoryError
- PhusionPassenger::AdminTools::ServerInstance::GenerationsAbsentError
- PhusionPassenger::AdminTools::ServerInstance::Group
- PhusionPassenger::AdminTools::ServerInstance::Process
- PhusionPassenger::AdminTools::ServerInstance::RoleDeniedError
- PhusionPassenger::AdminTools::ServerInstance::StaleDirectoryError
- PhusionPassenger::AdminTools::ServerInstance::Stats
- PhusionPassenger::AdminTools::ServerInstance::UnsupportedGenerationStructureVersionError
- PhusionPassenger::AnalyticsLogger
- PhusionPassenger::AnalyticsLogger::Connection
- PhusionPassenger::AnalyticsLogger::Lock
- PhusionPassenger::AnalyticsLogger::Log
- PhusionPassenger::ClassicRails
- PhusionPassenger::ClassicRails::CGIFixed
- PhusionPassenger::ClassicRails::ThreadHandlerExtension
- PhusionPassenger::ConsoleTextTemplate
- PhusionPassenger::DebugLogging
- PhusionPassenger::LoaderSharedHelpers
- PhusionPassenger::MessageChannel
- PhusionPassenger::MessageChannel::InvalidHashError
- PhusionPassenger::MessageClient
- PhusionPassenger::NativeSupportLoader
- PhusionPassenger::Packaging
- PhusionPassenger::PlatformInfo
- PhusionPassenger::PlatformInfo::Depcheck
- PhusionPassenger::PlatformInfo::Depcheck::ConsoleRunner
- PhusionPassenger::PlatformInfo::Depcheck::Dependency
- PhusionPassenger::PlatformInfo::RuntimeError
- PhusionPassenger::Plugin
- PhusionPassenger::PreloaderSharedHelpers
- PhusionPassenger::Rack
- PhusionPassenger::Rack::OutOfBandGc
- PhusionPassenger::Rack::ThreadHandlerExtension
- PhusionPassenger::Rails3Extensions
- PhusionPassenger::Rails3Extensions::AnalyticsLogging
- PhusionPassenger::Rails3Extensions::AnalyticsLogging::ACExtension
- PhusionPassenger::Rails3Extensions::AnalyticsLogging::ASBenchmarkableExtension
- PhusionPassenger::Rails3Extensions::AnalyticsLogging::ExceptionLogger
- PhusionPassenger::RequestHandler
- PhusionPassenger::RequestHandler::ThreadHandler
- PhusionPassenger::RequestHandler::ThreadHandler::Interrupted
- PhusionPassenger::SharedConstants
- PhusionPassenger::Standalone
- PhusionPassenger::Standalone::AppFinder
- PhusionPassenger::Standalone::Command
- PhusionPassenger::Standalone::ConfigFile
- PhusionPassenger::Standalone::ConfigFile::DisallowedContextError
- PhusionPassenger::Standalone::HelpCommand
- PhusionPassenger::Standalone::Main
- PhusionPassenger::Standalone::PackageRuntimeCommand
- PhusionPassenger::Standalone::RuntimeInstaller
- PhusionPassenger::Standalone::StartCommand
- PhusionPassenger::Standalone::StatusCommand
- PhusionPassenger::Standalone::StopCommand
- PhusionPassenger::Standalone::Utils
- PhusionPassenger::Standalone::VersionCommand
- PhusionPassenger::Utils
- PhusionPassenger::Utils::AnsiColors
- PhusionPassenger::Utils::FileSystemWatcher
- PhusionPassenger::Utils::FileSystemWatcher::DirInfo
- PhusionPassenger::Utils::FileSystemWatcher::FileInfo
- PhusionPassenger::Utils::HostsFileParser
- PhusionPassenger::Utils::TeeInput
- PhusionPassenger::Utils::TmpIO
- PhusionPassenger::Utils::UnseekableSocket
- CommonLibraryBuilder
- Exception
- GC
- IO
- Object
- Signal

No matching classes.

# PhusionPassenger::Utils::HostsFileParser

A /etc/hosts parser. Also supports writing groups of data to the file.

### Public Class Methods

flush_dns_cache!() click to toggle source

    # File lib/phusion_passenger/utils/hosts_file_parser.rb, line 29
    def self.flush_dns_cache!
            if RUBY_PLATFORM =~ /darwin/
                    system("dscacheutil -flushcache")
            end
    end

new(filename_or_io = "/etc/hosts") click to toggle source

    # File lib/phusion_passenger/utils/hosts_file_parser.rb, line 35
    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

### Public Instance Methods

add_group_data(marker, data) click to toggle source

    # File lib/phusion_passenger/utils/hosts_file_parser.rb, line 66
    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

host_count() click to toggle source

    # File lib/phusion_passenger/utils/hosts_file_parser.rb, line 49
    def host_count
            return @host_names.size
    end

ip_count() click to toggle source

    # File lib/phusion_passenger/utils/hosts_file_parser.rb, line 45
    def ip_count
            return @ips.size
    end

resolve(host_name) click to toggle source

    # File lib/phusion_passenger/utils/hosts_file_parser.rb, line 53
    def resolve(host_name)
            if host_name.downcase == "localhost"
                    return "127.0.0.1"
            else
                    return @host_names[host_name.downcase]
            end
    end

resolves_to_localhost?(hostname) click to toggle source

    # File lib/phusion_passenger/utils/hosts_file_parser.rb, line 61
    def resolves_to_localhost?(hostname)
            ip = resolve(hostname)
            return ip == "127.0.0.1" || ip == "::1" || ip == "0.0.0.0"
    end

write(io) click to toggle source

    # File lib/phusion_passenger/utils/hosts_file_parser.rb, line 78
    def write(io)
            @lines.each do |line|
                    io.puts(line)
            end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.