daddyz / phonelib

Ruby gem for phone validation and formatting using google libphonenumber library data
MIT License
1.08k stars 132 forks source link

default_country not working as expected for multiple countries / Canada numbers #278

Closed lusa closed 1 year ago

lusa commented 1 year ago

Hi, we have Canada phone numbers that are being entered without the 1 prefix. This works fine if default_country is set to :ca, but we want both [:us, :ca] to be set as the default countries, in a way where it will try to parse as a US number, then as Canada number. But, unfortunately it is guessing other countries that are not inside the default_country array before it is guessing the correct country in the default_country array (canada).

Strangely, it appears to work if the order is reversed: [:ca, :us] gives expected behavior. Would this be an acceptable workaround or would this clash with US numbers? Edit: See next comment. This doesn't work for some US numbers.

I am on the latest version (0.8.0).

Examples: I would expect all of the following to return Canada in the countries array (implying when we run number.e164 we get canada formatting with +1 in front of the number).

irb(main):026:0> Phonelib.default_country = :ca
=> :ca
irb(main):027:0> p = Phonelib.parse('6478864691')
=> 
#<Phonelib::Phone:0x0000ffff8b9c5280                                                          
...                                                                                           
irb(main):028:0> p.countries
# Works as expected! 👍 
=> ["CA"]
-------------------
irb(main):029:0> Phonelib.default_country = [:us, :ca] 
=> [:us, :ca]
irb(main):030:0> p = Phonelib.parse('6478864691')
=> 
#<Phonelib::Phone:0x0000ffff8ba16ea0                                                                 
...                                                                                                  
irb(main):031:0> p.countries
# Does not work, guesses NZ. 👎 
=> ["NZ"]
-------------------
** Example phone number 2**
irb(main):002:0> Phonelib.default_country = :ca
irb(main):002:0> p = Phonelib.parse('6047642951')
=> 
#<Phonelib::Phone:0x0000ffff7b44b468                             
irb(main):003:0> p.countries
# Works as expected! 👍 
=> ["CA"]
-------------------
**reversed order of countries**
irb(main):004:0> Phonelib.default_country = [:ca, :us]
=> [:ca, :us]
irb(main):005:0> p = Phonelib.parse('6047642951')
=> 
#<Phonelib::Phone:0x0000ffff7890b7d0                                                                 
...                                                                                                  
irb(main):006:0> p.countries
# Works as expected if Canada is first! 👍 
=> ["CA"]
-------------------
irb(main):007:0> Phonelib.default_country = [:us, :ca]
=> [:us, :ca]
irb(main):008:0> p = Phonelib.parse('6047642951')
=> 
#<Phonelib::Phone:0x0000ffff793ec0a0                                                                 
# Does not work, guesses MY. 👎                          
irb(main):009:0> p.countries
=> ["MY"]
lusa commented 1 year ago

Update: Unfortunately reversing the order will not fix this for us, as now US number (Ohio area code) 2346571289 doesn't work with Canada as the first option in default_country, but it does work when :us is the only option or is first in the list. It appears that this array doesn't work as expected and only considers the first option in the array.

lusa commented 1 year ago

@daddyz based on a comment you made in another ticket:

yes, you just need to specify country for such numbers or in case you are talking about single country phones use default_country setting. Refer to readme

So default_country only works for "single country" phone numbers, and so a phone number matching multiple countries as the ones I'm looking at will need to be manually checked with valid_for_country? for each of my countries? If that's the case feel free to close.

daddyz commented 1 year ago

@lusa just released new v0.8.1 with the fix

tilo commented 9 months ago

@daddyz a version change from 0.8.0 to 0.9.0 might have been better here https://github.com/daddyz/phonelib/issues/297#issue-2135368672