phony 2.0.0.beta10 → 2.0.0.beta11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- M2RiYWE2YjE2ZmM4NWFjZmQyMTI1NmFiYzQxNjNiOWQzYTM3M2VjZA==
4
+ OGU2OTIyNGI5NGE1MjkxNWNmYzBlYzRiM2Y3YWRhMjc0ZWFkZTM3Yg==
5
5
  data.tar.gz: !binary |-
6
- NDg1OGQwNGMxYzZlZDYyYjFkZjM0OGQ4YTVjZTYzODJjZjAwZjdhNw==
6
+ YTE2MWIzODdiYWE0Y2I1NTczZDIyOTBkMjBiOTEyOWEzYTMwYTFlYQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NTgxN2I0ZGVmZTZlZjU2M2Q5OGI4ZjcxMTAyMzQxMTA5N2ZkNGRiNWM5NDkx
10
- M2FlNTNmNDA0Y2JkYjc1Yjc0M2QxNTk0Y2Q4MjBmNjFlMDUxMDBiN2JjMzg1
11
- YTJhY2UxOGRiNjU4YzA5ZTNkMDY4YWI3NDg3ZGQ1ZmUwYTk1YmY=
9
+ YzAzM2UwYmQ2NDc2Njg5MWI2ZWYxZmQzOGU5NGY5YTQ0Yzc0ZjhjZmM3ZTM2
10
+ Njg1ZGNhYzM5Y2E2NWU4NjM0OGQ3NDcwOWQ5NWQ4NjcxMGMwM2FkNDc3NmIw
11
+ ODhmZTA5ZWE1MDFlZDE3NWI2YTI1ZmE5YzEwNzM5YTRkYmQ2NjQ=
12
12
  data.tar.gz: !binary |-
13
- ZmVjNmYzNTY3ZDkyYzI0MDQwYTc3N2FhZjI5OWMzNjlhMDE2NTNjNTYyNzcw
14
- MTJiNmVlN2EzYjU5ZDg2NTYwNmIzY2U1NDFhZDdiMzRmMzkxOTBjYWYyNDU5
15
- MmEyOTBhNWM4ZWFlMThhNmVjNjI4MmU2OGY3MzI0OWUwNGNmMDU=
13
+ NDU3NWJiYWIyODQxMDg0ZDYxNjg5MjVmNDhiZmQyNzcxMjZiNzljZTg3NGZm
14
+ ODhiZjU4YzlkZWYzZDdjZGJjZmVhY2NmMTVjOThkODRlOGNhMDc5ODQ1NWUz
15
+ ZDczYzgwZWUyNjg4MWRlN2M3ODRhNTAwMzA4YjFlZTM2ZGM2Nzg=
@@ -8,7 +8,7 @@ There's also a Rails wrapper: https://github.com/joost/phony_rails.
8
8
 
9
9
  h2. Description
10
10
 
11
- This gem can normalize, format and split E164 numbers.
11
+ This gem can normalize, format and split E164 numbers. E164 numbers are international numbers with a country dial prefix, usually an area code and a subscriber number. Here's an example from Australia: +61 412 345 678. 61 is the country code of Australia, 4 denotes a mobile number and 12 345 678 is the subscriber number.
12
12
  "More about E164 numbers in this Wiki":http://en.wikipedia.org/wiki/E.164.
13
13
 
14
14
  The (admittedly crazy) *goal* of this Gem is to be able to format/split all phone numbers in the world.
@@ -72,17 +72,27 @@ module Phony
72
72
  @codes = CountryCodes.instance
73
73
 
74
74
  class << self
75
+
76
+ # Get the Country for the given CC.
77
+ #
78
+ # Example:
79
+ # us = Phony['1']
80
+ # normalized_number = us.normalize number
81
+ #
82
+ def [] cc
83
+ @codes[cc]
84
+ end
75
85
 
76
86
  # Normalizes the given number.
77
87
  #
