phonelib 0.6.2 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. checksums.yaml +4 -4
  2. data/{README.rdoc → README.md} +116 -75
  3. data/data/extended_data.dat +0 -0
  4. data/data/libphonenumber/FALSEHOODS.md +16 -0
  5. data/data/libphonenumber/FAQ.md +8 -0
  6. data/data/libphonenumber/README.md +1 -1
  7. data/data/libphonenumber/cpp/src/phonenumbers/alternate_format.cc +108 -100
  8. data/data/libphonenumber/cpp/src/phonenumbers/lite_metadata.cc +10070 -10016
  9. data/data/libphonenumber/cpp/src/phonenumbers/metadata.cc +10938 -10884
  10. data/data/libphonenumber/cpp/src/phonenumbers/short_metadata.cc +2481 -2455
  11. data/data/libphonenumber/java/carrier/pom.xml +26 -4
  12. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/255_en +0 -0
  13. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/374_en +0 -0
  14. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/374_ru +0 -0
  15. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/421_en +0 -0
  16. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/66_en +0 -0
  17. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/91_en +0 -0
  18. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/93_fa +0 -0
  19. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/973_en +0 -0
  20. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/98_fa +0 -0
  21. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/992_en +0 -0
  22. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/994_en +0 -0
  23. data/data/libphonenumber/java/carrier/src/com/google/i18n/phonenumbers/carrier/data/config +0 -0
  24. data/data/libphonenumber/java/demo/pom.xml +6 -6
  25. data/data/libphonenumber/java/geocoder/pom.xml +26 -4
  26. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/1747_en +0 -0
  27. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/1934_en +0 -0
  28. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/371_en +0 -0
  29. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/44_en +0 -0
  30. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/504_en +0 -0
  31. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/82_bg +0 -0
  32. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/82_ca +0 -0
  33. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/82_el +0 -0
  34. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/82_es +0 -0
  35. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/82_fi +0 -0
  36. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/82_hi +0 -0
  37. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/82_hu +0 -0
  38. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/82_iw +0 -0
  39. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/82_ja +0 -0
  40. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/82_tr +0 -0
  41. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/91_en +0 -0
  42. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/93_en +0 -0
  43. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/93_fa +0 -0
  44. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/98_en +0 -0
  45. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/98_fa +0 -0
  46. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/992_en +0 -0
  47. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/994_en +0 -0
  48. data/data/libphonenumber/java/geocoder/src/com/google/i18n/phonenumbers/geocoding/data/config +0 -0
  49. data/data/libphonenumber/java/internal/prefixmapper/pom.xml +25 -3
  50. data/data/libphonenumber/java/libphonenumber/pom.xml +23 -2
  51. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/MetadataLoader.java +2 -1
  52. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/MultiFileMetadataSourceImpl.java +63 -70
  53. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberAlternateFormatsProto_595 +0 -0
  54. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_AZ +0 -0
  55. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BD +0 -0
  56. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BG +0 -0
  57. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_BH +0 -0
  58. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CI +0 -0
  59. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_CN +0 -0
  60. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_HK +0 -0
  61. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_HN +0 -0
  62. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_IN +0 -0
  63. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KN +0 -0
  64. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_KW +0 -0
  65. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_LV +0 -0
  66. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_PY +0 -0
  67. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SK +0 -0
  68. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_SN +0 -0
  69. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TC +0 -0
  70. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TH +0 -0
  71. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TJ +0 -0
  72. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_TZ +0 -0
  73. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto_US +0 -0
  74. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/ShortNumberMetadataProto_AE +0 -0
  75. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/ShortNumberMetadataProto_AR +0 -0
  76. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/ShortNumberMetadataProto_BR +0 -0
  77. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/ShortNumberMetadataProto_GB +0 -0
  78. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/ShortNumberMetadataProto_IN +0 -0
  79. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/ShortNumberMetadataProto_NO +0 -0
  80. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/ShortNumberMetadataProto_SK +0 -0
  81. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/ShortNumberMetadataProto_ZA +0 -0
  82. data/data/libphonenumber/java/libphonenumber/src/com/google/i18n/phonenumbers/data/SingleFilePhoneNumberMetadataProto +0 -0
  83. data/data/libphonenumber/java/libphonenumber/test/com/google/i18n/phonenumbers/MultiFileMetadataSourceImplTest.java +25 -5
  84. data/data/libphonenumber/java/pom.xml +2 -2
  85. data/data/libphonenumber/java/release_notes.txt +45 -0
  86. data/data/libphonenumber/javascript/i18n/phonenumbers/demo-compiled.js +72 -70
  87. data/data/libphonenumber/javascript/i18n/phonenumbers/metadata.js +38 -32
  88. data/data/libphonenumber/javascript/i18n/phonenumbers/metadatalite.js +38 -32
  89. data/data/libphonenumber/resources/PhoneNumberAlternateFormats.xml +16 -0
  90. data/data/libphonenumber/resources/PhoneNumberMetadata.xml +232 -96
  91. data/data/libphonenumber/resources/ShortNumberMetadata.xml +188 -44
  92. data/data/libphonenumber/resources/carrier/en/255.txt +2 -0
  93. data/data/libphonenumber/resources/carrier/en/374.txt +3 -3
  94. data/data/libphonenumber/resources/carrier/en/421.txt +1 -0
  95. data/data/libphonenumber/resources/carrier/en/66.txt +1 -0
  96. data/data/libphonenumber/resources/carrier/en/91.txt +419 -253
  97. data/data/libphonenumber/resources/carrier/en/973.txt +1 -0
  98. data/data/libphonenumber/resources/carrier/en/992.txt +1 -0
  99. data/data/libphonenumber/resources/carrier/en/994.txt +3 -0
  100. data/data/libphonenumber/resources/carrier/fa/93.txt +24 -0
  101. data/data/libphonenumber/resources/carrier/fa/98.txt +30 -0
  102. data/data/libphonenumber/resources/carrier/ru/374.txt +28 -0
  103. data/data/libphonenumber/resources/geocoding/bg/82.txt +34 -0
  104. data/data/libphonenumber/resources/geocoding/ca/82.txt +34 -0
  105. data/data/libphonenumber/resources/geocoding/el/82.txt +28 -0
  106. data/data/libphonenumber/resources/geocoding/en/1.txt +2 -0
  107. data/data/libphonenumber/resources/geocoding/en/371.txt +3 -0
  108. data/data/libphonenumber/resources/geocoding/en/44.txt +19 -1
  109. data/data/libphonenumber/resources/geocoding/en/504.txt +19 -0
  110. data/data/libphonenumber/resources/geocoding/en/91.txt +1 -5
  111. data/data/libphonenumber/resources/geocoding/en/93.txt +52 -0
  112. data/data/libphonenumber/resources/geocoding/en/98.txt +0 -2
  113. data/data/libphonenumber/resources/geocoding/en/992.txt +77 -0
  114. data/data/libphonenumber/resources/geocoding/en/994.txt +98 -0
  115. data/data/libphonenumber/resources/geocoding/es/82.txt +33 -0
  116. data/data/libphonenumber/resources/geocoding/fa/93.txt +53 -0
  117. data/data/libphonenumber/resources/geocoding/fa/98.txt +0 -2
  118. data/data/libphonenumber/resources/geocoding/fi/82.txt +34 -0
  119. data/data/libphonenumber/resources/geocoding/hi/82.txt +36 -0
  120. data/data/libphonenumber/resources/geocoding/hu/82.txt +35 -0
  121. data/data/libphonenumber/resources/geocoding/iw/82.txt +37 -0
  122. data/data/libphonenumber/resources/geocoding/ja/82.txt +35 -0
  123. data/data/libphonenumber/resources/geocoding/tr/82.txt +35 -0
  124. data/data/libphonenumber/tools/java/common/pom.xml +2 -0
  125. data/data/libphonenumber/tools/java/cpp-build/pom.xml +4 -0
  126. data/data/libphonenumber/tools/java/data/pom.xml +1 -11
  127. data/data/libphonenumber/tools/java/java-build/pom.xml +2 -0
  128. data/data/phone_data.dat +0 -0
  129. data/lib/phonelib.rb +7 -6
  130. data/lib/phonelib/core.rb +116 -68
  131. data/lib/phonelib/data_importer.rb +23 -19
  132. data/lib/phonelib/data_importer_helper.rb +16 -16
  133. data/lib/phonelib/phone.rb +47 -137
  134. data/lib/phonelib/phone_analyzer.rb +27 -25
  135. data/lib/phonelib/phone_analyzer_helper.rb +16 -9
  136. data/lib/phonelib/phone_extended_data.rb +27 -17
  137. data/lib/phonelib/phone_formatter.rb +131 -0
  138. data/lib/phonelib/version.rb +2 -3
  139. data/lib/tasks/phonelib_tasks.rake +0 -2
  140. data/lib/validators/phone_validator.rb +24 -14
  141. metadata +42 -3
