Skip to content

Commit

Permalink
add config var strict_double_prefix_check
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroMGSilva authored and daddyz committed Apr 22, 2019
1 parent 552a1c8 commit 227ac14
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 19 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ To disable sanitizing of passed phone number (keeping digits only)
Phonelib.strict_check = true
```

To disable sanitizing of double prefix on passed phone number

```ruby
Phonelib.strict_double_prefix_check = true
```

To set different extension separator on formatting, this setting doesn't affect parsing. Default setting is ';'

``` ruby
Expand Down
16 changes: 16 additions & 0 deletions lib/phonelib/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ def strict_check=(strict)
@@strict_check = strict
end

# @private strict double prefix check for validator, doesn't sanitize number
@@strict_double_prefix_check = false

# getter for strict double prefix check flag
# @return [Boolean] Flag defines whether to do strict double prefix parsing check
def strict_double_prefix_check
@@strict_double_prefix_check
end

# setter for strict double prefix check flag
# @param strict [Boolean] make a strict double prefix parsing or not
# @return [Boolean] Flag defines whether to do strict double prefix parsing check
def strict_double_prefix_check=(strict)
@@strict_double_prefix_check = strict
end

@@override_phone_data = nil
# setter for data file to use
def override_phone_data=(file_path)
Expand Down
6 changes: 3 additions & 3 deletions lib/phonelib/phone_analyzer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def analyze(phone, passed_country)
# all is good, return result
when passed_country.nil?
# trying for all countries if no country was passed
detect_and_parse phone
detect_and_parse(phone, country)
when country_can_dp?(country)
# if country allows double prefix trying modified phone
parse_country(changed_dp_phone(country, phone), country)
Expand Down Expand Up @@ -102,11 +102,11 @@ def parse_single_country(e164, data)
# ==== Attributes
#
# * +phone+ - phone number for parsing
def detect_and_parse(phone)
def detect_and_parse(phone, country = nil)
result = {}
Phonelib.phone_data.each do |key, data|
parsed = parse_single_country(phone, data)
if double_prefix_allowed?(data, phone, parsed && parsed[key]) && key == Phonelib.default_country.to_s.upcase
if (!Phonelib.strict_double_prefix_check || key == country) && double_prefix_allowed?(data, phone, parsed && parsed[key])
parsed = parse_single_country(changed_dp_phone(key, phone), data)
end
result.merge!(parsed) unless parsed.nil?
Expand Down
64 changes: 48 additions & 16 deletions spec/phonelib_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,6 @@

context 'issue #61' do
it 'should be valid number in India' do
Phonelib.default_country = "IN"
phone = Phonelib.parse('9111844757')
expect(phone.valid?).to be true
expect(phone.sanitized).to eq('9111844757')
Expand All @@ -659,20 +658,6 @@
expect(phone.sanitized).to eq('4949266444201')
expect(phone.e164).to eq('+4949266444201')
end

it 'should be invalid number outside India' do
Phonelib.default_country = nil
phone = Phonelib.parse('9111844757')
expect(phone.valid?).to be false
expect(Phonelib.valid?('919111844757')).to be true

phone = Phonelib.parse('49266444201')
expect(phone.valid?).to be true
phone = Phonelib.parse('4949266444201')
expect(phone.valid?).to be true
expect(phone.sanitized).to eq('4949266444201')
expect(phone.e164).to eq('+4949266444201')
end
end

context 'issue #60' do
Expand Down Expand Up @@ -930,7 +915,6 @@

context 'issue #105' do
it 'should be valid when original without +' do
Phonelib.default_country = :IN
expect(Phonelib.valid?('9183082081')).to be true
expect(Phonelib.valid_for_country?('9183082081', 'IN')).to be true
end
Expand Down Expand Up @@ -1055,6 +1039,54 @@
end
end

context 'issue #161' do
before do
Phonelib.strict_double_prefix_check = false
end

context 'when strict_double_prefix_check is false' do
it 'should be valid number outside the country' do
Phonelib.default_country = nil
phone = Phonelib.parse('9111844757')
expect(phone.valid?).to be true
expect(Phonelib.valid?('919111844757')).to be true
end

it 'should be valid number inside the country' do
phone = Phonelib.parse('9111844757', 'IN')
expect(phone.valid?).to be true
expect(Phonelib.valid?('919111844757')).to be true

Phonelib.default_country = 'IN'
phone = Phonelib.parse('9111844757')
expect(phone.valid?).to be true
end
end

context 'when strict_double_prefix_check is true' do
before do
Phonelib.strict_double_prefix_check = true
end

it 'should be invalid number outside the country' do
Phonelib.default_country = nil
phone = Phonelib.parse('9111844757')
expect(phone.valid?).to be false
expect(Phonelib.valid?('919111844757')).to be true
end

it 'should be valid number inside the country' do
phone = Phonelib.parse('9111844757', 'IN')
expect(phone.valid?).to be true
expect(Phonelib.valid?('919111844757')).to be true

Phonelib.default_country = 'IN'
phone = Phonelib.parse('9111844757')
expect(phone.valid?).to be true
end
end
end

context 'valid_country_name method' do
it 'should not return name for invalid number' do
phone = Phonelib.parse('+12121231234')
Expand Down

0 comments on commit 227ac14

Please sign in to comment.