From 656b6de255334cfff52ba7a1d5d46f6a32de427f Mon Sep 17 00:00:00 2001 From: Danil Pismenny Date: Tue, 27 Jan 2015 21:22:54 +0300 Subject: [PATCH 1/3] Local russian numbers detections and tests --- data/phone/countries.yml | 2 ++ lib/phone.rb | 2 +- lib/phone/country.rb | 10 +++++++--- test/countries/ru_test.rb | 30 ++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 test/countries/ru_test.rb 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..e0efa6b 100644 --- a/lib/phone.rb +++ b/lib/phone.rb @@ -147,7 +147,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..885f9e3 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 @@ -37,8 +37,12 @@ def self.find_by_country_isocode(isocode) 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..9b4a586 --- /dev/null +++ b/test/countries/ru_test.rb @@ -0,0 +1,30 @@ +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 + +end From 9830ede6e894e8fc93e37641f23655ad0d3cabc6 Mon Sep 17 00:00:00 2001 From: Danil Pismenny Date: Fri, 7 Apr 2017 15:10:08 +0300 Subject: [PATCH 2/3] add support for Russian freecall numbers --- lib/phone.rb | 2 ++ lib/phone/country.rb | 2 +- test/countries/ru_test.rb | 4 ++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/phone.rb b/lib/phone.rb index e0efa6b..fa4f606 100644 --- a/lib/phone.rb +++ b/lib/phone.rb @@ -70,6 +70,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? diff --git a/lib/phone/country.rb b/lib/phone/country.rb index 885f9e3..d50053b 100644 --- a/lib/phone/country.rb +++ b/lib/phone/country.rb @@ -30,7 +30,7 @@ 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] diff --git a/test/countries/ru_test.rb b/test/countries/ru_test.rb index 9b4a586..25a9321 100644 --- a/test/countries/ru_test.rb +++ b/test/countries/ru_test.rb @@ -27,4 +27,8 @@ def test_mobile parse_test('+7(903)389-12-28', '7', '903', '3891228') end + def test_freecall + parse_test('8(800)111-11-11', '8', '800', '1111111') + end + end From ea695a3ce692c9cbdcf5568f8c93005989f6e430 Mon Sep 17 00:00:00 2001 From: Danil Pismenny Date: Fri, 7 Apr 2017 15:19:54 +0300 Subject: [PATCH 3/3] format russian freecall numbers without + --- lib/phone.rb | 6 +++++- test/countries/ru_test.rb | 8 +++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/phone.rb b/lib/phone.rb index fa4f606..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 diff --git a/test/countries/ru_test.rb b/test/countries/ru_test.rb index 25a9321..5047285 100644 --- a/test/countries/ru_test.rb +++ b/test/countries/ru_test.rb @@ -28,7 +28,13 @@ def test_mobile end def test_freecall - parse_test('8(800)111-11-11', '8', '800', '1111111') + 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