78
88
  # Useful before inserting the number into a database.
79
89
  #
80
- def normalize phone_number
90
+ def normalize phone_number, options = {}
81
91
  raise ArgumentError, "Phone number cannot be nil. Use e.g. number && Phony.normalize(number)." unless phone_number
82
- normalize! phone_number.dup
92
+ normalize! phone_number.dup, options
83
93
  end
84
- def normalize! phone_number
85
- @codes.normalize phone_number
94
+ def normalize! phone_number, options = {}
95
+ @codes.normalize phone_number, options
86
96
  rescue
87
97
  raise NormalizationError.new
88
98
  end
@@ -13,30 +13,27 @@
13
13
  sdca_with_2_digits = %w{ 11 20 22 33 39 40 44 50 60 70 79 80 }
14
14
 
15
15
  sdca_with_3_digits = %w{
16
- 120 121 122 124 129 130 131 132 135 141 144 145 151 154 160 161 164 171 172 175 177 180 181 183 184 186 191 194
17
- 210 212 217 230 231 233 239 240 241 249 250 251 254 257 260 261 265 268 270 272 278 281 285 286 288 291 294
18
- 326 341 342 343 353 354 360 361 364 369 370 373 374 376 381 385 389
19
- 413 416 421 423 424 427 431 435 451 452 461 462 469 470 471 474 475 476 477 478 479 480 481 483 484 485 487 490 491 494 495 496 497
20
- 512 515 532 535 542 548 551 562 565 572 581 595
21
- 612 621 631 641 651 657 661 663 671 674 680
22
- 712 721 724 731 733 734 744 751 755 761 747 788
23
- 816 820 821 824 831 832 836 861 863 866 870 877 878 883 884 891
24
-
25
-
26
- 140
27
- 240 249 254 260 270 272
28
- 300 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 318 320 327 328 329 340 344 349 350 357 388
29
- 412 419 430 438 439 450 453 458 459 460 464 466 467 468
30
- 510 513 520 523 529 530 537 538 539 540 543 547 550 553 557 558 559 560 563 570 575 576 577 578 579 580 589 590 593 597 598 599
31
- 610 614 616 617 619 620 623 626 628 629 630 635 636 637 638 639 640 644 648 649 650 660 669 670 677 687 688 689
32
- 730 735 738 740 750 787 789
33
- 812 814 829 843 860 879 880 887 889 890 895 897 898 899
34
-
16
+ 120 121 122 124 129 130 131 132 135 141 144 145 151 154 160 161 164 171 172 175 177 180 181 183 184 186 191 194
17
+ 210 212 217 230 231 233 239 240 241 249 250 251 254 257 260 261 265 268 270 272 278 281 285 286 288 291 294
18
+ 326 341 342 343 353 354 360 361 364 369 370 373 374 376 381 385 389
19
+ 413 416 421 423 424 427 431 435 451 452 461 462 469 470 471 474 475 476 477 478 479 480 481 483 484 485 487 490 491 494 495 496 497
20
+ 512 515 532 535 542 548 551 562 565 572 581 595
21
+ 612 621 631 641 651 657 661 663 671 674 680
22
+ 712 721 724 731 733 734 744 751 755 761 747 788
23
+ 816 820 821 824 831 832 836 861 863 866 870 877 878 883 884 891
24
+ 140
25
+ 240 249 254 260 270 272
26
+ 300 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 318 320 327 328 329 340 344 349 350 357 388
27
+ 412 419 430 438 439 450 453 458 459 460 464 466 467 468
28
+ 510 513 520 523 529 530 537 538 539 540 543 547 550 553 557 558 559 560 563 570 575 576 577 578 579 580 589 590 593 597 598 599
29
+ 610 614 616 617 619 620 623 626 628 629 630 635 636 637 638 639 640 644 648 649 650 660 669 670 677 687 688 689
30
+ 730 735 738 740 750 787 789
31
+ 812 814 829 843 860 879 880 887 889 890 895 897 898 899
35
32
  }
