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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16d06f62f45a61c5481be6b1889d0615e889301b
4
- data.tar.gz: f36afbcdd5309dd268eb4b008e2639e1c56bb163
3
+ metadata.gz: de2b1c829af8ca3af5a9e976e456ab5a543c3089
4
+ data.tar.gz: 38063084067c0c3683d12a1f579fb130ab2dad3a
5
5
  SHA512:
6
- metadata.gz: 7f09c65d39d1aee28f8fbfb1d5b9fd47a21e05ab1da62cd7fcfe38401eeaa14e228937ecb9123e06678afd716d5d96fde22a170fb4c0394a84eb8d515c686ca5
7
- data.tar.gz: e89db6ae486c9068a29f8523f1cb76a1b5c6966c3eb8e50cbc421af14a1373b30b7c1782f7324508d0b6e67341ff4bf8b888b6eec143f1101599ac4bb50a3a2e
6
+ metadata.gz: a76aa835d65eccdbcd6734342a0856c729868a3b6c361b26d9c82fb67bc6d08fd9a25fe1dc7ea0c8f1b3b2f08a36af70069b9c855274adbff5468eb01858deeb
7
+ data.tar.gz: b63665d691b789c1c7b3e40a0a4b2edd4d1879d2bfef7ad454bc6fa8bac2db870b994a237e3174aa3e42b7db5c532dc41eba6ac618cc867a253eb49323016841
@@ -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
- #def mongoize
143
- # @phone
144
- #end
145
- # alias_method(:as_json, :mongoize)
146
-
147
- alias_method(:as_json, :to_s)
148
- alias_method(:mongoize, :to_s)
149
-
150
- class << self
151
- def clean(string)
152
- string.tr('^0-9', '')
153
- end
154
-
155
- #def _extract(string, subscriber_digits, code_digits)
156
- # [string[-(subscriber_digits + code_digits), code_digits], string[-subscriber_digits,subscriber_digits]]
157
- #end
158
-
159
- def _extract(string, subscriber_digits, code_digits)
160
- [string[0, code_digits], string[code_digits, subscriber_digits]]
161
- end
162
-
163
- def extract(string, subscriber_digits, code_digits)
164
- _extract(clean(string), subscriber_digits, code_digits)
165
- end
166
-
167
- def _extra(string, position)
168
- i = 0
169
- digits = 0
170
-
171
- string.each_char do |char|
172
- if char.match(/[0-9]/)
173
- digits += 1
174
- end
175
- i += 1
176
- # puts "#{char} #{digits} #{i} #{position}"
177
- if digits >= position
178
- return string[i..-1].strip
179
- end
180
- end
181
-
182
- ''
183
- end
184
-
185
- def country_code(string)
186
- clean(string)[-10, 1]
187
- end
188
-
189
- def process_options(opts = {})
190
- opts = {
191
- default_country: 7,
192
- default_city: nil,
193
- allowed_cities: nil,
194
- }.merge(opts)
195
-
196
- opts[:default_country] = opts[:default_country].to_s unless opts[:default_country].nil?
197
- opts[:default_city] = opts[:default_city].to_s unless opts[:default_city].nil?
198
- opts[:allowed_cities] = opts[:allowed_cities].map { |c| c.to_s } unless opts[:allowed_cities].nil?
199
-
200
- opts
201
- end
202
-
203
- def parse(string, opts = {})
204
- return string if string.class.name == 'RussianPhone::Number'
205
-
206
- opts = process_options(opts)
207
-
208
- if string.class.name == 'Array'
209
- string = string.join('')
210
- else
211
- string = string.to_s
212
- end
213
-
214
- clean_string = clean(string)
215
-
216
- if clean_string.length < 10
217
- if opts[:default_city].nil?
218
- return nil
219
- elsif clean_string.length > 7
220
- # Телефон слишком длинный для телефона без кода города
221
- return nil
222
- else
223
- if Codes.codes_for(clean_string.length).include? opts[:default_city]
224
- return {country: opts[:default_country], city: opts[:default_city], subscriber: clean_string}
225
- else
226
- # Количество цифр в телефоне не соответствует количеству цифр местных номеров города
227
- return nil
228
- end
229
- end
230
- end
231
-
232
- extra_after = 10
233
-
234
- if clean_string.length > 10 && string.starts_with?('+7') || string.starts_with?('8 ') || string.starts_with?('8(') || string.starts_with?('8-')
235
- clean_string[0] = ''
236
- extra_after += 1
237
- end
238
-
239
- if clean_string.length == 11 && string.starts_with?('7')
240
- clean_string[0] = ''
241
- extra_after += 1
242
- end
243
-
244
- if clean_string.length == 11 && string.starts_with?('8')
245
- clean_string[0] = ''
246
- extra_after += 1
247
- end
248
-
249
- # handles stuff like 89061010101 д. 123
250
- if string.split(' ').length > 1 && string.split(/\D/)[0].length > 10
251
- if string.starts_with?('7')
252
- clean_string[0] = ''
253
- extra_after += 1
254
- end
255
-
256
- if string.starts_with?('8')
257
- clean_string[0] = ''
258
- extra_after += 1
259
- end
260
- end
261
-
262
- # handle 7906123-12-12 доб 123
263
- if clean_string.length > 10 && extra_after == 10 && (clean_string[0] == '7' || clean_string[0] == '8')
264
- result = ''
265
- string.split(/\D/).each do |segm|
266
- result += segm
267
- break if result.length >= 10
268
- end
269
- if result.length > 10
270
- clean_string[0] = ''
271
- extra_after += 1
272
- end
273
- end
274
-
275
- code_3_digit, phone_7_digit = _extract(clean_string, 7, 3)
276
- if code_3_digit == '800' || Codes.cell_codes.include?(code_3_digit) || Codes.ndcs_with_7_subscriber_digits.include?(code_3_digit)
277
- return {country: opts[:default_country], city: code_3_digit, subscriber: phone_7_digit, extra: _extra(string, extra_after)}
278
- end
279
-
280
- code_4_digit, phone_6_digit = _extract(clean_string, 6, 4)
281
- if Codes.ndcs_with_6_subscriber_digits.include? code_4_digit
282
- return {country: opts[:default_country], city: code_4_digit, subscriber: phone_6_digit, extra: _extra(string, extra_after)}
283
- end
284
-
285
- code_5_digit, phone_5_digit = _extract(clean_string, 5, 5)
286
- if Codes.ndcs_with_5_subscriber_digits.include? code_5_digit
287
- return {country: opts[:default_country], city: code_5_digit, subscriber: phone_5_digit, extra: _extra(string, extra_after)}
288
- end
289
-
290
- return {country: opts[:default_country], city: code_3_digit, subscriber: phone_7_digit, extra: _extra(string, extra_after)}
291
- end
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
@@ -1,3 +1,3 @@
1
1
  module RussianPhone
2
- VERSION = "0.3.4"
2
+ VERSION = "0.3.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: russian_phone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - GlebTv