phonelib 0.2.8 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
data/data/phone_data.dat CHANGED
Binary file
data/lib/phonelib/core.rb CHANGED
@@ -98,17 +98,19 @@ module Phonelib
98
98
 
99
99
  # method for parsing phone number.
100
100
  # On first run fills @@phone_data with data present in yaml file
101
- def parse(phone, passed_country = nil)
101
+ def parse(original, passed_country = nil)
102
102
  load_data
103
+ sanitized = sanitize_phone original
103
104
 
104
105
  country = country_or_default_country(passed_country)
105
- if phone.nil? || country.nil?
106
+ if sanitized.empty?
106
107
  # has to return instance of Phonelib::Phone even if no phone passed
107
- Phonelib::Phone.new(phone, @@phone_data)
108
+ Phonelib::Phone.new(sanitized, original, @@phone_data)
108
109
  else
109
- detected = detect_and_parse_by_country(phone, country)
110
+ detected = detect_or_parse_by_country(sanitized, original, country)
110
111
  if passed_country.nil? && @@default_country && detected.invalid?
111
- Phonelib::Phone.new(phone, @@phone_data)
112
+ # try to detect country for number if it's invalid for specified one
113
+ detect_or_parse_by_country(sanitized, original)
112
114
  else
113
115
  detected
114
116
  end
@@ -160,11 +162,19 @@ module Phonelib
160
162
  end
161
163
 
162
164
  # Get Phone instance for provided phone with country specified
163
- def detect_and_parse_by_country(phone, country)
164
- detected = @@phone_data.find { |data| data[:id] == country }
165
- if detected
166
- phone = convert_phone_to_e164(phone, detected)
167
- Phonelib::Phone.new(phone, [detected])
165
+ def detect_or_parse_by_country(phone, original, country = nil)
166
+ if country.nil?
167
+ Phonelib::Phone.new(phone, original, @@phone_data)
168
+ else
169
+ detected = @@phone_data.find { |data| data[:id] == country }
170
+ if detected
171
+ phone = convert_phone_to_e164(phone, detected)
172
+ if phone[0] == '+'
173
+ detect_or_parse_by_country(phone[1..-1], original)
174
+ else
175
+ Phonelib::Phone.new(phone, original, [detected])
176
+ end
177
+ end
168
178
  end
169
179
  end
170
180
 
@@ -176,13 +186,18 @@ module Phonelib
176
186
  rx << "(#{data[Core::NATIONAL_PREFIX]})?"
177
187
  rx << "(#{data[Core::TYPES][Core::GENERAL][Core::VALID_PATTERN]})"
178
188
 
