floere/phony — pull request #93 by illoyd (2013-05-12 15:05:49+00:00)
Title: Country alpha2 detection

Phony is a fantastic tool but I found myself needing to guess the country of a phone number. I have extended Phony with this functionality.

In effect, you can now call `Phony.country` with a normalized number and it will use the same logic as it uses to split a number to determine the best guess country. The response will be a symbol of the ISO ALPHA2 code for the country.

Examples:
* `Phony.country '12121234567'` gives `:US` (United States)
* `Phony.country '18686627272'` gives `:TT` (Trinidad & Tobago)
* `Phony.country '59122772266'` gives `:BO` (Bolivia)

And so on. For those countries, such as the US and TT, which share the country prefix (NANP), I included a special 'country detector' which can further analyze the phone number to see which specific region or country it might be. A similar detector could be built for any other countries which share prefixes - I'm looking at you, Russia, Kazakhstan, Abkhasia, and South Ossetia. Unfortunately I do not have enough knowledge about those countries to build a detector, so it defaults to Russia.

I have included tests based upon the numbers you were already testing. Hopefully this is of some use to you and anyone else who might need to guess the country of a phone number. If you have any questions please let me know.

-Ian Lloyd