Sha256: d95d78e9260f7146c78bf5f27a6dbbf6105d2fddb242c618517b57e8bc30a8a8

Contents?: true

Size: 1.06 KB

Versions: 8

Compression:

Stored size: 1.06 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"
  }

  # 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

8 entries across 8 versions & 1 rubygems

Version Path
gman-5.0.9 lib/gman/country_codes.rb
gman-5.0.8 lib/gman/country_codes.rb
gman-5.0.7 lib/gman/country_codes.rb
gman-5.0.6 lib/gman/country_codes.rb
gman-5.0.5 lib/gman/country_codes.rb
gman-5.0.4 lib/gman/country_codes.rb
gman-5.0.3 lib/gman/country_codes.rb
gman-5.0.2 lib/gman/country_codes.rb