Method: RussianPhone::Number.parse

Defined in:
lib/russian_phone/number.rb

.parse(string, opts = {}) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
# File 'lib/russian_phone/number.rb', line 201

def parse(string, opts = {})
  return string if string.class.name == 'RussianPhone::Number'

  opts = process_options(opts)

  if string.class.name == 'Array'
    string = string.join('')
  else
    string = string.to_s
  end

  clean_string = clean(string)

  if clean_string.length < 10
    if opts[:default_city].nil?
      return nil
    elsif clean_string.length > 7
      # Телефон слишком длинный для телефона без кода города
      return nil
    else
      if Codes.codes_for(clean_string.length).include? opts[:default_city]
        return {country: opts[:default_country], city: opts[:default_city], subscriber: clean_string}
      else
        # Количество цифр в телефоне не соответствует количеству цифр местных номеров города
        return nil
      end
    end
  end

  extra_after = 10

  if clean_string.length > 10 && string.starts_with?('+7') || string.starts_with?('8 ') || string.starts_with?('8(') || string.starts_with?('8-')
    clean_string[0] = ''
    extra_after += 1
  end

  if clean_string.length == 11 && string.starts_with?('7')
    clean_string[0] = ''
    extra_after += 1
  end

  if clean_string.length == 11 && string.starts_with?('8')
    clean_string[0] = ''
    extra_after += 1
  end

  # handles stuff like 89061010101 д. 123
  if string.split(' ').length > 1 && string.split(/\D/)[0].length > 10
    if string.starts_with?('7')
      clean_string[0] = ''
      extra_after += 1
    end

    if string.starts_with?('8')
      clean_string[0] = ''
      extra_after += 1
    end
  end

  # handle 7906123-12-12 доб 123
  if clean_string.length > 10 && extra_after == 10 && (clean_string[0] == '7' || clean_string[0] == '8')
    result = ''
    string.split(/\D/).each do |segm|
      result += segm
      break if result.length >= 10
    end
    if result.length > 10
      clean_string[0] = ''
      extra_after += 1
    end
  end

  code_3_digit, phone_7_digit = _extract(clean_string, 7, 3)
  if code_3_digit == '800' || Codes.cell_codes.include?(code_3_digit) || Codes.ndcs_with_7_subscriber_digits.include?(code_3_digit)
    return {country: opts[:default_country], city: code_3_digit, subscriber: phone_7_digit, extra: _extra(string, extra_after)}
  end

  code_4_digit, phone_6_digit = _extract(clean_string, 6, 4)
  if Codes.ndcs_with_6_subscriber_digits.include? code_4_digit
    return {country: opts[:default_country], city: code_4_digit, subscriber: phone_6_digit, extra: _extra(string, extra_after)}
  end

  code_5_digit, phone_5_digit = _extract(clean_string, 5, 5)
  if Codes.ndcs_with_5_subscriber_digits.include? code_5_digit
    return {country: opts[:default_country], city: code_5_digit, subscriber: phone_5_digit, extra: _extra(string, extra_after)}
  end

  return {country: opts[:default_country], city: code_3_digit, subscriber: phone_7_digit, extra: _extra(string, extra_after)}
end