36
33
 
37
34
 
38
35
 
39
- mobile = %w{ 91 92 93 94 97 98 99}
36
+ mobile = %w{ 91 92 93 94 97 98 99 }
40
37
 
41
38
  pagers = %w{ 96 }
42
39
 
@@ -24,6 +24,7 @@ module Phony
24
24
  # TODO Rewrite.
25
25
  #
26
26
  def with options = {}
27
+ @cc = options[:cc]
27
28
  @invalid_ndcs = options[:invalid_ndcs] || []
28
29
  end
29
30
 
@@ -44,6 +45,15 @@ module Phony
44
45
  end
45
46
  end
46
47
 
48
+ def countrify! number
49
+ # The sensible default is to add the country code
50
+ # if it does not already start with it.
51
+ #
52
+ # Note: This won't be correct in some cases, but it is the best we can do.
53
+ #
54
+ number.sub! /\b#{@cc}?/, @cc
55
+ end
56
+
47
57
  # Removes 0s from partially normalized numbers
48
58
  # such as 410443643533.
49
59
  #
@@ -6,7 +6,7 @@ module Phony
6
6
  #
7
7
  class CountryCodes
8
8
 
9
- attr_reader :splitter_mapping
9
+ attr_reader :countries
10
10
  attr_accessor :international_absolute_format, :international_relative_format, :national_format
11
11
 
12
12
  def initialize
@@ -21,6 +21,12 @@ module Phony
21
21
  def self.instance
22
22
  @instance ||= new
23
23
  end
24
+
25
+ # Get the Country object for the given CC.
26
+ #
27
+ def [] cc
28
+ countries[cc.size][cc]
29
+ end
24
30
 
25
31
  # 00 for the standard international call prefix.
26
32
  # http://en.wikipedia.org/wiki/List_of_international_call_prefixes
@@ -37,11 +43,18 @@ module Phony
37
43
  #
38
44
  number.gsub!(@@basic_normalizing_pattern, EMPTY_STRING) || number
39
45
  end
40
- def normalize number
46
+ def normalize number, options = {}
41
47
  clean! number
48
+ countrify! number, options
42
49
  country, cc, rest = split_cc number
43
50
  @normalize_format % [cc, country.normalize(rest)]
44
51
  end
52
+
53
+ def countrify! number, options = {}
54
+ if cc = options[:cc]
55
+ self[cc].countrify! number
56
+ end
57
+ end
45
58
 
46
59
  # Splits this number into cc, ndc and locally split number parts.
47
60
  #
@@ -123,7 +136,7 @@ module Phony
123
136
  presumed_cc = ''
124
137
  1.upto(3) do |i|
125
138
  presumed_cc << rest.slice!(0..0)
126
- country = splitter_mapping[i][presumed_cc]
139
+ country = countries[i][presumed_cc]
127
140
  return [country, presumed_cc, rest] if country
128
141
  end
129
142
  # This line is never reached as CCs are in prefix code.
@@ -163,9 +176,9 @@ module Phony
163
176
  country_code = country_code.to_s
164
177
  optimized_country_code_access = country_code.size
165
178
 
166
- @splitter_mapping ||= {}
167
- @splitter_mapping[optimized_country_code_access] ||= {}
168
- @splitter_mapping[optimized_country_code_access][country_code] = country
179
+ @countries ||= {}
180
+ @countries[optimized_country_code_access] ||= {}
181
+ @countries[optimized_country_code_access][country_code] = country
169
182
  end
170
183
 
171
184
  end
@@ -50,7 +50,7 @@ module Phony
50
50
  # country '27', # CC, followed by rules, for example fixed(2) >> ...
51
51
  #
52
52
  def country country_code, definition, options = {}
53
- definition.with options
53
+ definition.with options.merge! :cc => country_code
54
54
  Phony::CountryCodes.instance.add country_code, definition
