phonelib 0.7.0 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9a96a615d554195979c6dc886188eb4f8638e89f616fb5ce5aa6cb74ee91ca0
4
- data.tar.gz: e8b51d76743373fd9e2913eb5eddcea65e35e57673f29e47a361398dab662a16
3
+ metadata.gz: de2fb5689287a2a83f3c74e7d12b25344d08f0d22d684e8c00cba53e558fcfa4
4
+ data.tar.gz: 23080bb4050fd864bdd099ff55cb685bc1b03fbc2ea202402f428070104726fb
5
5
  SHA512:
6
- metadata.gz: 7710f2ab513e1bb53bb6bec2fe78de93fa742c18c85f2393f05ebf862cd245c7b3b01a4055403af287c4e225a58c26fd03507f0f08bf9e4d1468ad1279623ecd
7
- data.tar.gz: e44959fbc033b27ac6b2974941c2ce010ba788bf371a4048a45c7d3dba78bb982b08c9201019b4bd2f958b139e46f7f29dcf0c0ae6c406a94d21490129df68de
6
+ metadata.gz: 82f68a1c09bf91a304778457977ead507f54feea2b67694571b9855bc41e44e238753d5f85b8bc89a1eaafe561e34d97a9031070325b448f02ba63558a03cb59
7
+ data.tar.gz: ea6c6adf618037f4aece9d1c871c08953bae9f934b5dad1f5406d4842e253f30a8c7f55669b6bb690d9b3be7df34534f5a0cd5d27b45d04708235b47dd15a01b
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Built in integration with JetBrains RubyMine](https://github.com/daddyz/phonelib/blob/master/icon_RubyMine.png?raw=true)](https://www.jetbrains.com/ruby/)
4
4
  [![Gem Version](https://badge.fury.io/rb/phonelib.svg)](http://badge.fury.io/rb/phonelib)
5
- [![CircleCI](https://circleci.com/gh/daddyz/phonelib/tree/master.svg?style=shield)](https://circleci.com/gh/daddyz/phonelib/tree/master)
5
+ [![CircleCI](https://circleci.com/gh/daddyz/phonelib/tree/master.svg?style=svg)](https://circleci.com/gh/daddyz/phonelib/tree/master)
6
6
  [![](https://codeclimate.com/github/daddyz/phonelib/badges/coverage.svg)](https://codeclimate.com/github/daddyz/phonelib/coverage)
7
7
  [![](https://codeclimate.com/github/daddyz/phonelib/badges/gpa.svg)](https://codeclimate.com/github/daddyz/phonelib)
8
8
  [![Inline docs](http://inch-ci.org/github/daddyz/phonelib.svg?branch=master)](http://inch-ci.org/github/daddyz/phonelib)
@@ -42,10 +42,11 @@ gem 'phonelib'
42
42
 
43
43
  Run the bundle command to install it.
44
44
 
45
- To set the default country (country names are [ISO 3166-1 Alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) codes), create a initializer in <tt>config/initializers/phonelib.rb</tt>:
45
+ To set the default country or several default countries for parsing (country names are [ISO 3166-1 Alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) codes), create a initializer in <tt>config/initializers/phonelib.rb</tt>:
46
46
 
47
47
  ``` ruby
48
48
  Phonelib.default_country = "CN"
49
+ Phonelib.default_country = ['CN', 'FR']
49
50
  ```
50
51
 
51
52
  To use the ability to parse special numbers (Short Codes, Emergency etc.) you can set ```Phonelib.parse_special```. This is disabled by default
@@ -66,6 +67,12 @@ To disable sanitizing of passed phone number (keeping digits only)
66
67
  Phonelib.strict_check = true
67
68
  ```
68
69
 
70
+ To disable country reset during parsing in case phone starts with + sign and country specified but country phone prefix doesn't match phone's prefix
71
+
72
+ ``` ruby
73
+ Phonelib.ignore_plus = true
74
+ ```
75
+
69
76
  To change sanitized symbols on parsed number, so non-specified symbols won't be wiped and will fail the parsing
70
77
 
71
78
  ``` ruby
Binary file
data/data/phone_data.dat CHANGED
Binary file
data/lib/phonelib/core.rb CHANGED
@@ -107,6 +107,22 @@ module Phonelib
107
107
  @@strict_check = strict
108
108
  end
109
109
 
110
+ # @private don't use plus sign for automatic country change
111
+ @@ignore_plus = false
112
+
113
+ # getter for ignore plus flag
114
+ # @return [Boolean] Flag defines whether to reset country in case number has + and country prefix doesn't match
115
+ def ignore_plus
116
+ @@ignore_plus
117
+ end
118
+
119
+ # setter for ignore plus flag
120
+ # @param ignore_plus [Boolean] ignore plus sign or not
121
+ # @return [Boolean] Flag defines whether to ignore plus for country reset during validations in case country prefix doesn't match
122
+ def ignore_plus=(ignore_plus)
123
+ @@ignore_plus = ignore_plus
124
+ end
125
+
110
126
  # @private sanitizing regex, matching symbols will get removed from parsed number, must be string
111
127
  @@sanitize_regex = '[^0-9]+'
112
128
 
@@ -32,7 +32,7 @@ module Phonelib
32
32
  # defines if to validate against single country or not
33
33
  def passed_country(country)
34
34
  code = country_prefix(country)
35
- if Core::PLUS_SIGN == @original[0] && code && !sanitized.start_with?(code)
35
+ if !Phonelib.ignore_plus && Core::PLUS_SIGN == @original[0] && code && !sanitized.start_with?(code)
36
36
  # in case number passed with + but it doesn't start with passed
37
37
  # country prefix
38
38
  country = nil
@@ -22,8 +22,8 @@ module Phonelib
22
22
  return nil if sanitized.nil? || sanitized.empty?
23
23
  if valid?
24
24
  @national_number
25
- elsif country_code && sanitized.start_with?(country_code)
26
- sanitized[country_code.size..-1]
25
+ elsif data_country_code && sanitized.start_with?(data_country_code)
26
+ sanitized[data_country_code.size..-1]
27
27
  else
28
28
  sanitized
29
29
  end
@@ -32,8 +32,17 @@ module Phonelib
32
32
  # Returns the country code from the original phone number.
33
33
  # @return [String] matched country phone code
34
34
  def country_code
35
- @country_code ||= Phonelib.phone_data[country] && \
36
- Phonelib.phone_data[country][Core::COUNTRY_CODE]
35
+ return @country_code if @country_code
36
+
37
+ code = Phonelib.phone_data[country] && Phonelib.phone_data[country][Core::COUNTRY_CODE]
38
+ return @country_code = code unless code == '1' && Phonelib.phone_data[country][Core::LEADING_DIGITS]
39
+
40
+ match = e164.match(/\A\+(1(#{Phonelib.phone_data[country][Core::LEADING_DIGITS]}))/)
41
+ if match
42
+ @country_code = match[1]
43
+ else
44
+ @country_code = '1'
45
+ end
37
46
  end
38
47
 
39
48
  # Returns e164 formatted phone number. Method can receive single string parameter that will be defined as prefix
@@ -44,7 +53,7 @@ module Phonelib
44
53
  prefix = formatted if formatted.is_a?(String)
45
54
  return nil if sanitized.empty?
46
55
  return "#{prefix}#{country_prefix_or_not}#{sanitized}" unless valid?
47
- return "#{prefix}#{country_code}#{@national_number}" unless formatted
56
+ return "#{prefix}#{data_country_code}#{@national_number}" unless formatted
48
57
 
49
58
  fmt = @data[country][:format]
50
59
  national = @national_number
@@ -53,7 +62,7 @@ module Phonelib
53
62
  national = fmt.gsub(/\$\d/) { |el| matches[el[1].to_i] } unless fmt == 'NA'
54
63
  end
55
64
 
56
- "#{prefix}#{country_code} #{national}"
65
+ "#{prefix}#{data_country_code} #{national}"
57
66
  end
58
67
 
59
68
  # returns national formatted number with extension added
@@ -109,6 +118,10 @@ module Phonelib
109
118
 
110
119
  private
111
120
 
121
+ def data_country_code
122
+ @data_country_code ||= Phonelib.phone_data[country] && Phonelib.phone_data[country][Core::COUNTRY_CODE]
123
+ end
124
+
112
125
  # @private defines if phone can have area code
113
126
  def area_code_possible?
114
127
  return false if impossible?
@@ -124,8 +137,8 @@ module Phonelib
124
137
 
125
138
  # @private defines whether to put country prefix or not
126
139
  def country_prefix_or_not
127
- return '' unless country_code
128
- sanitized.start_with?(country_code) ? '' : country_code
140
+ return '' unless data_country_code
141
+ sanitized.start_with?(data_country_code) ? '' : data_country_code
129
142
  end
130
143
 
131
144
  # @private returns extension with separator defined
@@ -1,4 +1,4 @@
1
1
  module Phonelib
2
2
  # @private
3
- VERSION = '0.7.0'
3
+ VERSION = '0.7.1'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phonelib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Senderovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-08 00:00:00.000000000 Z
11
+ date: 2022-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake