phoney 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +1 -5
- data/Gemfile.lock +3 -11
- data/{LICENCE → MIT-LICENCE} +1 -1
- data/README.rdoc +16 -20
- data/Rakefile +9 -25
- data/lib/data/regions.bin +0 -0
- data/lib/phoney.rb +103 -6
- data/lib/phoney/formatter.rb +144 -0
- data/lib/phoney/parser.rb +62 -218
- data/lib/phoney/region.rb +19 -35
- data/lib/phoney/rules.rb +56 -0
- data/lib/phoney/vanity.rb +31 -0
- data/lib/phoney/version.rb +2 -10
- data/phoney.gemspec +15 -43
- data/resources/Default.phoneformat +0 -0
- data/resources/liveinput_tester.rb +32 -0
- data/resources/phoneformat_converter.rb +122 -0
- data/resources/tasks/region_data.rake +23 -0
- data/test/formatter_test.rb +91 -0
- data/test/lib/phoney/test_helper.rb +6 -0
- data/test/phone_number_test.rb +4 -0
- data/test/regions/br_test.rb +33 -0
- data/test/regions/de_test.rb +45 -0
- data/test/regions/fr_test.rb +17 -0
- data/test/regions/ni_test.rb +14 -0
- data/test/regions/th_test.rb +38 -0
- data/test/regions/tw_test.rb +28 -0
- data/test/regions/ua_test.rb +18 -0
- data/test/regions/us_test.rb +44 -0
- metadata +45 -45
- data/lib/data/regions.yml +0 -19601
- data/lib/phoney/base.rb +0 -102
- data/lib/phoney/utils.rb +0 -52
- data/spec/parser/br_spec.rb +0 -64
- data/spec/parser/de_spec.rb +0 -73
- data/spec/parser/us_spec.rb +0 -49
- data/spec/phone_number_spec.rb +0 -40
- data/spec/spec.opts +0 -4
- data/spec/spec_helper.rb +0 -4
data/lib/phoney/base.rb
DELETED
@@ -1,102 +0,0 @@
|
|
1
|
-
class PhoneNumber
|
2
|
-
attr_accessor :country_code, :prefix_code, :area_code, :number
|
3
|
-
|
4
|
-
class << self
|
5
|
-
def default_region
|
6
|
-
@@region ||= Region[:us]
|
7
|
-
end
|
8
|
-
|
9
|
-
def default_region=(region)
|
10
|
-
if region.is_a?(Region)
|
11
|
-
@@region = region
|
12
|
-
else
|
13
|
-
@@region = Region[region.to_s.to_sym]
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def default_country_code
|
18
|
-
@@country_code ||= default_region.country_code.to_s
|
19
|
-
end
|
20
|
-
|
21
|
-
def default_country_code=(country_code)
|
22
|
-
@@country_code = country_code
|
23
|
-
end
|
24
|
-
|
25
|
-
def default_area_code
|
26
|
-
@@area_code ||= nil
|
27
|
-
end
|
28
|
-
|
29
|
-
def default_area_code=(area_code)
|
30
|
-
@@area_code = area_code
|
31
|
-
end
|
32
|
-
|
33
|
-
def version
|
34
|
-
VERSION::STRING
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
def initialize(params, region_code=nil)
|
39
|
-
region = Region.find(region_code)
|
40
|
-
country_code = region.country_code.to_s if region
|
41
|
-
|
42
|
-
if params.is_a?(String)
|
43
|
-
params = Parser.parse_to_parts(params, region_code)
|
44
|
-
end
|
45
|
-
|
46
|
-
self.number = params[:number].to_s
|
47
|
-
# The rare case when some digits are in front of the area code
|
48
|
-
self.prefix_code = params[:prefix_code].to_s
|
49
|
-
# Can be empty, because some special numbers just don't have an area code (e.g. 911)
|
50
|
-
self.area_code = params[:area_code].to_s || self.class.default_area_code
|
51
|
-
self.country_code = params[:country_code].to_s || country_code || self.class.default_country_code
|
52
|
-
|
53
|
-
raise "Must enter number" if(self.number.nil? || self.number.empty?)
|
54
|
-
raise "Must enter country code or set default country code" if(self.country_code.nil? || self.country_code.empty?)
|
55
|
-
end
|
56
|
-
|
57
|
-
# Does this number belong to the default country code?
|
58
|
-
def has_default_country_code?
|
59
|
-
country_code.to_s == self.class.default_country_code.to_s
|
60
|
-
end
|
61
|
-
|
62
|
-
# Does this number belong to the default area code?
|
63
|
-
def has_default_area_code?
|
64
|
-
(!area_code.to_s.empty? && area_code.to_s == self.class.default_area_code.to_s)
|
65
|
-
end
|
66
|
-
|
67
|
-
# Formats the phone number.
|
68
|
-
# If the method argument is a String, it is used as a format string, with the following fields being interpolated:
|
69
|
-
#
|
70
|
-
# * %c - country_code (385)
|
71
|
-
# * %a - area_code (91)
|
72
|
-
# * %n - number (5125486)
|
73
|
-
#
|
74
|
-
# If the method argument is a Symbol, we use one of the default formattings and let the parser do the rest.
|
75
|
-
def format(fmt)
|
76
|
-
if fmt.is_a?(Symbol)
|
77
|
-
case fmt
|
78
|
-
when :default
|
79
|
-
Parser::parse("+#{country_code} #{prefix_code}#{area_code} #{number}", country_code)
|
80
|
-
when :national
|
81
|
-
Parser::parse("#{area_code} #{number}", country_code)
|
82
|
-
when :local
|
83
|
-
STDERR.puts "Warning: Using local format without setting a default area code!?" if PhoneNumber.default_area_code.nil?
|
84
|
-
Parser::parse(number, country_code)
|
85
|
-
else
|
86
|
-
raise "The format #{fmt} doesn't exist'"
|
87
|
-
end
|
88
|
-
else
|
89
|
-
format_number(fmt)
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
# The default format is the canonical format: "+{country_code} {area_code} {number}"
|
94
|
-
def to_s
|
95
|
-
format(:default)
|
96
|
-
end
|
97
|
-
|
98
|
-
private
|
99
|
-
def format_number(fmt)
|
100
|
-
fmt.gsub("%c", country_code || "").gsub("%a", area_code || "").gsub("%n", number || "")
|
101
|
-
end
|
102
|
-
end
|
data/lib/phoney/utils.rb
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
class PhoneNumber
|
2
|
-
|
3
|
-
module Utils
|
4
|
-
|
5
|
-
# Returns the string formatted according to a pattern.
|
6
|
-
#
|
7
|
-
# Examples:
|
8
|
-
# format('123456789', 'XXX-XX-XXXX')
|
9
|
-
# => "123-45-6789"
|
10
|
-
# format('12345', 'XXX-XX-XXXX')
|
11
|
-
# => "123-45"
|
12
|
-
#
|
13
|
-
# Parameters:
|
14
|
-
# string -- The string to be formatted.
|
15
|
-
# pattern -- The format string, see above examples.
|
16
|
-
# fixchar -- The single-character placeholder. Default is 'X'.
|
17
|
-
def format(string, pattern, fixchar='X')
|
18
|
-
raise ArgumentError.new("First parameter 'string' must be a String") unless string.is_a?(String)
|
19
|
-
raise ArgumentError.new("#{fixchar} too long") if fixchar.length > 1
|
20
|
-
|
21
|
-
slots = pattern.count(fixchar)
|
22
|
-
|
23
|
-
# Return the string if it doesn't fit and we shouldn't even try,
|
24
|
-
return string if string.length > slots
|
25
|
-
|
26
|
-
# Make the result.
|
27
|
-
scanner = ::StringScanner.new(pattern)
|
28
|
-
regexp = Regexp.new(Regexp.escape(fixchar))
|
29
|
-
index = 0
|
30
|
-
result = ''
|
31
|
-
|
32
|
-
while(!scanner.eos? && index < string.length)
|
33
|
-
if scanner.scan(regexp) then
|
34
|
-
result += string[index].chr
|
35
|
-
index += 1
|
36
|
-
else
|
37
|
-
result += scanner.getch
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
result
|
42
|
-
end
|
43
|
-
|
44
|
-
# Strips all non-numberpad characters from a string
|
45
|
-
# => For example: "+45 (123) 023 1.1.1" -> "+45123023111"
|
46
|
-
def normalize(string_with_number)
|
47
|
-
string_with_number.gsub(/[^0-9+*#]/,'') unless string_with_number.nil?
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
end
|
data/spec/parser/br_spec.rb
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
|
3
|
-
describe PhoneNumber::Parser do
|
4
|
-
|
5
|
-
describe "with region set to [:br]" do
|
6
|
-
before(:each) do
|
7
|
-
PhoneNumber.default_region = :br
|
8
|
-
end
|
9
|
-
|
10
|
-
it "should format a [:br] (Brazil) phone number" do
|
11
|
-
PhoneNumber::Parser.parse("03001234567").should == "0300 123-4567"
|
12
|
-
|
13
|
-
PhoneNumber::Parser.parse("123").should == "123"
|
14
|
-
PhoneNumber::Parser.parse("1234").should == "123-4"
|
15
|
-
PhoneNumber::Parser.parse("12345").should == "123-45"
|
16
|
-
PhoneNumber::Parser.parse("123456").should == "123-456"
|
17
|
-
PhoneNumber::Parser.parse("1234567").should == "123-4567"
|
18
|
-
PhoneNumber::Parser.parse("12345678").should == "1234-5678"
|
19
|
-
PhoneNumber::Parser.parse("123456789").should == "(12) 3456-789"
|
20
|
-
PhoneNumber::Parser.parse("1234567891").should == "(12) 3456-7891"
|
21
|
-
PhoneNumber::Parser.parse("12345678910").should == "123 4567-8910"
|
22
|
-
PhoneNumber::Parser.parse("123456789100").should == "(12 34) 5678-9100"
|
23
|
-
end
|
24
|
-
|
25
|
-
describe "dialing with international prefix" do
|
26
|
-
it "should dial out with an international_prefix" do
|
27
|
-
PhoneNumber::Parser.parse("+4940123456").should == "+49 40 123456"
|
28
|
-
PhoneNumber::Parser.parse("+004940123456").should == "+00 49 40 123456"
|
29
|
-
PhoneNumber::Parser.parse("00004940123456").should == "00 00 49 40 123456"
|
30
|
-
PhoneNumber::Parser.parse("00014940123456").should == "00 01 49 40 123456"
|
31
|
-
PhoneNumber::Parser.parse("00024940123456").should == "00 02 49 40 123456"
|
32
|
-
end
|
33
|
-
|
34
|
-
describe "given an any phone number" do
|
35
|
-
it "should ignore non-number pad characters" do
|
36
|
-
PhoneNumber::Parser.parse("!!+ 1 7--12@ 34 5.6").should == "+1 (712) 345-6"
|
37
|
-
end
|
38
|
-
|
39
|
-
it "should guess the current formatting correctly" do
|
40
|
-
PhoneNumber::Parser.parse("+17").should == "+1 (7"
|
41
|
-
PhoneNumber::Parser.parse("+171").should == "+1 (71"
|
42
|
-
PhoneNumber::Parser.parse("+1712").should == "+1 (712"
|
43
|
-
PhoneNumber::Parser.parse("+171234").should == "+1 (712) 34"
|
44
|
-
PhoneNumber::Parser.parse("+1712345").should == "+1 (712) 345"
|
45
|
-
PhoneNumber::Parser.parse("+17123456").should == "+1 (712) 345-6"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
describe "given an invalid phone number" do
|
50
|
-
it "should use a reasonable default format" do
|
51
|
-
# the number is too long for [:br]
|
52
|
-
PhoneNumber::Parser.parse("1234567891001").should == "1234567891001"
|
53
|
-
|
54
|
-
PhoneNumber::Parser.parse("+++494070123").should == "+++494070123"
|
55
|
-
PhoneNumber::Parser.parse("++494070123").should == "++494070123"
|
56
|
-
PhoneNumber::Parser.parse("+01494070123").should == "+01494070123"
|
57
|
-
PhoneNumber::Parser.parse("+00000000").should == "+00 000000"
|
58
|
-
PhoneNumber::Parser.parse("0000000000").should == "00 00 000000"
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
end
|
data/spec/parser/de_spec.rb
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
|
3
|
-
describe PhoneNumber::Parser do
|
4
|
-
|
5
|
-
describe "with region set to [:de] (Germany)" do
|
6
|
-
before(:each) do
|
7
|
-
PhoneNumber.default_region = :de
|
8
|
-
end
|
9
|
-
|
10
|
-
describe "given a valid phone number" do
|
11
|
-
it "should output the correct format" do
|
12
|
-
# with national prefix '0'
|
13
|
-
PhoneNumber::Parser.parse("040789554488").should == "040 789554488"
|
14
|
-
# without national prefix '0'
|
15
|
-
PhoneNumber::Parser.parse("40789554488").should == "40 789554488"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
describe "without international prefix" do
|
20
|
-
describe "given an any phone number" do
|
21
|
-
it "should guess the current formatting correctly" do
|
22
|
-
PhoneNumber::Parser.parse("04").should == "04"
|
23
|
-
PhoneNumber::Parser.parse("040").should == "040"
|
24
|
-
PhoneNumber::Parser.parse("0407").should == "040 7"
|
25
|
-
|
26
|
-
PhoneNumber::Parser.parse("4").should == "4"
|
27
|
-
PhoneNumber::Parser.parse("40").should == "40"
|
28
|
-
PhoneNumber::Parser.parse("407").should == "40 7"
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe "with internatoinal prefix" do
|
34
|
-
describe "given an any phone number" do
|
35
|
-
it "should guess the current formatting correctly" do
|
36
|
-
PhoneNumber::Parser.parse("+494").should == "+49 4"
|
37
|
-
PhoneNumber::Parser.parse("+4940").should == "+49 40"
|
38
|
-
PhoneNumber::Parser.parse("+49407").should == "+49 40 7"
|
39
|
-
PhoneNumber::Parser.parse("+494070").should == "+49 40 70"
|
40
|
-
PhoneNumber::Parser.parse("+4940705").should == "+49 40 705"
|
41
|
-
PhoneNumber::Parser.parse("+49407055").should == "+49 40 7055"
|
42
|
-
PhoneNumber::Parser.parse("+494070558").should == "+49 40 70558"
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
describe "with country specific internatoinal prefix" do
|
48
|
-
describe "given an any phone number" do
|
49
|
-
it "should guess the current formatting correctly" do
|
50
|
-
PhoneNumber::Parser.parse("00494").should == "00 49 4"
|
51
|
-
PhoneNumber::Parser.parse("004940").should == "00 49 40"
|
52
|
-
PhoneNumber::Parser.parse("0049407").should == "00 49 40 7"
|
53
|
-
PhoneNumber::Parser.parse("00494070").should == "00 49 40 70"
|
54
|
-
PhoneNumber::Parser.parse("004940705").should == "00 49 40 705"
|
55
|
-
PhoneNumber::Parser.parse("0049407055").should == "00 49 40 7055"
|
56
|
-
PhoneNumber::Parser.parse("00494070558").should == "00 49 40 70558"
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
describe "given an invalid phone number" do
|
62
|
-
it "should use a reasonable default format" do
|
63
|
-
# the number is too long for [:de]
|
64
|
-
PhoneNumber::Parser.parse("040123456789123456789").should == "040123456789123456789"
|
65
|
-
|
66
|
-
PhoneNumber::Parser.parse("+++494070123").should == "+++494070123"
|
67
|
-
PhoneNumber::Parser.parse("++494070123").should == "++494070123"
|
68
|
-
PhoneNumber::Parser.parse("+01494070123").should == "+01494070123"
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
data/spec/parser/us_spec.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
-
|
3
|
-
describe PhoneNumber::Parser do
|
4
|
-
|
5
|
-
describe "with region set to [:us]" do
|
6
|
-
before(:each) do
|
7
|
-
PhoneNumber.default_region = :us
|
8
|
-
end
|
9
|
-
|
10
|
-
it "should format a [:us] (USA) phone number" do
|
11
|
-
PhoneNumber::Parser.parse("5689780").should == "568-9780"
|
12
|
-
PhoneNumber::Parser.parse("7045689780").should == "(704) 568-9780"
|
13
|
-
PhoneNumber::Parser.parse("17045689780").should == "1 (704) 568-9780"
|
14
|
-
end
|
15
|
-
|
16
|
-
describe "dialing with international prefix" do
|
17
|
-
it "should not dialout without the prefix" do
|
18
|
-
PhoneNumber::Parser.parse("4940123456").should == "(494) 012-3456"
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should dial out with an international_prefix" do
|
22
|
-
PhoneNumber::Parser.parse("0114940123456").should == "011 49 40 123456"
|
23
|
-
end
|
24
|
-
|
25
|
-
describe "given an any phone number" do
|
26
|
-
it "should ignore non-number pad characters" do
|
27
|
-
PhoneNumber::Parser.parse("!!+ 1 7--12@ 34 5.6").should == "+1 (712) 345-6"
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should guess the current formatting correctly" do
|
31
|
-
PhoneNumber::Parser.parse("+17").should == "+1 (7"
|
32
|
-
PhoneNumber::Parser.parse("+171").should == "+1 (71"
|
33
|
-
PhoneNumber::Parser.parse("+1712").should == "+1 (712"
|
34
|
-
PhoneNumber::Parser.parse("+171234").should == "+1 (712) 34"
|
35
|
-
PhoneNumber::Parser.parse("+1712345").should == "+1 (712) 345"
|
36
|
-
PhoneNumber::Parser.parse("+17123456").should == "+1 (712) 345-6"
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
describe "given an invalid phone number" do
|
41
|
-
it "should use a reasonable default format" do
|
42
|
-
# the number is too long for [:us]
|
43
|
-
PhoneNumber::Parser.parse("+1704123456789").should == "+1 704123456789"
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
data/spec/phone_number_spec.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
|
3
|
-
describe PhoneNumber do
|
4
|
-
|
5
|
-
describe "with region set to [:us]" do
|
6
|
-
before(:each) do
|
7
|
-
PhoneNumber.default_region = :us
|
8
|
-
end
|
9
|
-
|
10
|
-
it "should format a valid number according to [:us] formatting" do
|
11
|
-
pn = PhoneNumber.new '7041234567'
|
12
|
-
pn.to_s.should == "+1 (704) 123-4567"
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should extract the country, area, and number correctly" do
|
16
|
-
pn = PhoneNumber.new '7041234567'
|
17
|
-
|
18
|
-
pn.country_code.should == "1"
|
19
|
-
pn.area_code.should == "704"
|
20
|
-
pn.number.should == "1234567"
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
describe "using a number that has a prefix_code before the area code" do
|
25
|
-
before(:each) do
|
26
|
-
@pn = PhoneNumber.new("+55 12 34 5678-9012")
|
27
|
-
end
|
28
|
-
|
29
|
-
it "should have the correct prefix/area code assigned" do
|
30
|
-
@pn.prefix_code.should == "12"
|
31
|
-
@pn.area_code.should == "34"
|
32
|
-
@pn.number == "56789012"
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should output the correct canonical format" do
|
36
|
-
@pn.to_s.should == "+55 (12 34) 5678-9012"
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
end
|
data/spec/spec.opts
DELETED
data/spec/spec_helper.rb
DELETED