class Middleman::DnsResolver::HostsResolver

Use network name server to resolve ips and names

Attributes

resolver[R]

Public Class Methods

new(opts={}) click to toggle source
# File lib/middleman-core/dns_resolver/hosts_resolver.rb, line 11
def initialize(opts={})
  # using the splat operator works around a non-existing HOSTSRC variable
  # using nil as input does not work, but `*[]` does and then Resolv::Hosts
  # uses its defaults
  @resolver = opts.fetch(:resolver, Resolv::Hosts.new(*hosts_file))
end

Public Instance Methods

getaddresses(name) click to toggle source

Get ips for name

@param [#to_s] name

The name to resolve into ips

@return [Array]

Array of ipaddresses
# File lib/middleman-core/dns_resolver/hosts_resolver.rb, line 38
def getaddresses(name)
  resolver.getaddresses(name.to_s).map(&:to_s)
rescue Resolv::ResolvError
  []
end
getnames(ip) click to toggle source

Get names for ip

@param [#to_s] ip

The ip to resolve into names

@return [Array]

Array of Names
# File lib/middleman-core/dns_resolver/hosts_resolver.rb, line 25
def getnames(ip)
  resolver.getnames(ip.to_s).map(&:to_s)
rescue Resolv::ResolvError
  []
end

Private Instance Methods

hosts_file() click to toggle source

Path to hosts file

This looks for MM_HOSTSRC in your environment

@return [Array]

This needs to be an array, to make the splat operator work

@example

# <ip> <hostname>
127.0.0.1 localhost.localhost localhost
# File lib/middleman-core/dns_resolver/hosts_resolver.rb, line 56
def hosts_file
  return [ENV['MM_HOSTSRC']] if ENV.key?('MM_HOSTSRC') && File.file?(ENV['MM_HOSTSRC'])

  []
end