Class: RussianPhone::Number

Inherits:
Object
  • Object
show all
Defined in:
lib/russian_phone/number.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(phone, options = {}) ⇒ Number

Returns a new instance of Number.



7
8
9
10
# File 'lib/russian_phone/number.rb', line 7

def initialize(phone, options = {})
  @options = self.class.process_options(options)
  @phone = phone.to_s
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/russian_phone/number.rb', line 5

def options
  @options
end

#phoneObject

Returns the value of attribute phone.



5
6
7
# File 'lib/russian_phone/number.rb', line 5

def phone
  @phone
end

Class Method Details

._extra(string, position) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/russian_phone/number.rb', line 158

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

._extract(string, subscriber_digits, code_digits) ⇒ Object

def _extract(string, subscriber_digits, code_digits)

[string[-(subscriber_digits + code_digits), code_digits], string[-subscriber_digits,subscriber_digits]]

end



150
151
152
# File 'lib/russian_phone/number.rb', line 150

def _extract(string, subscriber_digits, code_digits)
  [string[0, code_digits], string[code_digits, subscriber_digits]]
end

.clean(string) ⇒ Object



142
143
144
# File 'lib/russian_phone/number.rb', line 142

def clean(string)
  string.tr('^0-9', '')
end

.country_code(string) ⇒ Object



176
177
178
# File 'lib/russian_phone/number.rb', line 176

def country_code(string)
  clean(string)[-10, 1]
end

.extract(string, subscriber_digits, code_digits) ⇒ Object



154
155
156
# File 'lib/russian_phone/number.rb', line 154

def extract(string, subscriber_digits, code_digits)
  _extract(clean(string), subscriber_digits, code_digits)
end

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



194
195
196
197
198
199
200
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
# File 'lib/russian_phone/number.rb', line 194

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

.process_options(opts = {}) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/russian_phone/number.rb', line 180

def process_options(opts = {})
  opts = {
      default_country: 7,
      default_city: nil,
      allowed_cities: nil,
  }.merge(opts)

  opts[:default_country] = opts[:default_country].to_s unless opts[:default_country].nil?
  opts[:default_city]    = opts[:default_city].to_s    unless opts[:default_city].nil?
  opts[:allowed_cities]  = opts[:allowed_cities].map { |c| c.to_s } unless opts[:allowed_cities].nil?

  opts
end

Instance Method Details

#==(other) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/russian_phone/number.rb', line 12

def ==(other)
  if other.class == self.class
    # (other.phone == self.phone && other.options == self.options)
    other.to_s == to_s
  elsif other.class == String
    parsed = RussianPhone::Number.new(other)
    parsed.to_s == to_s
    # parsed.phone == self.phone && parsed.options == self.options
  else
    false
  end
end

#blank?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/russian_phone/number.rb', line 47

def blank?
  @phone.strip == ''
end

#cell?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/russian_phone/number.rb', line 112

def cell?
  Codes.cell_codes.include?(city)
end

#cityObject



63
64
65
# File 'lib/russian_phone/number.rb', line 63

def city
  @city ||= parse(:city)
end

#city_allowed?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/russian_phone/number.rb', line 59

def city_allowed?
  options[:allowed_cities].nil? || options[:allowed_cities].include?(city)
end

#cleanObject



108
109
110
# File 'lib/russian_phone/number.rb', line 108

def clean
  "#{country}#{city}#{subscriber}"
end

#coerce(something) ⇒ Object



25
26
27
# File 'lib/russian_phone/number.rb', line 25

def coerce(something)
  [self, something]
end

#countryObject



67
68
69
# File 'lib/russian_phone/number.rb', line 67

def country
  @country ||= parse(:country)
end

#empty?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/russian_phone/number.rb', line 51

def empty?
  @phone.strip == ''
end

#extraObject



71
72
73
# File 'lib/russian_phone/number.rb', line 71

def extra
  @extra ||= parse(:extra)
end

#formatObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/russian_phone/number.rb', line 84

def format
  if subscriber.length == 7
    split([3, 2, 2], subscriber)
  elsif subscriber.length == 6
    split([2, 2, 2], subscriber)
  elsif subscriber.length == 5
    split([1, 2, 2], subscriber)
  else
    []
  end
end

#free?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/russian_phone/number.rb', line 116

def free?
  city == '800'
end

#fullObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/russian_phone/number.rb', line 96

def full
  if valid?
    if free? && extra == ''
    "8-#{city}-#{format.join('-')}"
    else
    "+#{country} (#{city}) #{format.join('-')}#{extra == '' ? '' : ' ' + extra}"
    end
  else
    ''
  end
end

#inspectObject



128
129
130
131
# File 'lib/russian_phone/number.rb', line 128

def inspect
  # '"' + full + '"'
  '"' + to_s + '"'
end

#parse(field) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/russian_phone/number.rb', line 29

def parse(field)
  data = self.class.parse(@phone, @options)
  return nil if data.nil?

  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

#split(format, number) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/russian_phone/number.rb', line 75

def split(format, number)
  number = number.dup
  format.inject([]) do |result, size|
    result << number.slice!(0..size-1)
    return result if number.empty?
    result
  end
end

#subscriberObject



55
56
57
# File 'lib/russian_phone/number.rb', line 55

def subscriber
  @subscriber ||= parse(:subscriber)
end

#to_sObject Also known as: as_json, mongoize

alias_method(:to_s, :full) alias_method(:inspect, :full)



123
124
125
# File 'lib/russian_phone/number.rb', line 123

def to_s
  valid? ? full : @phone
end

#valid?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/russian_phone/number.rb', line 43

def valid?
  @valid ||= !(country.nil? || city.nil? || subscriber.nil? || country == '' || city == ''  || subscriber == '')
end