class Gman

Constants

ALPHA2_MAP

Map last part of TLD to alpha2 country code

VERSION

Public Class Methods

academic_list() click to toggle source
# File lib/gman.rb, line 24
def academic_list
  @academic_list ||= DomainList.new(path: academic_list_path)
end
academic_list_path() click to toggle source
# File lib/gman.rb, line 37
def academic_list_path
  File.expand_path 'vendor/academic.txt', config_path
end
config_path() click to toggle source
# File lib/gman.rb, line 28
def config_path
  @config_path ||= File.expand_path '../config', File.dirname(__FILE__)
end
def_hash_delegator(hash_method, key, method = nil, default = nil) click to toggle source

Defines an instance method that delegates to a hash’s key

hash_method - a symbol representing the instance method to delegate to. The

instance method should return a hash or respond to #[]

key - the key to call within the hash method - (optional) the instance method the key should be aliased to.

If not specified, defaults to the hash key

default - (optional) value to return if value is nil (defaults to nil)

Returns a symbol representing the instance method

# File lib/gman/identifier.rb, line 14
def self.def_hash_delegator(hash_method, key, method = nil, default = nil)
  method ||= key.to_s.downcase.sub(' ', '_')
  define_method(method) do
    hash = send(hash_method)
    if hash.respond_to? :[]
      hash[key.to_s] || default
    else
      default
    end
  end
end
list() click to toggle source
# File lib/gman.rb, line 20
def list
  @list ||= DomainList.new(path: list_path)
end
list_path() click to toggle source

Returns the absolute path to the domain list

# File lib/gman.rb, line 33
def list_path
  File.expand_path 'domains.txt', config_path
end

Private Class Methods

dotgov_list() click to toggle source
# File lib/gman/identifier.rb, line 137
def dotgov_list
  @dotgov_list ||= CSV.read(dotgov_list_path, headers: true)
end
dotgov_list_path() click to toggle source
# File lib/gman/identifier.rb, line 143
def dotgov_list_path
  File.join Gman.config_path, 'vendor/dotgovs.csv'
end

Public Instance Methods

alpha2() click to toggle source

Returns the two character alpha county code represented by the domain

e.g., United States = US, United Kingdom = GB

# File lib/gman/country_codes.rb, line 24
def alpha2
  return unless domain

  @alpha2 ||= begin
    alpha2 = domain.tld.split('.').last
    ALPHA2_MAP[alpha2.to_sym] || alpha2
  end
end
city?() click to toggle source
# File lib/gman/identifier.rb, line 66
def city?
  if matches
    %w[ci town vil].include?(matches[3])
  elsif dotgov_listing
    domain_type == 'City'
  else
    false
  end
end
cog?() click to toggle source
# File lib/gman/identifier.rb, line 102
def cog?
  return false unless matches

  matches[1] == 'cog'
end
country() click to toggle source

Returns the ISO Country represented by the domain

Example Usage: Gman.new(“foo.gov”).country.name => “United States” Gman.new(“foo.gov”).country.currency => “USD”

# File lib/gman/country_codes.rb, line 38
def country
  return @country if defined? @country

  @country ||= begin
    IsoCountryCodes.find(alpha2) if alpha2
  rescue IsoCountryCodes::UnknownCodeError
    nil
  end
end
county?() click to toggle source
# File lib/gman/identifier.rb, line 76
def county?
  if matches
    matches[3] == 'co'
  elsif dotgov_listing
    domain_type == 'County'
  else
    false
  end
end
district?() click to toggle source
# File lib/gman/identifier.rb, line 96
def district?
  return false unless matches

  matches[1] == 'dst'
end
dotgov?() click to toggle source
# File lib/gman/identifier.rb, line 56
def dotgov?
  domain.tld == 'gov'
end
federal?() click to toggle source
# File lib/gman/identifier.rb, line 60
def federal?
  return false unless dotgov_listing

  domain_type =~ /^Federal/i
end
locality?() click to toggle source
# File lib/gman.rb, line 53
def locality?
  Locality.valid?(domain)
end
state() click to toggle source
# File lib/gman/identifier.rb, line 45
def state
  if matches
    matches[4].upcase
  elsif dotgov_listing['State']
    dotgov_listing['State']
  elsif list_category
    matches = list_category.match(/usagov([A-Z]{2})/)
    matches[1] if matches
  end
end
state?() click to toggle source
# File lib/gman/identifier.rb, line 86
def state?
  if matches
    matches[1] == 'state'
  elsif dotgov_listing
    domain_type == 'State/Local Govt' || domain_type == 'State'
  else
    false
  end
end
type() click to toggle source
# File lib/gman/identifier.rb, line 32
def type
  %i[state district cog city federal county].each do |type|
    return type if send "#{type}?"
  end
  return if list_category.nil?

  if list_category.include?('usagov')
    :unknown
  else
    list_category.to_sym
  end
end
valid?() click to toggle source

Checks if the input string represents a government domain

Returns boolean true if a government domain

# File lib/gman.rb, line 45
def valid?
  return @valid if defined?(@valid)

  @valid = false unless valid_domain?
  @valid = false if academic?
  @valid ||= locality? || public_suffix_valid?
end

Private Instance Methods

academic?() click to toggle source
# File lib/gman.rb, line 63
def academic?
  @academic ||= domain && Gman.academic_list.valid?(to_s)
end
dotgov_listing() click to toggle source
# File lib/gman/identifier.rb, line 127
def dotgov_listing
  return @dotgov_listing if defined? @dotgov_listing
  return unless dotgov?

  @dotgov_listing = Gman.dotgov_list.find do |listing|
    listing['Domain Name'].casecmp("#{domain.sld}.gov").zero?
  end
end
list_category() click to toggle source
# File lib/gman/identifier.rb, line 110
def list_category
  return @list_category if defined?(@list_category)

  match = Gman.list.public_suffix_list.find(domain.to_s)
  return @list_category = nil unless match

  regex = %r{// ([^\n]+)\n?[^/]*\n#{Regexp.escape(match.value)}\n}im
  matches = Gman.list.contents.match(regex)
  @list_category = matches ? matches[1] : nil
end
matches() click to toggle source
# File lib/gman/identifier.rb, line 121
def matches
  return @matches if defined? @matches

  @matches = domain.to_s.match(Locality::REGEX)
end
public_suffix_valid?() click to toggle source
# File lib/gman.rb, line 67
def public_suffix_valid?
  @public_suffix_valid ||= Gman.list.valid?(to_s)
end
valid_domain?() click to toggle source
# File lib/gman.rb, line 59
def valid_domain?
  @valid_domain ||= !domain.nil? && !academic?
end