0

I have an international number as a string, so 81312345678 (Japan, Tokyo number) or 85212345678 (Hong Kong) and I need to isolate the country code, 81 in the case of Japan and 852 in the case of Hong Kong, in order to then identify the 2 letter alpha2 code of the country.

I have tried the phony gem as follows:

number = Phony.format('18091231234', :format => :international)
  puts number
  split = Phony.split('85212345678')
  puts split[0]

In the case of Hong Kong, for example, I would now like to take '852' and get the 2 letter country code for Hong Kong in order to then look up pricing using the Plivo api.

which produces:

+1 809 123 1234
852

I have tried the 'iso_country_codes' gem unsuccessfully and the 'countries' gem with:

c = Country.find_country_by_national_prefix('852')
  puts c.alpha2

The error message is 'undefined method alpha2' this should be easy. Any ideas guys? Thanks as always.

user1903663
  • 1,713
  • 2
  • 22
  • 44

2 Answers2

0

With a little testing, it looks like the national_prefix value (in the countries gem) of Hong Kong is "None". You may want to use Country#find_country_by_country_code like so: Country.find_country_by_country_code('852') and that will return Hong Kong, which you can then call #alpha2 on.

On a side note, you may want to check that c is not nil before trying to call alpha2 on it.

Taylor Glaeser
  • 1,338
  • 12
  • 18
  • thank you, works perfectly. I am most grateful. Have a good day. – user1903663 May 07 '15 at 03:14
  • Glad that I could help! – Taylor Glaeser May 07 '15 at 03:14
  • I have discovered a very serious shortcoming, whiuc is that 44 (which is the UK or 'GB') returns 'JE', Jersey. Any ideas how I can fix this? – user1903663 May 07 '15 at 05:38
  • So it appears that Jersey and Great Britain have the same calling code (which makes sense) but regardless of that fact, there doesn't appear to be a way to get GB from the `countries` gem. Now I found that the `iso_country_codes` gem will return a list of all countries that use a particular country_code. (i.e. `IsoCountryCodes.search_by_calling_code('+44').map(&:alpha2)` returns `["IM", "JE", "GB"]`) So unfortunately you may have to do some extra parsing/checking prior to searching. – Taylor Glaeser May 07 '15 at 14:14
  • I found this gem after doing a little bit of research [countries-phone_numbers](https://github.com/illoyd/countries-phone_numbers). It works with the `countries` gem and is built on `phony`. It correctly identified 4 different GB phone numbers, and it even identified a Jersey phone number without issue. That may be your best bet to get the exact desired country. – Taylor Glaeser May 07 '15 at 14:30
  • much obliged, I will give it a shot and report results here. – user1903663 May 08 '15 at 00:37
  • thought you would like to know how it all ended up, so I have "answered! my own question. Thank you for your careful and researched input. – user1903663 May 09 '15 at 07:28
-1

While the "phony" gem is still very useful, I tried it with the "countries" gem and the "iso_country_codes" gem with difficulty and partial success so I ended up downloading the pricing data from the Plivo website (more than 38,000 rows) and loaded it onto a postgres database.

One column is the country_code and another is the prefix column. In some cases you need only the country_code and others both the country_code and the prefix to get a precise result. So, here is the code:

get '/iso' do
    number = '447928xxxxxx' #this is a mobile so you need both 44 and 7928
    code = Phony.split(number)
    lookup = code.take(2).join #joins the first 2 elements of the array
    result = DB[:call_pricing].where(:prefix=>lookup).get(:rate_usd)
    if result.nil?
      lookup = code.take(1) 
    result = DB[:call_pricing].where(:prefix=>lookup).get(:rate_usd)
    end
end

I hope that this helps somebody else.

user1903663
  • 1,713
  • 2
  • 22
  • 44