phonelib 0.4.6 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/data/extended_data.dat +0 -0
  3. data/data/libphonenumber/README.md +1 -1
  4. data/data/libphonenumber/cpp/src/phonenumbers/lite_metadata.cc +9342 -9356
  5. data/data/libphonenumber/cpp/src/phonenumbers/metadata.cc +9917 -9931
  6. data/data/libphonenumber/cpp/src/phonenumbers/phonenumberutil.cc +3 -3
  7. data/data/libphonenumber/cpp/src/phonenumbers/short_metadata.cc +2366 -2365
  8. data/data/libphonenumber/java/carrier/pom.xml +4 -4
  9. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/1246_en +0 -0
  10. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/248_en +0 -0
  11. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/373_en +0 -0
  12. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/60_en +0 -0
  13. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/63_en +0 -0
  14. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/92_en +0 -0
  15. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/94_en +0 -0
  16. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/95_en +0 -0
  17. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/960_en +0 -0
  18. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/972_en +0 -0
  19. data/data/libphonenumber/java/demo/pom.xml +5 -5
  20. data/data/libphonenumber/java/geocoder/pom.xml +4 -4
  21. data/data/libphonenumber/java/internal/prefixmapper/pom.xml +3 -3
  22. data/data/libphonenumber/java/libphonenumber/pom.xml +2 -2
  23. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java +2 -3
  24. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BB +0 -0
  25. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BR +0 -0
  26. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CR +0 -0
  27. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_ID +0 -0
  28. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IL +0 -0
  29. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_JO +0 -0
  30. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KW +0 -0
  31. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_LK +0 -0
  32. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_LU +0 -0
  33. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MD +0 -0
  34. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MM +0 -0
  35. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MV +0 -0
  36. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_MY +0 -0
  37. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PH +0 -0
  38. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PK +0 -0
  39. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SC +0 -0
  40. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SE +0 -0
  41. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/ShortNumberMetadataProto_BE +0 -0
  42. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/ShortNumberMetadataProto_LK +0 -0
  43. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/ShortNumberMetadataProto_US +0 -0
  44. data/data/libphonenumber/java/pom.xml +1 -1
  45. data/data/libphonenumber/java/release_notes.txt +8 -0
  46. data/data/libphonenumber/javascript/i18n/phonenumbers/demo-compiled.js +67 -67
  47. data/data/libphonenumber/javascript/i18n/phonenumbers/metadata.js +25 -25
  48. data/data/libphonenumber/javascript/i18n/phonenumbers/metadatalite.js +25 -25
  49. data/data/libphonenumber/resources/PhoneNumberMetadata.xml +67 -80
  50. data/data/libphonenumber/resources/ShortNumberMetadata.xml +6 -4
  51. data/data/libphonenumber/resources/carrier/en/1.txt +1 -0
  52. data/data/libphonenumber/resources/carrier/en/248.txt +2 -0
  53. data/data/libphonenumber/resources/carrier/en/373.txt +0 -1
  54. data/data/libphonenumber/resources/carrier/en/60.txt +189 -43
  55. data/data/libphonenumber/resources/carrier/en/63.txt +2 -0
  56. data/data/libphonenumber/resources/carrier/en/92.txt +1 -0
  57. data/data/libphonenumber/resources/carrier/en/94.txt +3 -0
  58. data/data/libphonenumber/resources/carrier/en/95.txt +1 -0
  59. data/data/libphonenumber/resources/carrier/en/960.txt +1 -1
  60. data/data/libphonenumber/resources/carrier/en/972.txt +1 -0
  61. data/data/phone_data.dat +0 -0
  62. data/lib/phonelib/core.rb +12 -2
  63. data/lib/phonelib/data_importer.rb +46 -0
  64. data/lib/phonelib/phone_analyzer.rb +45 -33
  65. data/lib/phonelib/version.rb +1 -1
  66. metadata +2 -2
@@ -4,6 +4,11 @@ module Phonelib
4
4
  # array of types not included for validation check in cycle
5
5
  NOT_FOR_CHECK = [:general_desc, :fixed_line, :mobile, :fixed_or_mobile]
6
6
 
7
+ # caches regular expression, reusing it for later lookups
8
+ def cr(regexp)
9
+ Phonelib.phone_regexp_cache[regexp] ||= Regexp.new(regexp)
10
+ end
11
+
7
12
  # parses provided phone if it is valid for country data and returns result of
