Sha256: 82d639faeb7361f2f12d5e6cd6646515a5e581eed0c8952b31574a641e94799f

Contents?: true

Size: 1013 Bytes

Versions: 156

Compression:

Stored size: 1013 Bytes

Contents

require "resolv-replace"

module CC
  class Resolv
    def self.with_fixed_dns(dns = ::Resolv::DNS.new)
      ::Resolv::DefaultResolver.replace_resolvers([Fixed.new(dns)])

      yield if block_given?
    ensure
      # There's no way to ask what the current values are before we override
      # them; hopefully going by the source is good enough.
      # https://docs.ruby-lang.org/en/2.0.0/Resolv.html#method-c-new
      default_resolvers = [::Resolv::Hosts.new, ::Resolv::DNS.new]
      ::Resolv::DefaultResolver.replace_resolvers(default_resolvers)
    end

    class Fixed
      def initialize(fallback)
        @addresses = {}
        @fallback = fallback
      end

      def each_address(name)
        if addresses.key?(name)
          yield addresses.fetch(name)
        else
          fallback.each_address(name) do |address|
            addresses[name] ||= address
            yield address
          end
        end
      end

      private

      attr_reader :addresses, :fallback
    end
  end
end

Version data entries

156 entries across 156 versions & 3 rubygems

Version Path
codeclimate-0.49.0 lib/cc/resolv.rb
codeclimate-0.48.0 lib/cc/resolv.rb
codeclimate-0.47.0 lib/cc/resolv.rb
codeclimate-0.46.0 lib/cc/resolv.rb
codeclimate-0.45.0 lib/cc/resolv.rb
codeclimate-0.44.0 lib/cc/resolv.rb
codeclimate-services-1.9.5 lib/cc/resolv.rb
codeclimate-services-1.9.4 lib/cc/resolv.rb
codeclimate-0.43.1 lib/cc/resolv.rb
codeclimate-services-1.9.3 lib/cc/resolv.rb
codeclimate-services-1.9.2 lib/cc/resolv.rb
codeclimate-0.43.0 lib/cc/resolv.rb
codeclimate-0.42.1 lib/cc/resolv.rb
codeclimate-0.42.0 lib/cc/resolv.rb
codeclimate-0.41.0 lib/cc/resolv.rb
codeclimate-services-1.9.1 lib/cc/resolv.rb