@@ -1,7 +1,7 @@
1
1
  require 'phonelib/data_importer_helper'
2
2
 
3
3
  module Phonelib
4
- # module processes creation of data files needed for this gem
4
+ # @private module processes creation of data files needed for this gem
5
5
  module DataImporter
6
6
  require 'nokogiri'
7
7
 
@@ -13,7 +13,7 @@ module Phonelib
13
13
  Importer.new
14
14
  end
15
15
 
16
- # class with functionality for importing data
16
+ # @private class with functionality for importing data
17
17
  class Importer
18
18
  include Phonelib::DataImporterHelper
19
19
 
@@ -31,18 +31,25 @@ module Phonelib
31
31
  # carrier data dir in repo
32
32
  CARRIER_DIR = 'resources/carrier/en/'
33
33
  # timezones data dir in repo
34
- TIMEZONES_DIR= 'resources/timezones/'
34
+ TIMEZONES_DIR = 'resources/timezones/'
35
35
 
36
36
  # class initialization method
37
37
  def initialize
38
38
  @destination = File.path(
39
- "#{File.dirname(__FILE__)}/../../data/libphonenumber/")
39
+ "#{File.dirname(__FILE__)}/../../data/libphonenumber/")
40
40
  @data = {}
41
41
  @prefixes = {}
42
42
  @geo_names = []
43
43
  @timezones = []
44
44
  @carriers = []
45
45
 
46
+ run_import
47
+ end
48
+
49
+ private
50
+
51
+ # running import method
52
+ def run_import
46
53
  clone_repo
47
54
  import_main_data
48
55
  import_short_data
@@ -53,8 +60,6 @@ module Phonelib
53
60
  save_data_file
54
61
  end
55
62
 
56
- private
57
-
58
63
  # method saves parsed data to data files
59
64
  def save_data_file
60
65
  data_file =
@@ -84,7 +89,7 @@ module Phonelib
84
89
 
85
90
  system("rm -rf #{@destination}")
86
91
  cloned = system("git clone #{repo} #{@destination} --depth 1 -b master")
87
- raise 'Could not clone repo' unless cloned
92
+ fail 'Could not clone repo' unless cloned
88
93
  end
89
94
 
90
95
  # method parses main data file
@@ -125,15 +130,14 @@ module Phonelib
125
130
  def import_alternate_formats
126
131
  puts 'IMPORTING ALTERNATE FORMATS'
127
132
 
128
-
129
133
  main_from_xml("#{@destination}#{FORMATS_FILE}").elements.each do |el|
130
- el.children.each do | phone_type |
131
- if phone_type.name == 'availableFormats'
132
- formats = parse_formats(phone_type.children)
134
+ el.children.each do |phone_type|
135
+ next unless phone_type.name == 'availableFormats'
133
136
 
