summaryrefslogtreecommitdiff
path: root/vendor/bundle/ruby/3.4.0/gems/jekyll-4.4.1/lib/jekyll/utils/internet.rb
blob: 945267f1e5d92ffc4ccf790ff50a5925d972fee9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

module Jekyll
  module Utils
    module Internet
      # Public: Determine whether the present device has a connection to
      # the Internet. This allows plugin writers which require the outside
      # world to have a neat fallback mechanism for offline building.
      #
      # Example:
      #   if Internet.connected?
      #     Typhoeus.get("https://pages.github.com/versions.json")
      #   else
      #     Jekyll.logger.warn "Warning:", "Version check has been disabled."
      #     Jekyll.logger.warn "", "Connect to the Internet to enable it."
      #     nil
      #   end
      #
      # Returns true if a DNS call can successfully be made, or false if not.

      module_function

      def connected?
        !dns("example.com").nil?
      end

      def dns(domain)
        require "resolv"
        Resolv::DNS.open do |resolver|
          resolver.getaddress(domain)
        end
      rescue Resolv::ResolvError, Resolv::ResolvTimeout
        nil
      end
    end
  end
end