8
13
  # analyze
9
14
  #
@@ -34,17 +39,18 @@ module Phonelib
34
39
  # * +phone+ - phone for parsing
35
40
  # * +country+ - country to parse phone with
36
41
  def try_to_parse_single_country(phone, country)
37
- if country && Phonelib.phone_data[country]
42
+ data = Phonelib.phone_data[country]
43
+ if country && data
38
44
  # if country was provided and it's a valid country, trying to
39
45
  # create e164 representation of phone number,
40
46
  # kind of normalization for parsing
41
- e164 = convert_to_e164 phone, Phonelib.phone_data[country]
47
+ e164 = convert_to_e164 phone, data
42
48
  # if phone starts with international prefix of provided
43
49
  # country try to reanalyze without international prefix for
44
50
  # all countries
45
51
  return analyze(e164.gsub('+', ''), nil) if e164[0] == '+'
46
52
  # trying to parse number for provided country
47
- parse_single_country e164, Phonelib.phone_data[country]
53
+ parse_single_country e164, data
48
54
  end
49
55
  end
50
56
 
@@ -55,8 +61,13 @@ module Phonelib
55
61
  # * +e164+ - e164 representation of phone for parsing
56
62
  # * +data+ - country data for single country for parsing
57
63
  def parse_single_country(e164, data)
58
- country_match = phone_match_data?(e164, data)
59
- country_match && get_national_and_data(e164, data, country_match)
64
+ valid_match = phone_match_data?(e164, data)
65
+ if valid_match
66
+ get_national_and_data(e164, data, valid_match)
67
+ else
68
+ possible_match = phone_match_data?(e164, data, true)
69
+ possible_match && get_national_and_data(e164, data, possible_match)
70
+ end
60
71
  end
61
72
 
62
73
  # method tries to detect what is the country for provided phone
@@ -90,12 +101,15 @@ module Phonelib
90
101
  # * +phone+ - phone number for parsing
91
102
  # * +data+ - country data to be based on for creating e164 representation
92
103
  def convert_to_e164(phone, data)
93
- match = phone.match full_valid_regex_for_data(data)
94
- if match
104
+ match = phone.match full_regex_for_data(data, Core::VALID_PATTERN)
105
+ case
106
+ when match
95
107
  national_start = (1..3).map { |i| match[i].to_s.length }.inject(:+)
96
108
  "#{data[Core::COUNTRY_CODE]}#{phone[national_start..-1]}"
109
+ when phone.match(cr("^#{data[Core::INTERNATIONAL_PREFIX]}"))
110
+ phone.sub(cr("^#{data[Core::INTERNATIONAL_PREFIX]}"), '+')
97
111
  else