134
- country_code = el.attribute('countryCode').value
135
- @data[get_country_by_code(country_code)][:formats] += formats
136
- end
137
+ formats = parse_formats(phone_type.children)
138
+
139
+ country_code = el.attribute('countryCode').value
140
+ @data[get_country_by_code(country_code)][:formats] += formats
137
141
  end
138
142
  end
139
143
  end
@@ -175,7 +179,7 @@ module Phonelib
175
179
  types = {}
176
180
  formats = []
177
181
 
178
- without_comments(children).each do | phone_type |
182
+ without_comments(children).each do |phone_type|
179
183
  if phone_type.name == 'availableFormats'
180
184
  formats = parse_formats(phone_type.children)
181
185
  else
@@ -218,7 +222,7 @@ module Phonelib
218
222
 
219
223
  without_comments(format.children).each do |f|
220
224
  current_format[name2sym(f.name)] =
221
- str_clean(f.children.first, is_not_format(f.name))
225
+ str_clean(f.children.first, not_format?(f.name))
222
226
  end
223
227
 
224
228
  current_format
@@ -242,13 +246,13 @@ module Phonelib
242
246
 
243
247
  # method finds country by country prefix
244
248
  def get_country_by_code(country_code)
245
- match = @data.select { |k, v| v[:country_code] == country_code }
249
+ match = @data.select { |_k, v| v[:country_code] == country_code }
246
250
  if match.size > 1
247
- match = match.select { |k, v| v[:main_country_for_code] == 'true' }
251
+ match = match.select { |_k, v| v[:main_country_for_code] == 'true' }
248
252
  end
249
253
 
250
254
  match.keys.first
251
255
  end
252
256
  end
253
257
  end
254
- end
258
+ end
@@ -1,5 +1,5 @@
1
1
  module Phonelib
2
- # helper module for parsing raw libphonenumber data
2
+ # @private helper module for parsing raw libphonenumber data
3
3
  module DataImporterHelper
4
4
  # xml comments attributes names that should not be parsed
5
5
  XML_COMMENT_ATTRIBUTES = %w(text comment)
@@ -20,7 +20,7 @@ module Phonelib
20
20
  end
21
21
 
22
22
  # method for checking if element name is not a format element
23
- def is_not_format(name)
23
+ def not_format?(name)
24
24
  !XML_FORMAT_NAMES.include? name
25
25
  end
26
26
 
@@ -35,18 +35,18 @@ module Phonelib
35
35
  def get_hash_from_xml(data, type)
36
36
  hash = {}
37
37
  case type
38
- when :attributes
39
- data.attributes.each do |k, v|
40
- hash[name2sym(k)] = str_clean(v)
41
- end
42
- when :children
43
- data.each do |f|
44
- hash[name2sym(f[0])] = f[1]
45
- end
46
- when :element
47
- data.elements.each do |child|
48
- hash[name2sym(child.name)] = str_clean(child.children.first)
49
- end
38
+ when :attributes
39
+ data.attributes.each do |k, v|
40
+ hash[name2sym(k)] = str_clean(v)
41
+ end
42
+ when :children
43
+ data.each do |f|
44
+ hash[name2sym(f[0])] = f[1]
45
+ end
46
+ when :element
47
+ data.elements.each do |child|
48
+ hash[name2sym(child.name)] = str_clean(child.children.first)
49
+ end
50
50
  end
51
51
  hash
52
52
  end
@@ -66,7 +66,7 @@ module Phonelib
66
66
  # get main body from parsed xml document
67
67
  def main_from_xml(file)
68
68
  xml_data = File.read(file)
69
- xml_data.force_encoding("utf-8")
69
+ xml_data.force_encoding('utf-8')
70
70
 
71
71
  doc = Nokogiri::XML(xml_data)
72
72
  doc.elements.first.elements.first
@@ -87,4 +87,4 @@ module Phonelib
87
87
  s.gsub(/[A-Z]+/) { |m| "_#{m.downcase}" }
88
88
  end
