diff --git a/data/phone/countries.yml b/data/phone/countries.yml index 2ee4976..a5b2753 100644 --- a/data/phone/countries.yml +++ b/data/phone/countries.yml @@ -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" diff --git a/lib/phone.rb b/lib/phone.rb index 978f43f..1d7e50c 100644 --- a/lib/phone.rb +++ b/lib/phone.rb @@ -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 @@ -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? @@ -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 diff --git a/lib/phone/country.rb b/lib/phone/country.rb index 668ac75..d50053b 100644 --- a/lib/phone/country.rb +++ b/lib/phone/country.rb @@ -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 @@ -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 @@ -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}") + end + def country_code_regexp - @country_code_regexp ||= Regexp.new("^[+]#{country_code}") + Regexp.new(super || "^[+]#{country_code}") end end diff --git a/test/countries/ru_test.rb b/test/countries/ru_test.rb new file mode 100644 index 0000000..5047285 --- /dev/null +++ b/test/countries/ru_test.rb @@ -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