Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions data/phone/countries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,8 @@
:char_3_code: RU
:name: Russian Federation
:international_dialing_prefix: "810"
:country_detect_regexp: (^[+]7)|(^8\d{10})|(^7\d{10})
:country_code_regexp: (^[+]7)|(^8)|(^7)
"98":
:country_code: "98"
:national_dialing_prefix: "0"
Expand Down
10 changes: 8 additions & 2 deletions lib/phone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ def default_area_code
end

def named_formats
self.class.named_formats
if country_code == '8'
self.class.named_formats.merge({ :default => '%c%a%n' })
else
self.class.named_formats
end
end

def n1_length
Expand Down Expand Up @@ -70,6 +74,8 @@ def initialize(*hash_or_args)
self.country_code = (hash_or_args[ keys[:country_code] ] || self.default_country_code).to_s.strip
self.extension = hash_or_args[ keys[:extension] ]

self.country_code = '8' if country_code == '7' && area_code == '800'

raise BlankNumberError, "Must enter number" if self.number.empty?
raise AreaCodeError, "Must enter area code or set default area code" if self.area_code.empty?
raise CountryCodeError, "Must enter country code or set default country code" if self.country_code.empty?
Expand Down Expand Up @@ -147,7 +153,7 @@ def self.detect_country(string)
detected_country = nil
# find if the number has a country code
Country.all.each_pair do |country_code, country|
if string =~ country.country_code_regexp
if string =~ country.country_detect_regexp
detected_country = country
end
end
Expand Down
12 changes: 8 additions & 4 deletions lib/phone/country.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'yaml'

module Phoner
class Country < Struct.new(:name, :country_code, :char_2_code, :char_3_code, :area_code)
class Country < Struct.new(:name, :country_code, :char_2_code, :char_3_code, :area_code, :country_detect_regexp, :country_code_regexp)
module All
attr_accessor :all
end
Expand All @@ -18,7 +18,7 @@ def self.load

self.all = {}
YAML.load(File.read(data_file)).each_pair do |key, c|
self.all[key] = Country.new(c[:name], c[:country_code], c[:char_2_code], c[:char_3_code], c[:area_code])
self.all[key] = Country.new(c[:name], c[:country_code], c[:char_2_code], c[:char_3_code], c[:area_code], c[:country_detect_regexp], c[:country_code_regexp])
end
self.all
end
Expand All @@ -30,15 +30,19 @@ def to_s
def self.find_by_country_code(code)
self.all[code]
end

def self.find_by_country_isocode(isocode)
if country = self.all.detect{|c|c[1].char_3_code.downcase == isocode}
country[1]
end
end

def country_detect_regexp
Regexp.new(super || "^[+]#{country_code}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no reason to cache regexp, because creation of it is very cheap

end

def country_code_regexp
@country_code_regexp ||= Regexp.new("^[+]#{country_code}")
Regexp.new(super || "^[+]#{country_code}")
end
end

Expand Down
40 changes: 40 additions & 0 deletions test/countries/ru_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "helper"

## Russia
class RUTest < Minitest::Test

def test_local
parse_test('89033891228', '7', '903', '3891228')
end

def test_local2
parse_test('8 903 389-12-28', '7', '903', '3891228')
end

def test_local3
parse_test('8(903)389-12-28', '7', '903', '3891228')
end

def test_local_city
parse_test('8(8352)42-14-14', '7', '835', '2421414')
end

def test_international_city
parse_test('+7(8352)42-14-14', '7', '835', '2421414')
end

def test_mobile
parse_test('+7(903)389-12-28', '7', '903', '3891228')
end

def test_freecall
phone = '8(800)111-11-11'

parse_test(phone, '8', '800', '1111111')

pn = Phoner::Phone.parse(phone)

assert_equal '88001111111', pn.to_s
end

end