Closed starkovv closed 11 years ago
Glad you like the gem.
The list of codes is out of date. I think the info is based on the 2005 ITU list. I'll update the gem with the data from the 2011 ITU doc:
I checked and that has a number of updated codes, including 382.
This should be resolved with version 0.4.1, which is now available on rubygems. Let me know if you find any issues.
Thanks for update.
After update to 0.4.1 I got this error:
1.9.3p0 :009 > country_codes = ItuCodes.itu2iso('505')
NoMethodError: undefined method `itu2iso' for ItuCodes:Module
It seems itu2iso
was removed?
That's right, I removed itu2iso
a few months ago with a4ab1fbe6c91512b7fb515a3672d1833d578f771 due to the difficulties of mapping between calling codes and country names. Today, I finally compiled a mapping of the ISO 2-letter codes to the ITU calling codes in ItuCodes::Helpers::ISO2ITU
, so it was easy to add that method back (see c18abf94c7ba7dfe7ec46a905c254c46c892499b). It's available in 0.4.2 on rubygems.
Now it's much better!
But it still has some issues.
First:
1.9.3p0 :005 > country_codes = ItuCodes.itu2iso('246')
=> nil
1.9.3p0 :006 > country_codes = ItuCodes.find_by_itu_code '246'
=> "Diego Garcia"
Second:
1.9.3p0 :011 > country_codes = ItuCodes.parse_code 784012 # This is actually +7 840 / 940 – Abkhazia
=> "7" # It should be "7840"
1.9.3p0 :011 > country_codes = ItuCodes.parse_code 761 # This is actually +7 6xx / 7xx – Kazakhstan
=> "7" # It should be "76"
1.9.3p0 :014 > ItuCodes.parse_code 1403 # This is actually +1 403 – Canada (Alberta)
=> "1" # It should be "1403"
Third:
1.9.3p0 :014 > ItuCodes.parse_code 1403
=> "1"
# BUT
1.9.3p0 :015 > country_codes = ItuCodes.itu2iso('1403')
=> "CA"
1.9.3p0 :024 > country_codes = ItuCodes.itu2iso('7601')
=> nil # No code found for Kazakhstan.
More on that. What I've found:
1.9.3p0 :043 > country_codes = ItuCodes.itu2iso('262')
=> nil
1.9.3p0 :034 > country_codes = ItuCodes.find_by_itu_code '262'
=> "French Departments and Territories in the Indian Ocean"
1.9.3p0 :053 > country_codes = ItuCodes.itu2iso('246')
=> nil
1.9.3p0 :052 > country_codes = ItuCodes.find_by_itu_code '246'
=> "Diego Garcia"
1.9.3p0 :069 > country_codes = ItuCodes.itu2iso('970')
=> nil
1.9.3p0 :070 > country_codes = ItuCodes.find_by_itu_code '970'
=> "Reserved" # This should be "Palestinian territories"
1.9.3p0 :073 > country_codes = ItuCodes.itu2iso('672')
=> nil
1.9.3p0 :074 > country_codes = ItuCodes.find_by_itu_code '672'
=> "Australian External Territories"
1.9.3p0 :079 > country_codes = ItuCodes.itu2iso('6721')
=> nil
1.9.3p0 :080 > country_codes = ItuCodes.find_by_itu_code '6721'
=> nil # This should be "Australian Antarctic Territory"
1.9.3p0 :084 > country_codes = ItuCodes.itu2iso('6723')
=> nil
1.9.3p0 :085 > country_codes = ItuCodes.find_by_itu_code '6723'
=> nil # This should be "Norfolk Island"
1.9.3p0 :105 > country_codes = ItuCodes.itu2iso('1242')
=> nil
1.9.3p0 :106 > country_codes = ItuCodes.find_by_itu_code '1242'
=> "Bahamas"
1.9.3p0 :118 > country_codes = ItuCodes.itu2iso('1670')
=> nil
1.9.3p0 :120 > country_codes = ItuCodes.find_by_itu_code '1670'
=> "Northern Mariana Islands"
This is not complete list. There is much more.
http://en.wikipedia.org/wiki/List_of_country_calling_codes http://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes
How can I help you with making it up?
Thanks for pointing out these issues. I think there are 3 sources of problems:
I think most of the problems are with 1 and 2 above.
Your help would be much appreciated! If you can find the codes for any missing territories and provide an official source, that would be great. I would prefer to use web pages or documents directly from the ISO or ITU. For example, I found that the ISO code for Diego Garcia is DB from this page: http://www.iso.org/iso/home/standards/country_codes/iso-3166-1_decoding_table.htm#DG
Another way you can help is to write test cases for any problems you find.
I have just pushed a commit that should resolve the issues for Diego Garcia and the Australian External Territories.
However, there are some of the use cases you report that are working correctly, actually. ItuCodes.parse_code
will return the country code for the passed value, so this is correct:
ItuCodes.parse_code '1403'
# => '1' # The country code for Canada is '1'
The confusion arises with North America and CIS regions where multiple countries are assigned the same code. To avoid a complex return value, I have simply returned the '1' country code, which is technically correct.
To detect the country name from a passed value, it is necessary to define the "area codes" for that country, which I have done for most of North America. However, I have not done this for Russia & Kazakhstan, which explains this erroneous behavior:
ItuCodes.itu2iso '7601'
# => nil
ItuCodes.find_by_itu_code '7601'
# => nil
ItuCodes.find_by_itu_code '7'
# => [ "Kazakhstan (Republic of)", "Russian Federation"]
I will open a new issue to address the situation with Kazakhstan and Russia.
I believe I have resolved nearly all the issues raised here with version 0.4.5 . Please let me know if you find any further issues. Regarding Abkhazia, that seems like a very complex situation as I found that 2 different country codes appear to be defined. If I can find clear, official specifications from the ITU on Abkhazia, I can update the gem accordingly.
I was off for a while.
Thanks for updating!
Countries recognition works noticeably better. Anyways I found this strange issue:
1.9.3p0 :003 > ItuCodes.itu2iso '88'
NoMethodError: undefined method `[]' for nil:NilClass
Ah, I see the problem. '88' is not a valid ITU country code, and the itu2iso
method is not properly handling the case when the passed value is not a valid country code.
Created a separate issue (#8) and fixed this. Will push a new version of the gem later tonight. Thanks for your help in testing and identifying issues!
OK, check out version 0.4.7 when you have a chance.
Now it's better!
Continue to improve.
First:
1.9.3p0 :004 > ItuCodes.itu2iso '1264'
=> "AI" # Nice!
# But
1.9.3p0 :006 > ItuCodes.iso2itu 'AI'
=> "1" # It should be 1264
# And so on.
Second:
1.9.3p0 :008 > ItuCodes.itu2iso '1700'
=> [] # Should not it be "US"?
1.9.3p0 :008 > ItuCodes.itu2iso '1800'
=> [] # Should not it be "US"?
1.9.3p0 :008 > ItuCodes.itu2iso '1866'
=> [] # Should not it be "US"?
1.9.3p0 :008 > ItuCodes.itu2iso '1877'
=> [] # Should not it be "US"?
1.9.3p0 :008 > ItuCodes.itu2iso '1888'
=> [] # Should not it be "US"?
1.9.3p0 :017 > ItuCodes.itu2iso '5997'
=> ["BQ", "CW"]
# Probably it's possible to improve it (from http://en.wikipedia.org/wiki/List_of_country_calling_codes):
# +599 – Former Netherlands Antilles, now grouped as follows:
# +599 3 – Sint Eustatius
# +599 4 – Saba
# +599 5 – formerly Sint Maarten – Now included in NANP as code +1-721
# +599 7 – Bonaire
# +599 9 – Curaçao
1.9.3p0 :021 > ItuCodes.itu2iso '7940'
=> "RU" # Should be Abkhazia
1.9.3p0 :022 > ItuCodes.itu2iso '7840'
=> "RU" # Should be Abkhazia
Third: Is it possible to get full country name by ITU code or ISO code?
Hi. Thanks for your feedback. I think there are some cases where the behaviour is "as designed" and not a bug.
The ITU code for North American regions is 1
. Even though certain regions only have one area code, that does not mean that the formula 1 + area code
is their ITU code.
ItuCodes.iso2itu 'AI'
=> "1" # as designed
The situation for Abkhazia is somewhat similar, but complicated by international disagreement regarding its 'correct' country code. If you can provide some official documentation from the ITU regarding the correct country code for Abkhazia, perhaps I can make an update.
I have created separate bug tickets to update the area code list for the US (#9) and to investigate the ITU codes for the Former Netherlands Antilles (#10).
Thanks again for the feedback!
I've looked into solutions for #9 and #10 . Neither is a quick fix, but I will work on implementing solutions for both.
Thank you for awesome gem!
It seems countries list is outdated. For instance check out http://en.wikipedia.org/wiki/List_of_country_calling_codes and phone code 382.