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



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/russian_phone/number.rb', line 165

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



157
158
159
# File 'lib/russian_phone/number.rb', line 157

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

.clean(string) ⇒ Object



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

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

.country_code(string) ⇒ Object



183
184
185
# File 'lib/russian_phone/number.rb', line 183

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

.extract(string, subscriber_digits, code_digits) ⇒ Object



161
162
163
# File 'lib/russian_phone/number.rb', line 161

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

.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 !opts[:default_city].blank? && 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



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/russian_phone/number.rb', line 187

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

#as_json(options = {}) ⇒ Object



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

def as_json(options = {})
  to_s
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)


121
122
123
# File 'lib/russian_phone/number.rb', line 121

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

#cityObject Also known as: area



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



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

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

#coerce(something) ⇒ Object



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

def coerce(something)
  [self, something]
end

#countryObject



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

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



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

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

#formatObject



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

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

#formatted_areaObject



97
98
99
# File 'lib/russian_phone/number.rb', line 97

def formatted_area
  area.nil? ? '' : "(#{area})"
end

#formatted_subscriberObject



101
102
103
# File 'lib/russian_phone/number.rb', line 101

def formatted_subscriber
  subscriber.nil? ? '' : format.join('-')
end

#free?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/russian_phone/number.rb', line 125

def free?
  area == '800'
end

#fullObject



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/russian_phone/number.rb', line 105

def full
  if valid?
    if free? && extra == ''
    "8-#{area}-#{formatted_subscriber}"
    else
    "+#{country} #{formatted_area} #{formatted_subscriber}#{extra == '' ? '' : ' ' + extra}"
    end
  else
    ''
  end
end

#inspectObject



137
138
139
140
# File 'lib/russian_phone/number.rb', line 137

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



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

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: mongoize

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



132
133
134
# File 'lib/russian_phone/number.rb', line 132

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