Class: RussianPhone::Number
- Inherits:
-
Object
- Object
- RussianPhone::Number
- Defined in:
- lib/russian_phone/number.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#phone ⇒ Object
Returns the value of attribute phone.
Class Method Summary collapse
- ._extra(string, position) ⇒ Object
-
._extract(string, subscriber_digits, code_digits) ⇒ Object
def _extract(string, subscriber_digits, code_digits) [string[-(subscriber_digits + code_digits), code_digits], string] end.
- .clean(string) ⇒ Object
- .country_code(string) ⇒ Object
- .extract(string, subscriber_digits, code_digits) ⇒ Object
- .parse(string, opts = {}) ⇒ Object
- .process_options(opts = {}) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #as_json(options = {}) ⇒ Object
- #blank? ⇒ Boolean
- #cell? ⇒ Boolean
- #city ⇒ Object (also: #area)
- #city_allowed? ⇒ Boolean
- #clean ⇒ Object
- #coerce(something) ⇒ Object
- #country ⇒ Object
- #empty? ⇒ Boolean
- #extra ⇒ Object
- #format ⇒ Object
- #formatted_area ⇒ Object
- #formatted_subscriber ⇒ Object
- #free? ⇒ Boolean
- #full ⇒ Object
-
#initialize(phone, options = {}) ⇒ Number
constructor
A new instance of Number.
- #inspect ⇒ Object
- #parse(field) ⇒ Object
- #split(format, number) ⇒ Object
- #subscriber ⇒ Object
-
#to_s ⇒ Object
(also: #mongoize)
alias_method(:to_s, :full) alias_method(:inspect, :full).
- #valid? ⇒ Boolean
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 = self.class.() @phone = phone.to_s end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/russian_phone/number.rb', line 5 def @options end |
#phone ⇒ Object
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 = (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 (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( = {}) to_s end |
#blank? ⇒ Boolean
47 48 49 |
# File 'lib/russian_phone/number.rb', line 47 def blank? @phone.strip == '' end |
#cell? ⇒ Boolean
121 122 123 |
# File 'lib/russian_phone/number.rb', line 121 def cell? Codes.cell_codes.include?(area) end |
#city ⇒ Object Also known as: area
63 64 65 |
# File 'lib/russian_phone/number.rb', line 63 def city @city ||= parse(:city) end |
#city_allowed? ⇒ Boolean
59 60 61 |
# File 'lib/russian_phone/number.rb', line 59 def city_allowed? [:allowed_cities].nil? || [:allowed_cities].include?(city) end |
#clean ⇒ Object
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 |
#country ⇒ Object
68 69 70 |
# File 'lib/russian_phone/number.rb', line 68 def country @country ||= parse(:country) end |
#empty? ⇒ Boolean
51 52 53 |
# File 'lib/russian_phone/number.rb', line 51 def empty? @phone.strip == '' end |
#extra ⇒ Object
72 73 74 |
# File 'lib/russian_phone/number.rb', line 72 def extra @extra ||= parse(:extra) end |
#format ⇒ Object
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_area ⇒ Object
97 98 99 |
# File 'lib/russian_phone/number.rb', line 97 def formatted_area area.nil? ? '' : "(#{area})" end |
#formatted_subscriber ⇒ Object
101 102 103 |
# File 'lib/russian_phone/number.rb', line 101 def formatted_subscriber subscriber.nil? ? '' : format.join('-') end |
#free? ⇒ Boolean
125 126 127 |
# File 'lib/russian_phone/number.rb', line 125 def free? area == '800' end |
#full ⇒ Object
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 |
#inspect ⇒ Object
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 |
#subscriber ⇒ Object
55 56 57 |
# File 'lib/russian_phone/number.rb', line 55 def subscriber @subscriber ||= parse(:subscriber) end |
#to_s ⇒ Object 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
43 44 45 |
# File 'lib/russian_phone/number.rb', line 43 def valid? @valid ||= !(country.nil? || city.nil? || subscriber.nil? || country == '' || city == '' || subscriber == '') end |