89
89
  end
90
- end
90
+ end
@@ -1,26 +1,26 @@
1
1
  module Phonelib
2
2
  # class for parsed phone number, includes validation and formatting methods
3
3
  class Phone
4
- # defining reader methods for class variables
5
- # original phone number passed for parsing
4
+ # @!attribute [r] original
5
+ # @return [String] original phone number passed for parsing
6
6
  attr_reader :original
7
- # phone extension passed for parsing after a number
7
+ # @!attribute [r] extension
8
+ # @return [String] phone extension passed for parsing after a number
8
9
  attr_reader :extension
9
10
 
10
11
  # including module that has all phone analyzing methods
11
12
  include Phonelib::PhoneAnalyzer
12
13
  include Phonelib::PhoneExtendedData
14
+ include Phonelib::PhoneFormatter
13
15
 
14
16
  # class initialization method
15
- #
16
- # ==== Attributes
17
- #
18
- # * +phone+ - Phone number for parsing
19
- # * +country+ - Country specification for parsing. Must be ISO code of
20
- # country (2 letters) like 'US', 'us' or :us for United States
21
- #
22
- def initialize(original, country = nil)
23
- @original, @extension = separate_extension(original)
17
+ # @param phone [String] Phone number for parsing
18
+ # @param country [String|Symbol] Country specification for parsing.
19
+ # Must be ISO code of country (2 letters) like 'US', 'us' or :us
20
+ # for United States
21
+ # @return [Phonelib::Phone] parsed phone instance
22
+ def initialize(phone, country = nil)
23
+ @original, @extension = separate_extension(phone.to_s)
24
24
  @extension.gsub!(/[^0-9]/, '') if @extension
25
25
 
26
26
  if sanitized.empty?
@@ -33,6 +33,7 @@ module Phonelib
33
33
  end
34
34
 
35
35
  # method to get sanitized phone number (only numbers)
36
+ # @return [String] Sanitized phone number
36
37
  def sanitized
37
38
  @sanitized = if Phonelib.strict_check
38
39
  @original
@@ -42,36 +43,43 @@ module Phonelib
42
43
  end
43
44
 
44
45
  # Returns all phone types that matched valid patterns
46
+ # @return [Array] all valid phone types
45
47
  def types
46
- @data.flat_map { |iso2, data| data[:valid] }.uniq
48
+ @data.flat_map { |_iso2, data| data[:valid] }.uniq
47
49
  end
48
50
 
49
51
  # Returns all possible types that matched possible patterns
52
+ # @return [Array] all possible phone types
50
53
  def possible_types
51
- @data.flat_map { |iso2, data| data[:possible] }.uniq
54
+ @data.flat_map { |_iso2, data| data[:possible] }.uniq
52
55
  end
53
56
 
54
57
  # Returns first phone type that matched
58
+ # @return [Symbol] valid phone type
55
59
  def type
56
60
  types.first
57
61
  end
58
62
 
59
63
  # Returns human representation of all matched phone types
64
+ # @return [Array] Array of human readable valid phone types
60
65
  def human_types
61
66
  types.map { |type| Core::TYPES_DESC[type] }
62
67
  end
63
68
 
64
69
  # Return human representation of phone type
70
+ # @return [String] Human readable valid phone type
65
71
  def human_type
66
72
  Core::TYPES_DESC[type]
67
73
  end
68
74
 
69
75
  # Returns all countries that matched valid patterns
76
+ # @return [Array] Possible ISO2 country codes array
70
77
  def countries
71
- @data.map { |iso2, data| iso2 }
78
+ @data.map { |iso2, _data| iso2 }
72
79
  end
73
80
 
74
81
  # Return countries with valid patterns
82
+ # @return [Array] Valid ISO2 country codes array
75
83
  def valid_countries
76
84
  @valid_countries ||= countries.select do |iso2|
77
85
  @data[iso2][:valid].any?
@@ -79,124 +87,60 @@ module Phonelib
79
87
  end
