class Gman
Constants
- ALPHA2_MAP
Map last part of TLD to alpha2 country code
- VERSION
Public Class Methods
# File lib/gman.rb, line 24 def academic_list @academic_list ||= DomainList.new(path: academic_list_path) end
# File lib/gman.rb, line 37 def academic_list_path File.expand_path 'vendor/academic.txt', config_path end
# File lib/gman.rb, line 28 def config_path @config_path ||= File.expand_path '../config', File.dirname(__FILE__) end
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
# File lib/gman.rb, line 20 def list @list ||= DomainList.new(path: list_path) end
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
# File lib/gman/identifier.rb, line 137 def dotgov_list @dotgov_list ||= CSV.read(dotgov_list_path, headers: true) end
# File lib/gman/identifier.rb, line 143 def dotgov_list_path File.join Gman.config_path, 'vendor/dotgovs.csv' end
Public Instance Methods
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
# 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
# File lib/gman/identifier.rb, line 102 def cog? return false unless matches matches[1] == 'cog' end
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
# File lib/gman/identifier.rb, line 76 def county? if matches matches[3] == 'co' elsif dotgov_listing domain_type == 'County' else false end end
# File lib/gman/identifier.rb, line 96 def district? return false unless matches matches[1] == 'dst' end
# File lib/gman/identifier.rb, line 56 def dotgov? domain.tld == 'gov' end
# File lib/gman/identifier.rb, line 60 def federal? return false unless dotgov_listing domain_type =~ /^Federal/i end
# File lib/gman.rb, line 53 def locality? Locality.valid?(domain) end
# 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
# 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
# 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
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
# File lib/gman.rb, line 63 def academic? @academic ||= domain && Gman.academic_list.valid?(to_s) end
# 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
# 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
# File lib/gman/identifier.rb, line 121 def matches return @matches if defined? @matches @matches = domain.to_s.match(Locality::REGEX) end
# File lib/gman.rb, line 67 def public_suffix_valid? @public_suffix_valid ||= Gman.list.valid?(to_s) end
# File lib/gman.rb, line 59 def valid_domain? @valid_domain ||= !domain.nil? && !academic? end