98
- phone.sub(/^#{data[Core::INTERNATIONAL_PREFIX]}/, '+')
112
+ "#{data[Core::COUNTRY_CODE]}#{phone}"
99
113
  end
100
114
  end
101
115
 
@@ -106,7 +120,7 @@ module Phonelib
106
120
  #
107
121
  # * +data+ - country data hash
108
122
  # * +country_optional+ - whether to put country code as optional group
109
- def full_valid_regex_for_data(data, country_optional = true)
123
+ def full_regex_for_data(data, type, country_optional = true)
110
124
  regex = []
111
125
  regex << "(#{data[Core::INTERNATIONAL_PREFIX]})?"
112
126
  regex << if country_optional
@@ -114,10 +128,10 @@ module Phonelib
114
128
  else
115
129
  data[Core::COUNTRY_CODE]
116
130
  end
117
- regex << "(#{data[Core::NATIONAL_PREFIX]})?"
118
- regex << "(#{data[Core::TYPES][Core::GENERAL][Core::VALID_PATTERN]})"
131
+ regex << "(#{data[Core::NATIONAL_PREFIX_FOR_PARSING] || data[Core::NATIONAL_PREFIX]})?"
132
+ regex << "(#{data[Core::TYPES][Core::GENERAL][type]})"
119
133
 
120
- /^#{regex.join}$/
134
+ cr("^#{regex.join}$")
121
135
  end
122
136
 
123
137
  # returns national number and analyzing results for provided phone number
@@ -130,7 +144,7 @@ module Phonelib
130
144
  def get_national_and_data(phone, data, country_match)
131
145
  prefix_length = data[Core::COUNTRY_CODE].length
132
146
  prefix_length += [1, 2].map { |i| country_match[i].to_s.size }.inject(:+)
133
- result = data.select { |k, v| ![:types, :formats].include?(k) }
147
+ result = data.select { |k, v| k != :types && k != :formats }
134
148
  result[:national] = phone[prefix_length..-1]
135
149
  result[:format] = get_number_format(result[:national],
136
150
  data[Core::FORMATS])
@@ -144,12 +158,13 @@ module Phonelib
144
158
  #
145
159
  # * +phone+ - phone number for parsing
146
160
  # * +data+ - country data
147
- def phone_match_data?(phone, data)
161
+ def phone_match_data?(phone, data, possible = false)
148
162
  country_code = "#{data[Core::COUNTRY_CODE]}"
149
163
  inter_prefix = "(#{data[Core::INTERNATIONAL_PREFIX]})?"
150
- if phone =~ /^#{inter_prefix}#{country_code}/
151
- phone.match full_valid_regex_for_data(data, false)
152
- end
164
+ return unless phone.match cr("^#{inter_prefix}#{country_code}")
165
+
166
+ type = possible ? Core::POSSIBLE_PATTERN : Core::VALID_PATTERN
167
+ phone.match full_regex_for_data(data, type, false)
153
168
  end
154
169
 
155
170
  # Returns all valid and possible phone number types for currently parsed
@@ -193,8 +208,8 @@ module Phonelib
193
208
  def get_number_format(national, format_data)
194
209
  format_data && format_data.find do |format|
195
210
  (format[Core::LEADING_DIGITS].nil? \
196
- || /^(#{format[Core::LEADING_DIGITS]})/ =~ national) \
197
- && /^(#{format[Core::PATTERN]})$/ =~ national
211
+ || national.match(cr("^(#{format[Core::LEADING_DIGITS]})"))) \
212
+ && national.match(cr("^(#{format[Core::PATTERN]})$"))
198
213
  end || Core::DEFAULT_NUMBER_FORMAT
199
214
  end
200
215
 
@@ -219,17 +234,14 @@ module Phonelib
219
234
  # * +all_patterns+ - hash of all patterns for validation
220
235
  # * +type+ - type of phone to get patterns for
221
236
  def get_patterns(all_patterns, type)
222
- patterns = case type
223
- when Core::FIXED_OR_MOBILE
224
- all_patterns[Core::FIXED_LINE]
225
- else
226
- all_patterns[type]
227
- end
228
- return [nil, nil] if patterns.nil?
229
- national_pattern = patterns[Core::VALID_PATTERN]
230
- possible_pattern = patterns[Core::POSSIBLE_PATTERN] || national_pattern
237
+ type = Core::FIXED_LINE if type == Core::FIXED_OR_MOBILE
238
+ patterns = all_patterns[type]
231
239
 
232
- [possible_pattern, national_pattern]
240
+ if patterns.nil?
241
+ [nil, nil]
242
+ else
243
+ [patterns[Core::POSSIBLE_PATTERN], patterns[Core::VALID_PATTERN]]
244
+ end
233
245
  end
234
246
 
235
247
  # Checks if passed number matches valid and possible patterns
@@ -240,15 +252,15 @@ module Phonelib
240
252
  # * +possible_pattern+ - possible pattern for validation
241
253
  # * +national_pattern+ - valid pattern for validation
242
254
  def number_valid_and_possible?(number, possible_pattern, national_pattern)
243
- possible_match = number.match(/^(?:#{possible_pattern})$/)
255
+ possible_match = number.match(cr("^(?:#{possible_pattern})$"))
244
256
  possible = possible_match && possible_match.to_s.length == number.length
245
257
 
258
+ return [possible, possible] if possible_pattern == national_pattern
259
+ valid = false
246
260
  if possible
247
261
  # doing national pattern match only in case possible matches
248
- national_match = number.match(/^(?:#{national_pattern})$/)
262
+ national_match = number.match(cr("^(?:#{national_pattern})$"))
249
263
  valid = national_match && national_match.to_s.length == number.length
250
- else
251
- valid = false
252
264
  end
253
265
 
254
266
  [valid && possible, possible]
@@ -1,5 +1,5 @@
1
1
  # :nodoc:
2
2
  module Phonelib
3
3
  # :nodoc:
4
- VERSION = '0.4.6'
4
+ VERSION = '0.4.7'
5
5
  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.4.6
4
+ version: 0.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Senderovich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-10 00:00:00.000000000 Z
11
+ date: 2015-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake