1. Introduction
When working on projects, we sometimes face problems related to information of different countries. And of course, you can search google for the information of the country you need and add it to your project yourself. However, this will take a lot of your time.
To solve this problem, today I will introduce you to gem countries
It is a collection of all the useful information of every country in ISO 3166
. It contains information:
- Country according to ISO 3166-1
- States / subdivisions follow ISO 3266-2
- Currency ISO 4217 standard
- Phone number according to E.164 standard.
From the above information, we have basically solved the National issues in our project.
2. Install
- Method 1:
gem install countries
- Method 2: Add to Gemfile:12gem 'countries'
and run12bundle install
3. Get the information of the Country
The first is to initialize the Country object and from there you can get other information related to this Country
1 2 | united_states <span class="token operator">=</span> <span class="token constant">ISO3166</span> <span class="token punctuation">:</span> <span class="token punctuation">:</span> <span class="token constant">Country</span> <span class="token punctuation">.</span> <span class="token keyword">new</span> <span class="token punctuation">(</span> <span class="token string">'US'</span> <span class="token punctuation">)</span> |
3.1 Country code
Usually use the following 2 types: alpha2, alpha3
1 2 3 | united_states <span class="token punctuation">.</span> alpha2 <span class="token comment"># => "US"</span> united_states <span class="token punctuation">.</span> alpha3 <span class="token comment"># => "USA"</span> |
3.2 Name
1 2 | united_states <span class="token punctuation">.</span> name <span class="token comment"># => "United States"</span> |
3.3 Location
Information related to location
1 2 3 4 5 6 7 8 | united_states <span class="token punctuation">.</span> latitude <span class="token comment"># => "38 00 N"</span> united_states <span class="token punctuation">.</span> longitude <span class="token comment"># => "97 00 W"</span> united_states <span class="token punctuation">.</span> latitude_dec <span class="token comment"># => 39.44325637817383</span> united_states <span class="token punctuation">.</span> longitude_dec <span class="token comment"># => -98.95733642578125</span> united_states <span class="token punctuation">.</span> region <span class="token comment"># => "Americas"</span> united_states <span class="token punctuation">.</span> subregion <span class="token comment"># => "Northern America"</span> |
3.4 District / State Information
1 2 3 | united_states <span class="token punctuation">.</span> subdivisions <span class="token comment"># Phân khu</span> united_states <span class="token punctuation">.</span> states <span class="token comment"># Tiểu bang</span> |
3.5 Timezone (optional)
To determine the timezone, it is necessary to install additional gem tzinfo
gem countries
because gem countries
do not support this part
After installation we can get the following common information:
1 2 3 4 | united_states <span class="token punctuation">.</span> timezones <span class="token punctuation">.</span> zone_identifiers united_states <span class="token punctuation">.</span> timezones <span class="token punctuation">.</span> zone_info united_states <span class="token punctuation">.</span> timezones |
3.6 Information about phone number
1 2 3 4 5 6 | united_states <span class="token punctuation">.</span> country_code <span class="token comment"># => "1"</span> united_states <span class="token punctuation">.</span> national_destination_code_lengths <span class="token comment"># => 3</span> united_states <span class="token punctuation">.</span> national_number_lengths <span class="token comment"># => 10</span> united_states <span class="token punctuation">.</span> international_prefix <span class="token comment"># => "011"</span> united_states <span class="token punctuation">.</span> national_prefix <span class="token comment"># => "1"</span> |
Conclude
Because it is a specific type of spec related to Country. So you probably won’t find it necessary to use it. But hopefully remember the key word gem countries
to need when needed. Can find this support gem quickly.