Sha256: f998a37baf74eec3343ff1046beff284fa6454dc033ed5058bfdac9fa709728d

Contents?: true

Size: 1.02 KB

Versions: 37

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Bridgetown
  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
      #     Bridgetown.logger.warn "Warning:", "Version check has been disabled."
      #     Bridgetown.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

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
bridgetown-core-0.19.3 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.19.2 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.19.1 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.19.0 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.18.6 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.18.5 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.18.4 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.18.3 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.18.2 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.18.1 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.18.0 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.17.1 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.17.0 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.16.0 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.16.0.beta2 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.16.0.beta1 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.15.0 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.15.0.beta4 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.15.0.beta3 lib/bridgetown-core/utils/internet.rb
bridgetown-core-0.15.0.beta2 lib/bridgetown-core/utils/internet.rb