80
88
 
81
89
  # Return valid country
90
+ # @return [String] valid ISO2 country code
82
91
  def valid_country
83
- @valid_country ||= get_main_country(valid_countries)
92
+ @valid_country ||= main_country(valid_countries)
84
93
  end
85
94
 
86
95
  # Returns first country that matched valid patterns
96
+ # @return [String] valid country ISO2 code or first matched country code
87
97
  def country
88
- @country ||= valid_country || get_main_country(countries)
89
- end
90
-
91
- # Returns the country code from the original phone number.
92
- def country_code
93
- if country_data = Phonelib.phone_data[country]
94
- country_data[:country_code]
95
- end
98
+ @country ||= valid_country || main_country(countries)
96
99
  end
97
100
 
98
101
  # Returns whether a current parsed phone number is valid
102
+ # @return [Boolean] parsed phone is valid
99
103
  def valid?
100
- @data.select { |iso2, data| data[:valid].any? }.any?
104
+ @data.select { |_iso2, data| data[:valid].any? }.any?
101
105
  end
102
106
 
103
107
  # Returns whether a current parsed phone number is invalid
108
+ # @return [Boolean] parsed phone is invalid
104
109
  def invalid?
105
110
  !valid?
106
111
  end
107
112
 
108
113
  # Returns whether a current parsed phone number is possible
114
+ # @return [Boolean] parsed phone is possible
109
115
  def possible?
110
- @data.select { |iso2, data| data[:possible].any? }.any?
116
+ @data.select { |_iso2, data| data[:possible].any? }.any?
111
117
  end
112
118
 
113
119
  # Returns whether a current parsed phone number is impossible
120
+ # @return [Boolean] parsed phone is impossible
114
121
  def impossible?
115
122
  !possible?
116
123
  end
117
124
 
118
- # returns area code of parsed number
119
- def area_code
120
- return nil unless possible?
121
- format_match, format_string = get_formatting_data
122
-
123
- if format_string =~ /^.*[0-9]+.*\$1/ && format_match
124
- format_string.gsub(/\$1.*$/, format_match[1]).gsub(/[^\d]+/, '')
125
- end
126
- end
127
-
128
125
  # returns local number
126
+ # @return [String] local number
129
127
  def local_number
130
128
  return national unless possible?
131
- format_match, format_string = get_formatting_data
129
+ format_match, format_string = formatting_data
132
130
 
133
131
  if format_string =~ /^.*[0-9]+.*\$1/ && format_match
134
- format_string.gsub(/^.*\$2/, '$2').
135
- gsub(/\$\d/) { |el| format_match[el[1].to_i] }
132
+ format_string.gsub(/^.*\$2/, '$2')
133
+ .gsub(/\$\d/) { |el| format_match[el[1].to_i] }
136
134
  else
137
135
  national
138
136
  end
139
137
  end
140
138
 