55
55
  end
56
56
 
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Phony.normalize' do
4
+
5
+ describe 'cases' do
6
+
7
+ it 'handles the US (with cc) correctly' do
8
+ Phony.normalize('+1 724 999 9999').should == '17249999999'
9
+ end
10
+ it 'handles the US (with cc and cc option) correctly' do
11
+ Phony.normalize('+1 724 999 9999', cc: '1').should == '17249999999'
12
+ end
13
+ it 'handles the US (without cc) correctly' do
14
+ Phony.normalize('(310) 555-2121', cc: '1').should == '13105552121'
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -7,7 +7,11 @@ describe 'country descriptions' do
7
7
  def self.it_splits number, expected
8
8
  it { Phony.split(number).should == expected }
9
9
  end
10
-
10
+
11
+ describe 'regression' do
12
+ it_splits '33630588659', ["33", "6", "30", "58", "86", "59"]
13
+ end
14
+
11
15
  describe 'splitting' do
12
16
  describe 'Ascension Island' do
13
17
  it_splits '2473551', ['247', false, '3551']
@@ -268,7 +272,8 @@ describe 'country descriptions' do
268
272
  it_splits '919911182111', ['91', '99', '111', '82', '111'] # mobile
269
273
  it_splits '912212345678', ['91', '22', '123', '45', '678'] # New Delhi
270
274
  it_splits '911411234567', ['91', '141', '123', '45', '67'] # Jaipur
271
- it_splits '913525123456', ['91', '3525', '123', '456' ] # DALKHOLA
275
+ it_splits '913525123456', ['91', '3525', '123', '456'] # DALKHOLA
276
+ it_splits '914433993939', ['91', '44', '339', '93', '939'] #
272
277
  end
273
278
 
274
279
  describe 'Iran' do
@@ -4,6 +4,16 @@ require 'spec_helper'
4
4
 
5
5
  describe Phony do
6
6
 
7
+ describe 'OO interface' do
8
+ describe '[]' do
9
+ it '' do
10
+ us = described_class['1']
11
+ us.plausible?('4159224711').should be_true
12
+ us.countrify!('4159224711').should == '14159224711'
13
+ end
14
+ end
15
+ end
16
+
7
17
  describe 'normalize' do
8
18
  describe 'exceptions' do
9
19
  it 'raises on nil' do
@@ -106,6 +116,9 @@ describe Phony do
106
116
  it 'should format New Zealand landline numbers' do
107
117
  Phony.format('6493791234').should == '+64 9 379 1234'
108
118
  end
119
+ it 'should format Indian numbers' do
120
+ Phony.format('914433993939').should == '+91 44 339 93 939'
121
+ end
109
122
  end
110
123
  describe 'international' do
111
124
  it 'should format north american numbers' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phony
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.beta10
4
+ version: 2.0.0.beta11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Hanke
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-17 00:00:00.000000000 Z
11
+ date: 2013-09-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ! 'Fast international phone number (E164 standard) normalizing, splitting
14
14
  and formatting. Lots of formatting options: International (+.., 00..), national
@@ -70,6 +70,7 @@ files:
70
70
  - lib/phony/vanity.rb
71
71
  - lib/phony.rb
72
72
  - README.textile
73
+ - spec/functional/normalize_spec.rb
73
74
  - spec/lib/phony/countries_spec.rb
74
75
  - spec/lib/phony/country_codes_spec.rb
75
76
  - spec/lib/phony/country_spec.rb
@@ -110,6 +111,7 @@ specification_version: 4
110
111
  summary: Fast international phone number (E164 standard) normalizing, splitting and
111
112
  formatting.
112
113
  test_files:
114
+ - spec/functional/normalize_spec.rb
113
115
  - spec/lib/phony/countries_spec.rb
114
116
  - spec/lib/phony/country_codes_spec.rb
115
117
  - spec/lib/phony/country_spec.rb