To validate country informations such as phone number, Postal code.It also help’s in Parsing phone numbers based on country from a given string.
Installation
Add this line to your application’s Gemfile:
gem 'country_lookup'
And then execute:
$ bundle
Or install it yourself as:
$ gem install country_lookup
Usage
This gem has two modules.
1. Country
2. Phone Number
Country
This module of gem helps to get countries information such as alpha2, countrycode, internationalprefix, nationalnumberlengths, postcode_regex etc..
all
It helps to get array of all the countries information.
Country.all
=> [{"alpha2"=>"AF", "name"=>"Afghanistan", "country_code"=>"93", "international_prefix"=>"00", "national_destination_code_lengths"=>[2], "national_number_lengths"=>[8, 9], "postal_code"=>true, "postcode_regex"=>nil}, ....]
names
To get array of all countries names.
Country.names
=> ["Afghanistan", "Aland Islands" .....]
with postal code
To get all Countries with Postal codes.
Country.with_postal_code
=> {"AF"=>"Afghanistan", "AX"=>"Aland Islands", ...}
without postal code
To get all Countries with Postal codes.
Country.with_postal_code
=> {"AO"=>"Angola", "AG"=>"Antigua and Barbuda", ....}
alpha2
To get alpha2 value of given country.
Country.alpha2("United States")
=> 'US'
Phone Number:
This Module of Gem helps in identifying exact phone number and country code form a given string. Below are some of the methods.
extract:
From a given string it extract the phone number.
PhoneNumber.extract("+1(333) 555-2222")
=> 13335552222
parse:
It help to differentiate country code and phone number.
PhoneNumber.parse("+1(333) 555-2222")
=> {:country_code=>"1", :number=>"3335552222"}
prefix country code:
It help’s to prepend country code to a given phone number as specified by the end user. If no country is specified, by default it assumes as ‘US’.
By default it prefixes United State’s country code.
PhoneNumber.prefix_country_code("3335552222")
=> 13335552222
If you want to prefix India’s country code.
PhoneNumber.prefix_country_code("3335552222", 'IN')
=> "913335552222"
If you want to prefix United Kingdom’s country code.
PhoneNumber.prefix_country_code("3335552222", 'GB')
=> "443335552222"
country_code
it help to get country code for a given country
PhoneNumber.country_code('GB')
=> "44"