141
- # Returns formatted national number
142
- def national(formatted = true)
143
- return @national_number unless valid?
144
- format_match, format_string = get_formatting_data
145
-
146
- if format_match
147
- out = format_string.gsub(/\$\d/) { |el| format_match[el[1].to_i] }
148
- formatted ? out : out.gsub(/[^0-9]/, '')
149
- else
150
- @national_number
151
- end
152
- end
153
-
154
- # Returns e164 formatted phone number
155
- def international(formatted = true)
156
- return nil if sanitized.nil? || sanitized.empty?
157
- return "+#{country_prefix_or_not}#{sanitized}" unless valid?
158
- return "#{@data[country][Core::COUNTRY_CODE]}#{@national_number}" unless formatted
159
-
160
- format = @data[country][:format]
161
- if matches = @national_number.match(/#{format[Core::PATTERN]}/)
162
- fmt = format[:intl_format] || format[:format]
163
- national = fmt.gsub(/\$\d/) { |el| matches[el[1].to_i] }
164
- else
165
- national = @national_number
166
- end
167
-
168
- "+#{country_code} #{national}"
169
- end
170
-
171
- # returns national formatted number with extension added
172
- def full_national
173
- "#{national}#{formatted_extension}"
174
- end
175
-
176
- # returns international formatted number with extension added
177
- def full_international
178
- "#{international}#{formatted_extension}"
179
- end
180
-
181
- # returns e164 format of phone with extension added
182
- def full_e164
183
- "#{e164}#{formatted_extension}"
184
- end
185
-
186
- # Returns e164 unformatted phone number
187
- def e164
188
- international = self.international
189
- international and international.gsub /[^+0-9]/, ''
190
- end
191
-
192
139
  # Returns whether a current parsed phone number is valid for specified
193
140
  # country
194
- #
195
- # ==== Attributes
196
- #
197
- # * +country+ - ISO code of country (2 letters) like 'US', 'us' or :us
198
- # for United States
199
- #
141
+ # @param country [String|Symbol] ISO code of country (2 letters) like 'US',
142
+ # 'us' or :us for United States
143
+ # @return [Boolean] parsed phone number is valid
200
144
  def valid_for_country?(country)
201
145
  country = country.to_s.upcase
202
146
  @data.find do |iso2, data|
@@ -206,61 +150,27 @@ module Phonelib
206
150
 
207
151
  # Returns whether a current parsed phone number is invalid for specified
208
152
  # country
209
- #
210
- # ==== Attributes
211
- #
212
- # * +country+ - ISO code of country (2 letters) like 'US', 'us' or :us
213
- # for United States
214
- #
153
+ # @param country [String|Symbol] ISO code of country (2 letters) like 'US',
154
+ # 'us' or :us for United States
155
+ # @return [Boolean] parsed phone number is invalid
215
156
  def invalid_for_country?(country)
216
157
  !valid_for_country?(country)
217
158
  end
218
159
 
219
160
  private
220
161
 
221
- def country_prefix_or_not
222
- return '' unless country_code
223
- sanitized.start_with?(country_code) ? '' : country_code
224
- end
225
-
226
- # returns extension with separator defined
227
- def formatted_extension
228
- return '' if @extension.nil? || @extension.empty?
229
-
230
- "#{Phonelib.extension_separator}#{@extension}"
231
- end
232
-
233
- # extracts extension from passed phone number if provided
162
+ # @private extracts extension from passed phone number if provided
234
163
  def separate_extension(original)
235
164
  regex = cr("[#{Phonelib.extension_separate_symbols}]")
236
165
  split = (original || '').split regex
237
- [split.first, split[1..-1] && split[1..-1].join]
166
+ [split.first || '', split[1..-1] && split[1..-1].join || '']
238
167
  end
239
168
 
240
- # get main country for code among provided countries
241
- def get_main_country(countries_array)
169
+ # @private get main country for code among provided countries
170
+ def main_country(countries_array)
242
171
  countries_array.find do |iso2|
243
172
  @data[iso2][Core::MAIN_COUNTRY_FOR_CODE] == 'true'
244
173
  end || countries_array.first
245
174
  end
246
-
247
- # Get needable data for formatting phone as national number
248
- def get_formatting_data
249
- return @formatting_data if @formatting_data
250
-
251
- format = @data[country][:format]
252
- prefix = @data[country][Core::NATIONAL_PREFIX]
253
- rule = (format[Core::NATIONAL_PREFIX_RULE] ||
254
- @data[country][Core::NATIONAL_PREFIX_RULE] || '$1')
255
-
256
- # change rule's constants to values
257
- rule.gsub!(/(\$NP|\$FG)/, '$NP' => prefix, '$FG' => '$1')
258
-
259
- # add space to format groups, change first group to rule,
260
- format_string = format[:format].gsub(/(\d)\$/, '\\1 $').gsub('$1', rule)
261
-
262
- @formatting_data =
263
- [@national_number.match(/#{format[Core::PATTERN]}/), format_string]
264
- end
265
175
  end
266
176
  end