Sha256: 6898dbc6cd574aeca207f9b518d7c3a68a15f143d0e81023221b4b05ab49456f

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 KB

Contents

class Gman
  # Map last part of TLD to alpha2 country code
  ALPHA2_MAP = {
    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

  # Returns the two character alpha county code represented by the domain
  #
  # e.g., United States = US, United Kingdom = GB
  def alpha2
    return unless domain
    @alpha2 ||= begin
      alpha2 = domain.tld.split('.').last
      if ALPHA2_MAP[alpha2.to_sym]
        ALPHA2_MAP[alpha2.to_sym]
      else
        alpha2
      end
    end
  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"
  def country
    return @country if defined? @country
    @country ||= begin
      IsoCountryCodes.find(alpha2) if alpha2
    rescue IsoCountryCodes::UnknownCodeError
      nil
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gman-7.0.2 lib/gman/country_codes.rb
gman-7.0.1 lib/gman/country_codes.rb
gman-7.0.0 lib/gman/country_codes.rb
gman-6.0.1 lib/gman/country_codes.rb
gman-6.0.0 lib/gman/country_codes.rb