Sha256: b92ce02e9584ca0b660e7aca9f52df5a3ef19f71cf06dfc5794dc6e4ffbb73ad

Contents?: true

Size: 980 Bytes

Versions: 2

Compression:

Stored size: 980 Bytes

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"
  }

  # 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 = domain.tld.split('.').last
    if ALPHA2_MAP[alpha2.to_sym]
      ALPHA2_MAP[alpha2.to_sym]
    else
      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
    @country ||= IsoCountryCodes.find(alpha2) if alpha2
  rescue IsoCountryCodes::UnknownCodeError
    nil
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gman-5.0.1 lib/gman/country_codes.rb
gman-5.0.0 lib/gman/country_codes.rb