russian_phone 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/russian_phone/number.rb +291 -293
- data/lib/russian_phone/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de2b1c829af8ca3af5a9e976e456ab5a543c3089
|
4
|
+
data.tar.gz: 38063084067c0c3683d12a1f579fb130ab2dad3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a76aa835d65eccdbcd6734342a0856c729868a3b6c361b26d9c82fb67bc6d08fd9a25fe1dc7ea0c8f1b3b2f08a36af70069b9c855274adbff5468eb01858deeb
|
7
|
+
data.tar.gz: b63665d691b789c1c7b3e40a0a4b2edd4d1879d2bfef7ad454bc6fa8bac2db870b994a237e3174aa3e42b7db5c532dc41eba6ac618cc867a253eb49323016841
|
data/lib/russian_phone/number.rb
CHANGED
@@ -1,294 +1,292 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
module RussianPhone
|
4
|
-
class Number
|
5
|
-
attr_accessor :phone, :options
|
6
|
-
|
7
|
-
def initialize(phone, options = {})
|
8
|
-
@options = self.class.process_options(options)
|
9
|
-
@phone = phone.to_s
|
10
|
-
end
|
11
|
-
|
12
|
-
def ==(other)
|
13
|
-
if other.class == self.class
|
14
|
-
# (other.phone == self.phone && other.options == self.options)
|
15
|
-
other.to_s == to_s
|
16
|
-
elsif other.class == String
|
17
|
-
parsed = RussianPhone::Number.new(other)
|
18
|
-
parsed.to_s == to_s
|
19
|
-
# parsed.phone == self.phone && parsed.options == self.options
|
20
|
-
else
|
21
|
-
false
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def coerce(something)
|
26
|
-
[self, something]
|
27
|
-
end
|
28
|
-
|
29
|
-
def parse(field)
|
30
|
-
data = self.class.parse(@phone, @options)
|
31
|
-
return nil if data.nil?
|
32
|
-
|
33
|
-
if data.has_key? :subscriber
|
34
|
-
@subscriber = data[:subscriber].to_s
|
35
|
-
@city = data[:city].to_s
|
36
|
-
@country = data[:country].to_s
|
37
|
-
@extra = data[:extra].to_s
|
38
|
-
end
|
39
|
-
|
40
|
-
data.has_key?(field) ? data[field] : nil
|
41
|
-
end
|
42
|
-
|
43
|
-
def valid?
|
44
|
-
@valid ||= !(country.nil? || city.nil? || subscriber.nil? || country == '' || city == '' || subscriber == '')
|
45
|
-
end
|
46
|
-
|
47
|
-
def blank?
|
48
|
-
@phone.strip == ''
|
49
|
-
end
|
50
|
-
|
51
|
-
def empty?
|
52
|
-
@phone.strip == ''
|
53
|
-
end
|
54
|
-
|
55
|
-
def subscriber
|
56
|
-
@subscriber ||= parse(:subscriber)
|
57
|
-
end
|
58
|
-
|
59
|
-
def city_allowed?
|
60
|
-
options[:allowed_cities].nil? || options[:allowed_cities].include?(city)
|
61
|
-
end
|
62
|
-
|
63
|
-
def city
|
64
|
-
@city ||= parse(:city)
|
65
|
-
end
|
66
|
-
alias_method :area, :city
|
67
|
-
|
68
|
-
def country
|
69
|
-
@country ||= parse(:country)
|
70
|
-
end
|
71
|
-
|
72
|
-
def extra
|
73
|
-
@extra ||= parse(:extra)
|
74
|
-
end
|
75
|
-
|
76
|
-
def split(format, number)
|
77
|
-
number = number.dup
|
78
|
-
format.inject([]) do |result, size|
|
79
|
-
result << number.slice!(0..size-1)
|
80
|
-
return result if number.empty?
|
81
|
-
result
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
def format
|
86
|
-
if subscriber.length == 7
|
87
|
-
split([3, 2, 2], subscriber)
|
88
|
-
elsif subscriber.length == 6
|
89
|
-
split([2, 2, 2], subscriber)
|
90
|
-
elsif subscriber.length == 5
|
91
|
-
split([1, 2, 2], subscriber)
|
92
|
-
else
|
93
|
-
[]
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
def formatted_area
|
98
|
-
area.nil? ? '' : "(#{area})"
|
99
|
-
end
|
100
|
-
|
101
|
-
def formatted_subscriber
|
102
|
-
subscriber.nil? ? '' : format.join('-')
|
103
|
-
end
|
104
|
-
|
105
|
-
def full
|
106
|
-
if valid?
|
107
|
-
if free? && extra == ''
|
108
|
-
"8-#{area}-#{formatted_subscriber}"
|
109
|
-
else
|
110
|
-
"+#{country} #{formatted_area} #{formatted_subscriber}#{extra == '' ? '' : ' ' + extra}"
|
111
|
-
end
|
112
|
-
else
|
113
|
-
''
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
def clean
|
118
|
-
"#{country}#{area}#{subscriber}"
|
119
|
-
end
|
120
|
-
|
121
|
-
def cell?
|
122
|
-
Codes.cell_codes.include?(area)
|
123
|
-
end
|
124
|
-
|
125
|
-
def free?
|
126
|
-
area == '800'
|
127
|
-
end
|
128
|
-
|
129
|
-
# alias_method(:to_s, :full)
|
130
|
-
# alias_method(:inspect, :full)
|
131
|
-
|
132
|
-
def to_s
|
133
|
-
valid? ? full : @phone
|
134
|
-
end
|
135
|
-
|
136
|
-
|
137
|
-
def inspect
|
138
|
-
# '"' + full + '"'
|
139
|
-
'"' + to_s + '"'
|
140
|
-
end
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
#
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
opts[:
|
197
|
-
|
198
|
-
opts
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
string = string.
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
clean_string
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
end
|
293
|
-
end
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module RussianPhone
|
4
|
+
class Number
|
5
|
+
attr_accessor :phone, :options
|
6
|
+
|
7
|
+
def initialize(phone, options = {})
|
8
|
+
@options = self.class.process_options(options)
|
9
|
+
@phone = phone.to_s
|
10
|
+
end
|
11
|
+
|
12
|
+
def ==(other)
|
13
|
+
if other.class == self.class
|
14
|
+
# (other.phone == self.phone && other.options == self.options)
|
15
|
+
other.to_s == to_s
|
16
|
+
elsif other.class == String
|
17
|
+
parsed = RussianPhone::Number.new(other)
|
18
|
+
parsed.to_s == to_s
|
19
|
+
# parsed.phone == self.phone && parsed.options == self.options
|
20
|
+
else
|
21
|
+
false
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def coerce(something)
|
26
|
+
[self, something]
|
27
|
+
end
|
28
|
+
|
29
|
+
def parse(field)
|
30
|
+
data = self.class.parse(@phone, @options)
|
31
|
+
return nil if data.nil?
|
32
|
+
|
33
|
+
if data.has_key? :subscriber
|
34
|
+
@subscriber = data[:subscriber].to_s
|
35
|
+
@city = data[:city].to_s
|
36
|
+
@country = data[:country].to_s
|
37
|
+
@extra = data[:extra].to_s
|
38
|
+
end
|
39
|
+
|
40
|
+
data.has_key?(field) ? data[field] : nil
|
41
|
+
end
|
42
|
+
|
43
|
+
def valid?
|
44
|
+
@valid ||= !(country.nil? || city.nil? || subscriber.nil? || country == '' || city == '' || subscriber == '')
|
45
|
+
end
|
46
|
+
|
47
|
+
def blank?
|
48
|
+
@phone.strip == ''
|
49
|
+
end
|
50
|
+
|
51
|
+
def empty?
|
52
|
+
@phone.strip == ''
|
53
|
+
end
|
54
|
+
|
55
|
+
def subscriber
|
56
|
+
@subscriber ||= parse(:subscriber)
|
57
|
+
end
|
58
|
+
|
59
|
+
def city_allowed?
|
60
|
+
options[:allowed_cities].nil? || options[:allowed_cities].include?(city)
|
61
|
+
end
|
62
|
+
|
63
|
+
def city
|
64
|
+
@city ||= parse(:city)
|
65
|
+
end
|
66
|
+
alias_method :area, :city
|
67
|
+
|
68
|
+
def country
|
69
|
+
@country ||= parse(:country)
|
70
|
+
end
|
71
|
+
|
72
|
+
def extra
|
73
|
+
@extra ||= parse(:extra)
|
74
|
+
end
|
75
|
+
|
76
|
+
def split(format, number)
|
77
|
+
number = number.dup
|
78
|
+
format.inject([]) do |result, size|
|
79
|
+
result << number.slice!(0..size-1)
|
80
|
+
return result if number.empty?
|
81
|
+
result
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def format
|
86
|
+
if subscriber.length == 7
|
87
|
+
split([3, 2, 2], subscriber)
|
88
|
+
elsif subscriber.length == 6
|
89
|
+
split([2, 2, 2], subscriber)
|
90
|
+
elsif subscriber.length == 5
|
91
|
+
split([1, 2, 2], subscriber)
|
92
|
+
else
|
93
|
+
[]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def formatted_area
|
98
|
+
area.nil? ? '' : "(#{area})"
|
99
|
+
end
|
100
|
+
|
101
|
+
def formatted_subscriber
|
102
|
+
subscriber.nil? ? '' : format.join('-')
|
103
|
+
end
|
104
|
+
|
105
|
+
def full
|
106
|
+
if valid?
|
107
|
+
if free? && extra == ''
|
108
|
+
"8-#{area}-#{formatted_subscriber}"
|
109
|
+
else
|
110
|
+
"+#{country} #{formatted_area} #{formatted_subscriber}#{extra == '' ? '' : ' ' + extra}"
|
111
|
+
end
|
112
|
+
else
|
113
|
+
''
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def clean
|
118
|
+
"#{country}#{area}#{subscriber}"
|
119
|
+
end
|
120
|
+
|
121
|
+
def cell?
|
122
|
+
Codes.cell_codes.include?(area)
|
123
|
+
end
|
124
|
+
|
125
|
+
def free?
|
126
|
+
area == '800'
|
127
|
+
end
|
128
|
+
|
129
|
+
# alias_method(:to_s, :full)
|
130
|
+
# alias_method(:inspect, :full)
|
131
|
+
|
132
|
+
def to_s
|
133
|
+
valid? ? full : @phone
|
134
|
+
end
|
135
|
+
|
136
|
+
|
137
|
+
def inspect
|
138
|
+
# '"' + full + '"'
|
139
|
+
'"' + to_s + '"'
|
140
|
+
end
|
141
|
+
|
142
|
+
def as_json(options = {})
|
143
|
+
to_s
|
144
|
+
end
|
145
|
+
|
146
|
+
alias_method(:mongoize, :to_s)
|
147
|
+
|
148
|
+
class << self
|
149
|
+
def clean(string)
|
150
|
+
string.tr('^0-9', '')
|
151
|
+
end
|
152
|
+
|
153
|
+
#def _extract(string, subscriber_digits, code_digits)
|
154
|
+
# [string[-(subscriber_digits + code_digits), code_digits], string[-subscriber_digits,subscriber_digits]]
|
155
|
+
#end
|
156
|
+
|
157
|
+
def _extract(string, subscriber_digits, code_digits)
|
158
|
+
[string[0, code_digits], string[code_digits, subscriber_digits]]
|
159
|
+
end
|
160
|
+
|
161
|
+
def extract(string, subscriber_digits, code_digits)
|
162
|
+
_extract(clean(string), subscriber_digits, code_digits)
|
163
|
+
end
|
164
|
+
|
165
|
+
def _extra(string, position)
|
166
|
+
i = 0
|
167
|
+
digits = 0
|
168
|
+
|
169
|
+
string.each_char do |char|
|
170
|
+
if char.match(/[0-9]/)
|
171
|
+
digits += 1
|
172
|
+
end
|
173
|
+
i += 1
|
174
|
+
# puts "#{char} #{digits} #{i} #{position}"
|
175
|
+
if digits >= position
|
176
|
+
return string[i..-1].strip
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
''
|
181
|
+
end
|
182
|
+
|
183
|
+
def country_code(string)
|
184
|
+
clean(string)[-10, 1]
|
185
|
+
end
|
186
|
+
|
187
|
+
def process_options(opts = {})
|
188
|
+
opts = {
|
189
|
+
default_country: 7,
|
190
|
+
default_city: nil,
|
191
|
+
allowed_cities: nil,
|
192
|
+
}.merge(opts)
|
193
|
+
|
194
|
+
opts[:default_country] = opts[:default_country].to_s unless opts[:default_country].nil?
|
195
|
+
opts[:default_city] = opts[:default_city].to_s unless opts[:default_city].nil?
|
196
|
+
opts[:allowed_cities] = opts[:allowed_cities].map { |c| c.to_s } unless opts[:allowed_cities].nil?
|
197
|
+
|
198
|
+
opts
|
199
|
+
end
|
200
|
+
|
201
|
+
def parse(string, opts = {})
|
202
|
+
return string if string.class.name == 'RussianPhone::Number'
|
203
|
+
|
204
|
+
opts = process_options(opts)
|
205
|
+
|
206
|
+
if string.class.name == 'Array'
|
207
|
+
string = string.join('')
|
208
|
+
else
|
209
|
+
string = string.to_s
|
210
|
+
end
|
211
|
+
|
212
|
+
clean_string = clean(string)
|
213
|
+
|
214
|
+
if clean_string.length < 10
|
215
|
+
if opts[:default_city].nil?
|
216
|
+
return nil
|
217
|
+
elsif clean_string.length > 7
|
218
|
+
# Телефон слишком длинный для телефона без кода города
|
219
|
+
return nil
|
220
|
+
else
|
221
|
+
if Codes.codes_for(clean_string.length).include? opts[:default_city]
|
222
|
+
return {country: opts[:default_country], city: opts[:default_city], subscriber: clean_string}
|
223
|
+
else
|
224
|
+
# Количество цифр в телефоне не соответствует количеству цифр местных номеров города
|
225
|
+
return nil
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
extra_after = 10
|
231
|
+
|
232
|
+
if clean_string.length > 10 && string.starts_with?('+7') || string.starts_with?('8 ') || string.starts_with?('8(') || string.starts_with?('8-')
|
233
|
+
clean_string[0] = ''
|
234
|
+
extra_after += 1
|
235
|
+
end
|
236
|
+
|
237
|
+
if clean_string.length == 11 && string.starts_with?('7')
|
238
|
+
clean_string[0] = ''
|
239
|
+
extra_after += 1
|
240
|
+
end
|
241
|
+
|
242
|
+
if clean_string.length == 11 && string.starts_with?('8')
|
243
|
+
clean_string[0] = ''
|
244
|
+
extra_after += 1
|
245
|
+
end
|
246
|
+
|
247
|
+
# handles stuff like 89061010101 д. 123
|
248
|
+
if string.split(' ').length > 1 && string.split(/\D/)[0].length > 10
|
249
|
+
if string.starts_with?('7')
|
250
|
+
clean_string[0] = ''
|
251
|
+
extra_after += 1
|
252
|
+
end
|
253
|
+
|
254
|
+
if string.starts_with?('8')
|
255
|
+
clean_string[0] = ''
|
256
|
+
extra_after += 1
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
# handle 7906123-12-12 доб 123
|
261
|
+
if clean_string.length > 10 && extra_after == 10 && (clean_string[0] == '7' || clean_string[0] == '8')
|
262
|
+
result = ''
|
263
|
+
string.split(/\D/).each do |segm|
|
264
|
+
result += segm
|
265
|
+
break if result.length >= 10
|
266
|
+
end
|
267
|
+
if result.length > 10
|
268
|
+
clean_string[0] = ''
|
269
|
+
extra_after += 1
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
code_3_digit, phone_7_digit = _extract(clean_string, 7, 3)
|
274
|
+
if code_3_digit == '800' || Codes.cell_codes.include?(code_3_digit) || Codes.ndcs_with_7_subscriber_digits.include?(code_3_digit)
|
275
|
+
return {country: opts[:default_country], city: code_3_digit, subscriber: phone_7_digit, extra: _extra(string, extra_after)}
|
276
|
+
end
|
277
|
+
|
278
|
+
code_4_digit, phone_6_digit = _extract(clean_string, 6, 4)
|
279
|
+
if Codes.ndcs_with_6_subscriber_digits.include? code_4_digit
|
280
|
+
return {country: opts[:default_country], city: code_4_digit, subscriber: phone_6_digit, extra: _extra(string, extra_after)}
|
281
|
+
end
|
282
|
+
|
283
|
+
code_5_digit, phone_5_digit = _extract(clean_string, 5, 5)
|
284
|
+
if Codes.ndcs_with_5_subscriber_digits.include? code_5_digit
|
285
|
+
return {country: opts[:default_country], city: code_5_digit, subscriber: phone_5_digit, extra: _extra(string, extra_after)}
|
286
|
+
end
|
287
|
+
|
288
|
+
return {country: opts[:default_country], city: code_3_digit, subscriber: phone_7_digit, extra: _extra(string, extra_after)}
|
289
|
+
end
|
290
|
+
end
|
291
|
+
end
|
294
292
|
end
|