lib/russian_phone/number.rb in russian_phone-0.1.1 vs lib/russian_phone/number.rb in russian_phone-0.2.4
- old
+ new
@@ -32,10 +32,11 @@
if data.has_key? :subscriber
@subscriber = data[:subscriber].to_s
@city = data[:city].to_s
@country = data[:country].to_s
+ @extra = data[:extra].to_s
end
data.has_key?(field) ? data[field] : nil
end
@@ -57,10 +58,14 @@
def country
@country ||= parse(:country)
end
+ def extra
+ @extra ||= parse(:extra)
+ end
+
def split(format, number)
number = number.dup
format.inject([]) do |result, size|
result << number.slice!(0..size-1)
return result if number.empty?
@@ -80,14 +85,14 @@
end
end
def full
if valid?
- if free?
+ if free? && extra == ''
"8-#{city}-#{format.join('-')}"
else
- "+#{country} (#{city}) #{format.join('-')}"
+ "+#{country} (#{city}) #{format.join('-')}#{extra == '' ? '' : ' ' + extra}"
end
else
''
end
end
@@ -116,27 +121,52 @@
# '"' + full + '"'
'"' + to_s + '"'
end
#def mongoize
- # valid? ? full : @phone
+ # @phone
#end
+ # alias_method(:as_json, :mongoize)
+
+ alias_method(:as_json, :to_s)
alias_method(:mongoize, :to_s)
class << self
def clean(string)
string.tr('^0-9', '')
end
+ #def _extract(string, subscriber_digits, code_digits)
+ # [string[-(subscriber_digits + code_digits), code_digits], string[-subscriber_digits,subscriber_digits]]
+ #end
+
def _extract(string, subscriber_digits, code_digits)
- [string[-(subscriber_digits + code_digits), code_digits], string[-subscriber_digits,subscriber_digits]]
+ [string[0, code_digits], string[code_digits, subscriber_digits]]
end
def extract(string, subscriber_digits, code_digits)
_extract(clean(string), subscriber_digits, code_digits)
end
+ def _extra(string, position)
+ i = 0
+ digits = 0
+
+ string.each_char do |char|
+ if char.match(/[0-9]/)
+ digits += 1
+ end
+ i += 1
+ # puts "#{char} #{digits} #{i} #{position}"
+ if digits >= position
+ return string[i..-1].strip
+ end
+ end
+
+ ''
+ end
+
def country_code(string)
clean(string)[-10, 1]
end
def process_options(opts = {})
@@ -164,14 +194,10 @@
string = string.to_s
end
clean_string = clean(string)
- if clean_string.length > 11
- return nil
- end
-
if clean_string.length < 10
if opts[:default_city].nil?
return nil
elsif clean_string.length > 7
# Телефон слишком длинный для телефона без кода города
@@ -184,25 +210,68 @@
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}
+ 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}
+ 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}
+ return {country: opts[:default_country], city: code_5_digit, subscriber: phone_5_digit, extra: _extra(string, extra_after)}
end
- nil
+ return {country: opts[:default_country], city: code_3_digit, subscriber: phone_7_digit, extra: _extra(string, extra_after)}
end
end
end
end
\ No newline at end of file