Sha256: 9b29a90e37d9fe93de124f17f0a28990b6fb586cba939295805e808689cc6b8d

Contents?: true

Size: 984 Bytes

Versions: 3

Compression:

Stored size: 984 Bytes

Contents

# frozen_string_literal: true

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
      ALPHA2_MAP[alpha2.to_sym] || alpha2
    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

3 entries across 3 versions & 1 rubygems

Version Path
gman-7.0.6 lib/gman/country_codes.rb
gman-7.0.5 lib/gman/country_codes.rb
gman-7.0.4 lib/gman/country_codes.rb