countries Ruby Gem

repository·master·Indexed 25 days ago

https://github.com/countries/countries

A Ruby gem providing comprehensive ISO 3166 standard data, including country codes (alpha-2 and alpha-3), subdivisions, currencies, and geographic information. It features attribute-based finder methods, localized country names, membership checks for international organizations (EU, G7, G20, UN), and native integration for Mongoid models. Optional extensions provide currency details via the money gem.

Tokens
5K
Snippets
17
Records
46
Agent score
81%

What's inside countries

  1. Migrate from #name and #names to new name attributes

    master

    In versions 4.2 and later, the #name attribute was deprecated in favor of #iso_short_name and #iso_long_name to clarify that these follow ISO3166 standards.

    If you need the 'common name' (e.g., 'United Kingdom' instead of 'United Kingdom of Great Britain and Northern Ireland'), use the #common_name method, which is a shortcut for #translation('en').

    Note: The #name and #names attributes and their corresponding finder methods were completely removed in version 5.0.

  2. Enable and use the Currencies extension

    master

    To access currency information, you must enable the currency extension in the initializer and add the money gem to your Gemfile.

    # 1. Add to Gemfile
    gem "money", "~> 6.9"
    
    # 2. Enable in initializer
    ISO3166.configuration.enable_currency_extension!
    
    # 3. Usage
    c = ISO3166::Country['us']
    c.currency.iso_code # => 'USD'
    c.currency.name    # => 'United States Dollar'
    c.currency.symbol # => '$'
  3. Basic usage of ISO3166::Country

    master
    To work with country data, instantiate a country object using ISO3166::Country.new(alpha2_code) or the shortcut ISO3166::Country[alpha2_code]. You can also retrieve all available alpha2 country codes using ISO3166::Country.codes.
  4. Use Mongoid integration for country fields

    master

    If Mongoid is defined in your project, countries automatically enables Mongoid support. You can use the Country type directly in your Mongoid models to enable native support for searching and saving by either a country object or an alpha2 code.

    Note that the database only stores the alpha2 code. The full Country object is rebuilt when the record is queried.

    field :country, type: Country
  5. Configure selective locale loading

    master

    To reduce memory usage in production, you can configure which locales are loaded. By default, it uses I18n.available_locales if I18n is present. If you change the locales after initialization, you must call ISO3166::Data.reset to refresh the cache.

    ISO3166.configure do |config|
      config.locales = [:en, :de, :fr, :es]
    end
    
    # If changing after initialization:
    ISO3166::Data.reset
  6. Return country name by default in Mongoid models

    master

    Since Mongoid stores only the alpha2 code, querying a record returns a Country object. If you want your model to return the country's name (e.g., iso_short_name) by default when accessing the field, override the reader method in your model.

    def country
      super.iso_short_name
    end
  7. Search and save countries in Mongoid

    master

    When using field :country, type: Country in a Mongoid model, you can perform queries and assignments using both alpha2 strings and Country objects.

    # Searching
    # By alpha2
    spanish_things = Things.where(country: 'ES')
    spanish_things.first.country.iso_short_name    # => "Spain"
    
    # By object
    spanish_things = Things.where(country: Country.find_by_iso_short_name('Spain')[1])
    spanish_things.first.country.iso_short_name    # => "Spain"
    
    # Saving
    # By alpha2
    spanish_things = Thing.new(country: 'ES')
    spanish_things.save!
    spanish_things.country.iso_short_name    # => "Spain"
    
    # By object
    spanish_things = Thing.new(country: Country.find_by_iso_short_name('Spain')[1])
    spanish_things.save!
    spanish_things.country.iso_short_name    # => "Spain"
  8. Access country location and boundary data

    master

    Retrieve geographic information including coordinates, regions, and bounding boxes.

    c = ISO3166::Country.new('US')
    
    # Coordinates
    c.latitude  # => "37.09024"
    c.longitude # => "-95.712891"
    
    # Regional info
    c.world_region # => "AMER"
    c.region       # => "Americas"
    c.subregion    # => "Northern America"
    
    # Bounding box
    c.bounds # => {"northeast"=>{"lat"=>22.166667, "lng"=>58}, "southwest"=>{"lat"=>22.166667, "lng"=>45}}