Sha256: e416b9beb761cc16313838cd05d8480115f97d4b139761da1d345bcc903a2dbf

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

# frozen_string_literal: true

$LOAD_PATH.unshift(File.dirname(__FILE__))

require 'naughty_or_nice'
require 'iso_country_codes'
require 'csv'
require_relative 'gman/version'
require_relative 'gman/country_codes'
require_relative 'gman/identifier'

class Gman
  include NaughtyOrNice

  autoload :DomainList, 'gman/domain_list'
  autoload :Importer,   'gman/importer'
  autoload :Locality,   'gman/locality'

  class << self
    def list
      @list ||= DomainList.new(path: list_path)
    end

    def academic_list
      @academic_list ||= DomainList.new(path: academic_list_path)
    end

    def config_path
      @config_path ||= File.expand_path '../config', File.dirname(__FILE__)
    end

    # Returns the absolute path to the domain list
    def list_path
      File.expand_path 'domains.txt', config_path
    end

    def academic_list_path
      File.expand_path 'vendor/academic.txt', config_path
    end
  end

  # Checks if the input string represents a government domain
  #
  # Returns boolean true if a government domain
  def valid?
    @valid ||= begin
      return false unless valid_domain?
      return false if academic?

      locality? || public_suffix_valid?
    end
  end

  def locality?
    Locality.valid?(domain)
  end

  private

  def valid_domain?
    @valid_domain ||= !domain.nil? && !academic?
  end

  def academic?
    @academic ||= domain && Gman.academic_list.valid?(to_s)
  end

  def public_suffix_valid?
    @public_suffix_valid ||= Gman.list.valid?(to_s)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gman-7.0.5 lib/gman.rb
gman-7.0.4 lib/gman.rb
gman-7.0.3 lib/gman.rb