GithubHelp home page GithubHelp logo

kraziegent / countries Goto Github PK

View Code? Open in Web Editor NEW

This project forked from countries/countries

0.0 1.0 0.0 52.42 MB

All sorts of useful information about every country packaged as convenient little country objects. It includes data from ISO 3166 (countries and states/subdivisions ), ISO 4217 (currency), and E.164 (phone numbers).

License: MIT License

Ruby 100.00%

countries's Introduction

Countries

Countries is a collection of all sorts of useful information for every country in the ISO 3166 standard. It contains info for the following standards ISO3166-1(countries), ISO3166-2(states/subdivisions), ISO4217(currency) and E.164(phone numbers). I will add any country based data I can get access to. I hope this to be a repository for all country based information.

Build Status Dependency Status Code Climate Waffle.io Issues in Ready

Installation

gem install countries

If you’re in Rails 2.3 or earlier, place this in your environment.rb:

config.gem 'countries'

Or you can install via bundler Gemfile if you are using Rails 3:

gem 'countries'

Or you can install via bundler Gemfile with a Country Helper class:

gem 'countries', :require => 'global'

Basic Usage

Note that Country class still exist by default. (is inherited from ISO3166::Country to keep backward compatibility).

Simply load a new country object using Country.new(alpha2) or the shortcut Country[alpha2]. An example works best.

c = ISO3166::Country.new('US')

# with global Country Helper
c = Country['US']

Attribute-Based Finder Methods

You can lookup a country or an array of countries using any of the data attributes via the find_country_by_attribute dynamic methods:

c = ISO3166::Country.find_country_by_name('united states')
list = ISO3166::Country.find_all_countries_by_region('Americas')
c = ISO3166::Country.find_country_by_alpha3('can')

For a list of available attributes please see ISO3166::Country::AttrReaders. Note: searches are case insensitive.

Country Info

Identification Codes

c.number #=> "840"
c.alpha2 #=> "US"
c.alpha3 #=> "USA"
c.gec    #=> "US"

Names & Translations

c.name #=> "United States"
c.names #=> ["United States of America", "Vereinigte Staaten von Amerika", "États-Unis", "Estados Unidos"]

# Get a specific translation
c.translation('de') #=> 'Vereinigte Staaten von Amerika'
c.translations['fr'] #=> "États-Unis"

ISO3166::Country.translations             # {"DE"=>"Germany",...}
ISO3166::Country.translations('DE')       # {"DE"=>"Deutschland",...}
ISO3166::Country.all_translated           # ['Germany', ...]
ISO3166::Country.all_translated('DE')     # ['Deutschland', ...]

Subdivisions & States

c.subdivisions #=> {"CO" => {"name" => "Colorado", "names" => "Colorado"}, ... }
c.states #=> {"CO" => {"name" => "Colorado", "names" => "Colorado"}, ... }

Location

c.latitude #=> "38 00 N"
c.longitude #=> "97 00 W"
c.latitude_dec #=> 39.44325637817383
c.longitude_dec #=> -98.95733642578125

c.region #=> "Americas"
c.subregion #=> "Northern America"

Telephone Routing (E164)

c.country_code #=> "1"
c.national_destination_code_lengths #=> 3
c.national_number_lengths #=> 10
c.international_prefix #=> "011"
c.national_prefix #=> "1"

Boundry Boxes

c.min_longitude #=> '45'
c.min_latitude #=> '22.166667'
c.max_longitude #=> '58'
c.max_latitude #=> '26.133333'

European Union Membership

c.in_eu? #=> false

Currencies

Countries now uses the Currencies gem. What this means is you now get back a Currency object that gives you access to all the currency information. It acts the same as a hash so the same ['name'] methods still work.

c.currency['code'] #=> 'USD'
c.currency['name'] #=> 'Dollars'
c.currency['symbol'] #=> '$'

If a country has an alternate currency it can be accessed via the alt_currency method and will also return a Currency object.

Since we are using the Currencies gem we get a bonus ExchangeBank that can be used with the Money gem. It auto loads exchange rates from Yahoo Finance.

Money.default_bank = Currency::ExchangeBank.new
Money.us_dollar(100).exchange_to("CAD")  # => Money.new(124, "CAD")

Address Formatting

A template for formatting addresses is available through the address_format method. These templates are compatible with the Liquid template system.

c.address_format #=> "{{recipient}}\n{{street}}\n{{city}} {{region}} {{postalcode}}\n{{country}}"

Mongoid

Mongoid support has been added. It is required automatically if Mongoid is defined in your project.

Use native country fields in your model:

field :country, type: Country

Adds native support for searching/saving by a country object or alpha2 code.

Searching:

# By alpha2
british_things = Things.where(country: 'GB')
british_things.first.country.name    # => "United Kingdom"

# By object
british_things = Things.where(country: Country.find_by_name('United Kingdom')[1])
british_things.first.country.name    # => "United Kingdom"

Saving:

# By alpha2
british_thing = Thing.new(country: 'GB')
british_thing.save!
british_thing.country.name    # => "United Kingdom"

# By object
british_thing = Thing.new(country: Country.find_by_name('United Kingdom')[1])
british_thing.save!
british_thing.country.name    # => "United Kingdom"

Note that the database stores only the alpha2 code and rebuilds the object when queried. To return the country name by default you can override the reader method in your model:

def country
    super.name
end

ToDo

  • State select
  • Class methods for looking up information
  • Default country
  • Exclude countries
  • Preferred countries
  • Whitelist countries

Note on Patches/Pull Requests

Please do not submit pull requests on cache/translations.yaml

Any additions should be directed upstream to (pkg-isocodes)[http://anonscm.debian.org/cgit/pkg-isocodes/iso-codes.git/]

New Bugs can be filed upstream here https://alioth.debian.org/projects/pkg-iso-codes/

Any corrections can be applied in translations_corrections.yaml these will be injected during the next rake update_cache

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Copyright

Copyright (c) 2015 hexorx. See LICENSE for details.

countries's People

Contributors

cedricdubois avatar cibernox avatar cllns avatar dwilkie avatar esfourteen avatar fabn avatar franckverrot avatar gui avatar guilleart avatar hexorx avatar jeremywrowe avatar jgrau avatar joecorcoran avatar joshtaylor avatar jrdi avatar kriskhaira avatar leemhenson avatar lorddoig avatar lorenzopagano avatar lukkry avatar maxwell avatar mibamur avatar micrum avatar pr0d1r2 avatar pwim avatar rposborne avatar schatteleyn avatar skarlcf avatar stayhero avatar sykaeh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.