Class: Gman

Inherits:
Object
  • Object
show all
Includes:
NaughtyOrNice
Defined in:
lib/gman.rb,
lib/gman/version.rb,
lib/gman/importer.rb,
lib/gman/locality.rb,
lib/gman/identifier.rb,
lib/gman/domain_list.rb,
lib/gman/country_codes.rb

Defined Under Namespace

Classes: DomainList, Importer, Locality

Constant Summary collapse

VERSION =
'7.0.6'
ALPHA2_MAP =

Map last part of TLD to alpha2 country code

{
  ac: 'sh',
  uk: 'gb',
  su: 'ru',
  tp: 'tl',
  yu: 'rs',
  gov: 'us',
  mil: 'us',
  org: 'us',
  com: 'us',
  net: 'us',
  edu: 'us',
  travel: 'us',
  info: 'us'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.academic_listObject



24
25
26
# File 'lib/gman.rb', line 24

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

.academic_list_pathObject



37
38
39
# File 'lib/gman.rb', line 37

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

.config_pathObject



28
29
30
# 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) ⇒ Object

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



14
15
16
17
18
19
20
21
22
23
24
# 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

.dotgov_listObject



137
138
139
# File 'lib/gman/identifier.rb', line 137

def dotgov_list
  @dotgov_list ||= CSV.read(dotgov_list_path, headers: true)
end

.listObject



20
21
22
# File 'lib/gman.rb', line 20

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

.list_pathObject

Returns the absolute path to the domain list



33
34
35
# File 'lib/gman.rb', line 33

def list_path
  File.expand_path 'domains.txt', config_path
end

Instance Method Details

#alpha2Object

Returns the two character alpha county code represented by the domain

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



24
25
26
27
28
29
30
31
# 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?Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
# 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?Boolean

Returns:

  • (Boolean)


102
103
104
105
106
# File 'lib/gman/identifier.rb', line 102

def cog?
  return false unless matches

  matches[1] == 'cog'
end

#countryObject

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”



38
39
40
41
42
43
44
45
46
# 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?Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
82
83
84
# 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?Boolean

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/gman/identifier.rb', line 96

def district?
  return false unless matches

  matches[1] == 'dst'
end

#dotgov?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/gman/identifier.rb', line 56

def dotgov?
  domain.tld == 'gov'
end

#federal?Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/gman/identifier.rb', line 60

def federal?
  return false unless dotgov_listing

  domain_type =~ /^Federal/i
end

#locality?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/gman.rb', line 53

def locality?
  Locality.valid?(domain)
end

#stateObject



45
46
47
48
49
50
51
52
53
54
# 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?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
92
93
94
# 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

#typeObject



32
33
34
35
36
37
38
39
40
41
42
43
# 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?Boolean

Checks if the input string represents a government domain

Returns boolean true if a government domain

Returns:

  • (Boolean)


45
46
47
48
49
50
51
# 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