Skip to content

Commit

Permalink
Merge branch 'PedroMGSilva-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
daddyz committed Apr 22, 2019
2 parents f05d75a + 596e207 commit 08aa551
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 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)
result = {}
Phonelib.phone_data.each do |key, data|
parsed = parse_single_country(phone, data)
if double_prefix_allowed?(data, phone, parsed && parsed[key])
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
48 changes: 48 additions & 0 deletions spec/phonelib_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1039,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 08aa551

Please sign in to comment.