phony 1.2.8 → 1.2.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -74,8 +74,9 @@ module Phony
74
74
  '43' => Countries::Austria,
75
75
  '44' => fixed(2), # TODO United Kingdom of Great Britain and Northern Ireland
76
76
  '45' => fixed(2, # Denmark
77
- :local_format => [2, 2, 2]
78
- ),
77
+ :local_format => [2, 2, 2],
78
+ :service_ndcs => %w{112 114}
79
+ ), # Denmark has no NDC, but 4 groups of 2 digits. I'm faking it here.
79
80
  '46' => Countries::Sweden,
80
81
  '47' => fixed(4, # Norway
81
82
  :local_format => [4]
@@ -3,6 +3,7 @@
3
3
  # Taken from: http://en.wikipedia.org/wiki/Telephone_numbers_in_Belgium
4
4
  #
5
5
  Phony::Countries::Belgium = Phony::Country.configured :local_format => [3, 5],
6
+ :mobile_local_format => [6],
6
7
  :ndc_fallback_length => 2,
7
8
  :ndc_mapping => {
8
9
  :landline => [
@@ -13,7 +13,7 @@ Phony::Countries::Hungary = Phony::Country.configured :local_format => [3, 4], #
13
13
  :landline => [
14
14
  '1'
15
15
  ],
16
- :mobile => [], # TODO
16
+ # TODO :mobile => [],
17
17
  :service => [
18
18
  '104',
19
19
  '105',
@@ -3,7 +3,7 @@
3
3
  # http://en.wikipedia.org/wiki/Telephone_numbers_in_Peru
4
4
  #
5
5
  Phony::Countries::Peru = Phony::Country.configured :local_format => [4, 4],
6
- :local_special_format => [3, 3],
6
+ :service_local_format => [3, 3],
7
7
  :ndc_fallback_length => 2,
8
8
  :ndc_mapping => {
9
9
  :landline => [
@@ -4,6 +4,7 @@
4
4
  #
5
5
  Phony::Countries::Portugal = Phony::Country.configured :local_format => [3, 4],
6
6
  :local_special_format => [3, 3],
7
+ :local_mobile_format => [3, 3],
7
8
  :ndc_fallback_length => 3,
8
9
  :ndc_mapping => {
9
10
  :landline => [
@@ -4,6 +4,7 @@
4
4
  #
5
5
  Phony::Countries::Romania = Phony::Country.configured :local_format => [3, 4], # Also captures 3, 3 on a fallback.
6
6
  :local_special_format => [3, 3],
7
+ :local_mobile_format => [3, 4],
7
8
  :ndc_fallback_length => 3,
8
9
  :ndc_mapping => {
9
10
  :landline => [
data/lib/phony/country.rb CHANGED
@@ -4,16 +4,20 @@ module Phony
4
4
  #
5
5
  class Country
6
6
 
7
- def initialize national_code, special_code = nil
7
+ def initialize national_code, special_code = nil, mobile_code = nil
8
8
  @national_code = national_code
9
9
  @special_code = special_code
10
+ @mobile_code = mobile_code
10
11
  end
11
12
 
12
13
  #
13
14
  #
14
15
  def split national_number
15
16
  ndc, *rest = @special_code.split national_number if @special_code
16
- ndc, *rest = @national_code.split national_number unless rest && !rest.empty?
17
+ return [ndc, *rest] if rest && !rest.empty?
18
+ ndc, *rest = @mobile_code.split national_number if @mobile_code
19
+ return [ndc, *rest] if rest && !rest.empty?
20
+ ndc, *rest = @national_code.split national_number
17
21
  [ndc, *rest]
18
22
  end
19
23
 
@@ -27,8 +31,10 @@ module Phony
27
31
  #
28
32
  def normalize national_number
29
33
  normalized = @special_code.normalize national_number if @special_code
30
- normalized = @national_code.normalize national_number unless normalized && !normalized.empty?
31
- normalized
34
+ return normalized if normalized && !normalized.empty?
35
+ normalized = @mobile_code.normalize national_number if @mobile_code
36
+ return normalized if normalized && !normalized.empty?
37
+ @national_code.normalize national_number
32
38
  end
33
39
 
34
40
  # Is this national number a vanity number?
@@ -77,8 +83,14 @@ module Phony
77
83
  service_local_splitter = Phony::LocalSplitter.instance_for options[:service_local_format] || [3, 6]
78
84
  service_code = Phony::NationalCode.new service_national_splitter, service_local_splitter, normalize
79
85
  end
86
+ if ndc_mapping[:mobile]
87
+ mobile_local_format = options[:mobile_local_format] || options[:local_format] || [9]
88
+ mobile_national_splitter = Phony::NationalSplitters::Variable.new nil, :mobile => ndc_mapping[:mobile]
89
+ mobile_local_splitter = Phony::LocalSplitter.instance_for mobile_local_format
90
+ mobile_code = Phony::NationalCode.new mobile_national_splitter, mobile_local_splitter, normalize
91
+ end
80
92
 
81
- new national_code, service_code
93
+ new national_code, service_code, mobile_code
82
94
  end
83
95
 
84
96
  # Gets a configured country instance with fixed length ndc code.
@@ -104,9 +116,9 @@ module Phony
104
116
 
105
117
  service_code = nil
106
118
  if service_ndcs
107
- service_local_format = options[:service_local_format] || local_format
119
+ service_local_format = options[:service_local_format] || local_format || [3, 3]
108
120
  service_national_splitter = Phony::NationalSplitters::Variable.new nil, :service => service_ndcs
109
- service_local_splitter = Phony::LocalSplitter.instance_for service_local_format || [3, 3]
121
+ service_local_splitter = Phony::LocalSplitter.instance_for service_local_format
110
122
  service_code = Phony::NationalCode.new service_national_splitter, service_local_splitter, normalize
111
123
  end
112
124
 
@@ -7,6 +7,7 @@ describe Phony::Country do
7
7
  before(:each) do
8
8
  @country = Phony::Country.configured :local_format => [3, 2, 2],
9
9
  :service_local_format => [3, 3],
10
+ :mobile_local_format => [1, 2, 4],
10
11
  :ndc_fallback_length => 4,
11
12
  :ndc_mapping => {
12
13
  :normal => ['44'],
@@ -21,7 +22,7 @@ describe Phony::Country do
21
22
  @country.split('800333666').should == ['800', '333', '666']
22
23
  end
23
24
  it 'works with mobile numbers' do
24
- @country.split('764333532').should == ['76', '433', '35', '32']
25
+ @country.split('764333532').should == ['76', '4', '33', '3532']
25
26
  end
26
27
  it 'uses the fallback if it is not in the mapping' do
27
28
  @country.split('123456789').should == ['1234', '567', '89']
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: phony
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.2.8
5
+ version: 1.2.9
6
6
  platform: ruby
7
7
  authors:
8
8
  - Florian Hanke
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-02-02 00:00:00 +01:00
13
+ date: 2011-02-03 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies: []
16
16