Sha256: 3a276b0902fe7522536fbfa5710322b21de3bacbdbf9800b952ae45c35d86518

Contents?: true

Size: 1.49 KB

Versions: 43

Compression:

Stored size: 1.49 KB

Contents

module Middleman
  class DnsResolver
    # Use network name server to resolve ips and names
    class HostsResolver
      private

      attr_reader :resolver

      public

      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

      # Get names for ip
      #
      # @param [#to_s] ip
      #   The ip to resolve into names
      #
      # @return [Array]
      #   Array of Names
      def getnames(ip)
        resolver.getnames(ip.to_s).map(&:to_s)
      rescue Resolv::ResolvError
        []
      end

      # Get ips for name
      #
      # @param [#to_s] name
      #   The name to resolve into ips
      #
      # @return [Array]
      #   Array of ipaddresses
      def getaddresses(name)
        resolver.getaddresses(name.to_s).map(&:to_s)
      rescue Resolv::ResolvError
        []
      end

      private

      # 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
      def hosts_file
        return [ENV['MM_HOSTSRC']] if ENV.key?('MM_HOSTSRC') && File.file?(ENV['MM_HOSTSRC'])

        []
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 3 rubygems

Version Path
middleman-core-4.5.1 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.5.0 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.4.3 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.4.2 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.4.0 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.3.11 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.3.10 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.3.8 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.3.7 lib/middleman-core/dns_resolver/hosts_resolver.rb
files.com-1.0.55 docs/vendor/bundle/ruby/2.5.0/gems/middleman-core-4.3.3/lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.3.6 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.3.5 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.3.4 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.3.3 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.3.2 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.3.1 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.3.0 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-with-external-sources-watch-fix-4.1.10 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-with-external-sources-watch-fix-4.1.0 lib/middleman-core/dns_resolver/hosts_resolver.rb
middleman-core-4.3.0.rc.1 lib/middleman-core/dns_resolver/hosts_resolver.rb