179
- match = phone.gsub('+', '').match(/^#{rx.join}$/)
189
+ match = phone.match(/^#{rx.join}$/)
180
190
  if match
181
- national_start = 1.upto(3).map {|i| match[i].to_s.length}.inject(:+)
191
+ national_start = (1..3).map { |i| match[i].to_s.length }.inject(:+)
182
192
  "#{data[Core::COUNTRY_CODE]}#{phone[national_start..-1]}"
183
193
  else
184
- phone
194
+ phone.sub(/^#{data[Core::INTERNATIONAL_PREFIX]}/, '+')
185
195
  end
186
196
  end
197
+
198
+ # Sanitizes passed phone number. Returns only digits from passed string.
199
+ def sanitize_phone(phone)
200
+ phone && phone.gsub(/[^0-9]+/, '') || ''
201
+ end
187
202
  end
188
203
  end
@@ -15,9 +15,9 @@ module Phonelib
15
15
  # * +phone+ - Phone number for parsing
16
16
  # * +country_data+ - Hash of data for parsing
17
17
  #
18
- def initialize(phone, country_data)
19
- @original = phone
20
- @sanitized = sanitize_phone(@original)
18
+ def initialize(sanitized, original, country_data)
19
+ @sanitized = sanitized
20
+ @original = original
21
21
  if @sanitized.empty?
22
22
  @analyzed_data = {}
23
23
  else
@@ -148,12 +148,5 @@ module Phonelib
148
148
  def invalid_for_country?(country)
149
149
  !valid_for_country?(country)
150
150
  end
151
-
152
- private
153
-
154
- # Sanitizes passed phone number. Returns only digits from passed string.
155
- def sanitize_phone(phone)
156
- phone && phone.gsub(/[^0-9]+/, '') || ''
157
- end
158
151
  end
159
152
  end
@@ -23,7 +23,7 @@ module Phonelib
23
23
  # provided phone number
24
24
  def get_national_and_data(phone, data, country_match)
25
25
  prefix_length = data[Core::COUNTRY_CODE].length
26
- prefix_length += country_match[1].length unless country_match[1].nil?
26
+ prefix_length += [1, 2].map {|i| country_match[i].to_s.length}.inject(:+)
27
27
  result = data.select { |k, v| ![:types, :formats].include?(k) }
28
28
  result[:national] = phone[prefix_length..-1]
29
29
  result[:format] = get_number_format(result[:national], data[Core::FORMATS])
@@ -36,8 +36,9 @@ module Phonelib
36
36
  country_code = "#{data[Core::COUNTRY_CODE]}"
37
37
  inter_prefix = "(#{data[Core::INTERNATIONAL_PREFIX]})?"
38
38
  if phone =~ /^#{inter_prefix}#{country_code}/
39
+ national_prefix = "(#{data[Core::NATIONAL_PREFIX]})?"
39
40
  _possible, valid = get_patterns(data[Core::TYPES], Core::GENERAL)
40
- phone.match /^#{inter_prefix}#{country_code}#{valid}$/
41
+ phone.match /^#{inter_prefix}#{country_code}#{national_prefix}#{valid}$/
41
42
  end
42
43
  end
43
44
 
@@ -1,5 +1,5 @@
1
1
  # :nodoc:
2
2
  module Phonelib
3
3
  # :nodoc:
4
- VERSION = '0.2.8'
4
+ VERSION = '0.2.9'
5
5
  end
metadata CHANGED
@@ -1,74 +1,82 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phonelib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.9
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - Vadim Senderovich
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-01-22 00:00:00.000000000 Z
12
+ date: 2014-02-02 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rake
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
- - - '>='
19
+ - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
21
  version: '0'
20
22
  type: :development
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
- - - '>='
27
+ - - ! '>='
25
28
  - !ruby/object:Gem::Version
26
29
  version: '0'
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: nokogiri
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
- - - '>='
35
+ - - ! '>='
32
36
  - !ruby/object:Gem::Version
33
37
  version: '0'
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
- - - '>='
43
+ - - ! '>='
39
44
  - !ruby/object:Gem::Version
40
45
  version: '0'
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: pry
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
- - - '>='
51
+ - - ! '>='
46
52
  - !ruby/object:Gem::Version
47
53
  version: '0'
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
- - - '>='
59
+ - - ! '>='
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  - !ruby/object:Gem::Dependency
56
63
  name: rspec
57
64
  requirement: !ruby/object:Gem::Requirement
65
+ none: false
58
66
  requirements:
59
- - - '>='
67
+ - - ! '>='
60
68
  - !ruby/object:Gem::Version
61
69
  version: '0'
62
70
  type: :development
63
71
  prerelease: false
64
72
  version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
65
74
  requirements:
66
- - - '>='
75
+ - - ! '>='
67
76
  - !ruby/object:Gem::Version
68
77
  version: '0'
69
- description: |2
70
- Google libphonenumber library was taken as a basis for
71
- this gem. Gem uses its data file for validations and number formatting.
78
+ description: ! " Google libphonenumber library was taken as a basis for\n this
79
+ gem. Gem uses its data file for validations and number formatting.\n"
72
80
  email:
73
81
  - daddyzgm@gmail.com
74
82
  executables: []
@@ -77,42 +85,47 @@ extra_rdoc_files: []
77
85
  files:
78
86
  - data/phone_data.dat
79
87
  - data/PhoneNumberMetaData.xml
80
- - lib/phonelib.rb
81
- - lib/phonelib/version.rb
82
- - lib/phonelib/phone_analyzer.rb
83
- - lib/phonelib/phone.rb
84
- - lib/phonelib/core.rb
85
88
  - lib/validators/phone_validator.rb
86
89
  - lib/tasks/phonelib_tasks.rake
90
+ - lib/phonelib/phone.rb
91
+ - lib/phonelib/core.rb
92
+ - lib/phonelib/version.rb
93
+ - lib/phonelib/phone_analyzer.rb
94
+ - lib/phonelib.rb
87
95
  - MIT-LICENSE
88
96
  - Rakefile
89
97
  - README.rdoc
90
98
  homepage: https://github.com/daddyz/phonelib
91
99
  licenses:
92
100
  - MIT
93
- metadata: {}
94
- post_install_message: |2
95
- IMPORTANT NOTICE!
96
- Phone types were changed from camel case to snake case!
97
- Example: ":tollFree" changed to ":toll_free".
98
- Please update your app in case your are checking types!
101
+ post_install_message: ! " IMPORTANT NOTICE!\n Phone types were changed from
102
+ camel case to snake case!\n Example: \":tollFree\" changed to \":toll_free\".\n
103
+ \ Please update your app in case your are checking types!\n"
99
104
  rdoc_options: []
100
105
  require_paths:
101
106
  - lib
102
107
  required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
103
109
  requirements:
104
- - - '>='
110
+ - - ! '>='
105
111
  - !ruby/object:Gem::Version
106
112
  version: '0'
113
+ segments:
114
+ - 0
115
+ hash: -3579515089575950219
107
116
  required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
108
118
  requirements:
109
- - - '>='
119
+ - - ! '>='
110
120
  - !ruby/object:Gem::Version
111
121
  version: '0'
122
+ segments:
123
+ - 0
124
+ hash: -3579515089575950219
112
125
  requirements: []
113
126
  rubyforge_project:
114
- rubygems_version: 2.0.3
127
+ rubygems_version: 1.8.23
115
128
  signing_key:
116
- specification_version: 4
129
+ specification_version: 3
117
130
  summary: Gem validates phone numbers with Google libphonenumber database
118
131
  test_files: []
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 0ff01b459bd1e85094346f7d122ecd50c4767dc8
4
- data.tar.gz: 437a1259ac26e6aa385dd5e1135eac3c11416f3c
5
- SHA512:
6
- metadata.gz: 034361056395d337e3e0762bb039d46db01b930c50859b65f15cfa1f4b4b9384588934f909450abdeabd6220eb5bd1a34972221639e6ffa095608c442d46c81d
7
- data.tar.gz: c29cd9f483a4cb2e0418e48faf7e63df5e6811006f93f61a4841bede73c3a0862c0865ef8ac0e03804a7915f4d36ba8d8941f7d0a975fc7214099d816b4e372b