phony 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/phony.rb CHANGED
@@ -39,11 +39,11 @@ require File.expand_path '../phony/countries/united_kingdom', __FILE__
39
39
  require File.expand_path '../countries', __FILE__
40
40
 
41
41
  module Phony
42
-
42
+
43
43
  # Phony uses a single country codes instance.
44
44
  #
45
45
  @codes = CountryCodes.instance
46
-
46
+
47
47
  # Normalizes the given number.
48
48
  #
49
49
  # Useful before inserting the number into a database.
@@ -54,7 +54,7 @@ module Phony
54
54
  def self.normalize! phone_number
55
55
  @codes.normalize phone_number
56
56
  end
57
-
57
+
58
58
  # Splits the phone number into pieces according to the country codes.
59
59
  #
60
60
  def self.split phone_number
@@ -63,7 +63,7 @@ module Phony
63
63
  def self.split! phone_number
64
64
  @codes.split phone_number
65
65
  end
66
-
66
+
67
67
  # Formats a E164 number according to local customs.
68
68
  #
69
69
  def self.formatted phone_number, options = {}
@@ -72,7 +72,7 @@ module Phony
72
72
  def self.formatted! phone_number, options = {}
73
73
  @codes.formatted phone_number, options
74
74
  end
75
-
75
+
76
76
  # def self.service? number
77
77
  # @codes.service? number.dup
78
78
  # end
@@ -82,19 +82,19 @@ module Phony
82
82
  # def self.landline? number
83
83
  # @codes.landline? number.dup
84
84
  # end
85
-
85
+
86
86
  # Returns true if there is a character in the number
87
87
  # after the first four numbers.
88
88
  #
89
89
  def self.vanity? phone_number
90
90
  @codes.vanity? phone_number.dup
91
91
  end
92
-
92
+
93
93
  # Converts any character in the vanity_number to its numeric representation.
94
94
  # Does not check if the passed number is a valid vanity_number, simply does replacement.
95
95
  #
96
96
  def self.vanity_to_number vanity_number
97
97
  @codes.vanity_to_number vanity_number.dup
98
98
  end
99
-
99
+
100
100
  end
@@ -1,5 +1,3 @@
1
- include Phony::DSL
2
-
3
1
  # Austria uses a variable-length ndc code, thus we use a separate file to not let all_other.rb explode.
4
2
  #
5
3
  ndcs = [
@@ -25,8 +23,8 @@ ndcs = [
25
23
  ]
26
24
 
27
25
  mobile = [
28
- '67',
29
- '68',
26
+ '67',
27
+ '68',
30
28
  '644',
31
29
  '650',
32
30
  '651',
@@ -64,7 +62,9 @@ service = [
64
62
  '901',
65
63
  '930',
66
64
  '931',
67
- '939'
65
+ '939'
68
66
  ]
69
67
 
70
- Phony::Countries::Austria = one_of(service + mobile + ndcs, :max_length => 4) >> split(10)
68
+ Phony.define do
69
+ country '43', one_of(service + mobile + ndcs, :max_length => 4) >> split(10)
70
+ end
@@ -1,5 +1,3 @@
1
- include Phony::DSL
2
-
3
1
  # Belgium uses a variable-length ndc code, thus we use a separate file to not let all_other.rb explode.
4
2
  #
5
3
  # Taken from: http://en.wikipedia.org/wiki/Telephone_numbers_in_Belgium
@@ -28,6 +26,8 @@ service = [
28
26
  '909' # Premium
29
27
  ]
30
28
 
31
- Phony::Countries::Belgium = one_of(service) >> split(3,3) |
32
- match(mobile_regex) >> split(6) |
33
- one_of(ndcs, :max_length => 2) >> split(3,5)
29
+ Phony.define do
30
+ country '32', one_of(service) >> split(3,3) |
31
+ match(mobile_regex) >> split(6) |
32
+ one_of(ndcs, :max_length => 2) >> split(3,5)
33
+ end
@@ -8,6 +8,7 @@
8
8
  #
9
9
  service = %w{130 131 132 133 134 135 136 137 139 147 149}
10
10
 
11
- Phony::Countries::Chile = one_of(*service) >> split(3,3) |
12
-
13
- one_of('2', '9', :max_length => 2) >> split(8) # Santiago (2) and mobile.
11
+ Phony.define do
12
+ country '56', one_of(*service) >> split(3,3) |
13
+ one_of('2', '9', :max_length => 2) >> split(8) # Santiago (2) and mobile.
14
+ end
@@ -1,10 +1,8 @@
1
- include Phony::DSL
2
-
3
1
  # Chinese phone numbers.
4
2
  #
5
3
  # http://en.wikipedia.org/wiki/Telephone_numbers_in_China
6
4
  #
7
- # Note: Tibet partially uses 4-digit NDC codes. This is NOT YET reflected here.
5
+ # Note: Tibet partially uses 4-digit NDC codes. This is NOT YET reflected here.
8
6
  #
9
7
  ndcs = [
10
8
  '10', # Beijing, TODO Confirm – is it 10 or 1?
@@ -22,6 +20,8 @@ ndcs = [
22
20
  mobile = %w{ 130 131 132 133 135 136 137 138 139 145 150 151 152 153 155 156 157 158 159 185 186 187 188 189 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 }
23
21
  service = %w{ 110 114 119 120 122 999 } # Probably not exhaustive. TODO Look at http://www.eoc.com.cn/?action-viewnews-itemid-11493.
24
22
 
25
- Phony::Countries::China = one_of(service) >> split(8) |
26
- one_of(mobile) >> split(4,4) |
27
- one_of(ndcs, :max_length => 3) >> split(4,4)
23
+ Phony.define do
24
+ country '86', one_of(service) >> split(8) |
25
+ one_of(mobile) >> split(4,4) |
26
+ one_of(ndcs, :max_length => 3) >> split(4,4)
27
+ end
@@ -1,5 +1,3 @@
1
- include Phony::DSL
2
-
3
1
  # TODO Move this into the country definitions.
4
2
 
5
3
  # Egyptian phone numbers.
@@ -13,5 +11,7 @@ ndcs = [
13
11
 
14
12
  # TODO Change how one_of works. max_length should be optional.
15
13
  #
16
- Phony::Countries::Egypt = one_of('800') >> split(7) |
17
- one_of(ndcs, :max_length => 2) >> split(8)
14
+ Phony.define do
15
+ Phony::Countries::Egypt = one_of('800') >> split(7) |
16
+ one_of(ndcs, :max_length => 2) >> split(8)
17
+ end
@@ -1,5 +1,3 @@
1
- include Phony::DSL
2
-
3
1
  # Germany uses a variable-length ndc code, thus we use a separate file to not let all_other.rb explode.
4
2
  #
5
3
  # Note: Germany uses a variable ndc format from length 2 to 5.
@@ -4067,6 +4065,8 @@ service = [
4067
4065
  '9009',
4068
4066
  ]
4069
4067
 
4070
- Phony::Countries::Germany = one_of(service) >> split(3,10) |
4071
- match(/^(1[567]\d)\d*$/) >> split(3,10) | # Mobile
4072
- one_of(ndcs, :max_length => 5) >> split(3,10)
4068
+ Phony.define do
4069
+ country '49', one_of(service) >> split(3,10) |
4070
+ match(/^(1[567]\d)\d*$/) >> split(3,10) | # Mobile
4071
+ one_of(ndcs, :max_length => 5) >> split(3,10)
4072
+ end
@@ -1,5 +1,3 @@
1
- include Phony::DSL
2
-
3
1
  # Greece uses a variable-length ndc code, thus we use a separate file to not let all_other.rb explode.
4
2
  #
5
3
  # Note: Greece uses a variable ndc format from length 2 to 4.
@@ -23,4 +21,6 @@ ndcs = [
23
21
  '800' # Service
24
22
  ]
25
23
 
26
- Phony::Countries::Greece = one_of(ndcs, :max_length => 4) >> split(6)
24
+ Phony.define do
25
+ country '30', one_of(ndcs, :max_length => 4) >> split(6)
26
+ end
@@ -1,10 +1,10 @@
1
- include Phony::DSL
2
-
3
1
  # Hungarian phone numbers.
4
2
  #
5
3
  # http://en.wikipedia.org/wiki/Telephone_numbers_in_Hungary
6
4
  #
7
- Phony::Countries::Hungary = one_of('104', '105', '107', '112') >> split(3,3) |
8
- # special
9
- # TODO mobile
10
- one_of('1', :max_length => 2) >> split(3,4)
5
+
6
+ Phony.define do
7
+ country '36', one_of('104', '105', '107', '112') >> split(3,3) | # special
8
+ # TODO mobile
9
+ one_of('1', :max_length => 2) >> split(3,4)
10
+ end
@@ -1,5 +1,3 @@
1
- include Phony::DSL
2
-
3
1
  # Italian phone numbers.
4
2
  #
5
3
  # http://en.wikipedia.org/wiki/Telephone_numbers_in_Italy
@@ -15,7 +13,7 @@ ndcs = [
15
13
  '0161', # Vercelli
16
14
  '0165', # Aosta, Courmayeur
17
15
  '0166', # Cervinia
18
- '0171', # Cuneo
16
+ '0171', # Cuneo
19
17
  '0185', # Tigullio
20
18
  '0187', # La Spezia and Cinque Terre
21
19
  '019', # Savona
@@ -63,27 +61,27 @@ ndcs = [
63
61
  '0771', # Fondi
64
62
  '0773', # Latina
65
63
  '0775', # Frosinone
66
- '0789', # Olbia and Costa Smeralda
64
+ '0789', # Olbia and Costa Smeralda
67
65
  '079', # Sassari
68
66
  '080', # Bari
69
67
  '081', # Naples
70
68
  '0823', # Caserta
71
69
  '0824', # Benevento
72
- '0825', # Avellino
70
+ '0825', # Avellino
73
71
  '085', # Pescara
74
- '0865', # Isernia
72
+ '0865', # Isernia
75
73
  '0871', # Chieti
76
74
  '0872', # Lanciano
77
75
  '0873', # Vasto
78
- '0874', # Campobasso
76
+ '0874', # Campobasso
79
77
  '0875', # Termoli
80
78
  '0881', # Foggia
81
- '089', # Salerno
79
+ '089', # Salerno
82
80
  '090', # Messina
83
81
  '091', # Palermo
84
82
  '092', # Agrigento
85
83
  '095', # Catania
86
- '0961', # Catanzaro
84
+ '0961', # Catanzaro
87
85
  '0965', # Reggio di Calabria
88
86
  '0971', # Potenza
89
87
  '099', # Taranto
@@ -155,10 +153,10 @@ service = [ # Not exhaustive.
155
153
 
156
154
  # TODO Normalize!
157
155
  #
158
- # Phony::Countries::Italy = one_of(*service) >> split(3,3) |
159
- # one_of(*mobile) >> split(3,4) |
156
+ # Phony::Countries::Italy = one_of(*service) >> split(3,3) |
157
+ # one_of(*mobile) >> split(3,4) |
160
158
  # one_of(*ndcs, :max_length => 3) >> split(3,4)
161
-
159
+
162
160
  handlers = []
163
161
  handlers << Phony::NationalCode.new(
164
162
  Phony::NationalSplitters::Variable.new(nil, service),
@@ -175,4 +173,7 @@ handlers << Phony::NationalCode.new(
175
173
  Phony::LocalSplitters::Fixed.instance_for([3, 4]),
176
174
  false
177
175
  )
178
- Phony::Countries::Italy = Phony::Country.new *handlers
176
+
177
+ Phony.define do
178
+ country '39', Phony::Country.new(*handlers)
179
+ end
@@ -1,5 +1,3 @@
1
- include Phony::DSL
2
-
3
1
  # Malaysian phone numbers.
4
2
  #
5
3
  # http://en.wikipedia.org/wiki/Telephone_numbers_in_Malaysia
@@ -16,6 +14,8 @@ ndcs = [
16
14
  mobile = %w{ 10 11 12 13 14 153 154 156 158 16 17 18 19 }
17
15
  service = %w{ 100 101 102 103 104 108 112 991 994 995 999 }
18
16
 
19
- Phony::Countries::Malaysia = one_of(service) >> split(3,3) |
20
- one_of(mobile) >> split(8) |
21
- one_of(ndcs, :max_length => 2) >> split(8)
17
+ Phony.define do
18
+ country '60', one_of(service) >> split(3,3) |
19
+ one_of(mobile) >> split(8) |
20
+ one_of(ndcs, :max_length => 2) >> split(8)
21
+ end
@@ -50,6 +50,8 @@ service = [
50
50
  '909', # Betaalde informatienummers
51
51
  ]
52
52
 
53
- Phony::Countries::Netherlands = one_of(service) >> split(3,3) |
54
- one_of(['6'] + ndcs, :max_length => 3) >> split(8) # mobile + landline
55
- # 8 is for mobile numbers, other numbers will work as well (they use 7).
53
+ Phony.define do
54
+ country '31', one_of(service) >> split(3,3) |
55
+ one_of(['6'] + ndcs, :max_length => 3) >> split(8) # mobile + landline
56
+ # 8 is for mobile numbers, other numbers will work as well (they use 7).
57
+ end
@@ -1,5 +1,3 @@
1
- include Phony::DSL
2
-
3
1
  # Romanian phone numbers.
4
2
  #
5
3
  # http://en.wikipedia.org/wiki/Romania_telephone_area_codes
@@ -27,6 +25,8 @@ service = [
27
25
  '906',
28
26
  ]
29
27
 
30
- Phony::Countries::Romania = one_of(*service) >> split(3,3) |
31
- match(/^(7[1-8])\d*$/) >> split(3,4) |
32
- one_of('21', '31', :max_length => 3) >> split(3,4) # Bucureşti
28
+ Phony.define do
29
+ country '40', one_of(*service) >> split(3,3) |
30
+ match(/^(7[1-8])\d*$/) >> split(3,4) |
31
+ one_of('21', '31', :max_length => 3) >> split(3,4) # Bucureşti
32
+ end
@@ -1,5 +1,3 @@
1
- include Phony::DSL
2
-
3
1
  # South Korean (Republic of Korea) phone numbers.
4
2
  #
5
3
  # http://en.wikipedia.org/wiki/Telephone_numbers_in_South_Korea
@@ -10,6 +8,8 @@ include Phony::DSL
10
8
  service = %w{ 30 50 60 70 80 100 101 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 125 127 128 129 131 132 134 141 182 188 }
11
9
  mobile = ('10'..'19').to_a
12
10
 
13
- Phony::Countries::SouthKorea = one_of(*service) >> split(3,3) |
14
- one_of(*mobile) >> split(4,4) |
15
- one_of('2', :max_length => 2) >> split(4,4) # Seoul
11
+ Phony.define do
12
+ country '82', one_of(*service) >> split(3,3) |
13
+ one_of(*mobile) >> split(4,4) |
14
+ one_of('2', :max_length => 2) >> split(4,4) # Seoul
15
+ end
@@ -53,5 +53,7 @@ service = [
53
53
  '11414', # Police
54
54
  ]
55
55
 
56
- Phony::Countries::Sweden = one_of(service) >> split(3,3) |
57
- one_of(ndcs + mobile, :max_length => 3) >> split(8)
56
+ Phony.define do
57
+ country '46', one_of(service) >> split(3,3) |
58
+ one_of(ndcs + mobile, :max_length => 3) >> split(8)
59
+ end
@@ -17,7 +17,7 @@ area_code_2_national = Phony::NationalSplitters::Variable.new nil, [
17
17
  '23', # Southampton, Portsmith
18
18
  '24', # Coventry
19
19
  '28', # Northern Ireland
20
- '29', # Cardiff
20
+ '29', # Cardiff
21
21
  ]
22
22
  area_code_2_local = Phony::LocalSplitters::Fixed.instance_for [4, 4]
23
23
  handlers << Phony::NationalCode.new(area_code_2_national, area_code_2_local)
@@ -26,10 +26,10 @@ area_code_3_national = Phony::NationalSplitters::Variable.new nil, [
26
26
  # Geographical.
27
27
  #
28
28
  '113', # Leeds
29
- '114', # Sheffield
30
- '115', # Nottingham
31
- '116', # Leicester
32
- '117', # Bristol
29
+ '114', # Sheffield
30
+ '115', # Nottingham
31
+ '116', # Leicester
32
+ '117', # Bristol
33
33
  '118', # Reading
34
34
  '121', # Birmingham
35
35
  '131', # Edinburgh
@@ -46,7 +46,7 @@ area_code_3_national = Phony::NationalSplitters::Variable.new nil, [
46
46
  '842',
47
47
  '843',
48
48
  '844',
49
- '845',
49
+ '845',
50
50
  '870',
51
51
  '871',
52
52
  '872',
@@ -70,7 +70,7 @@ area_code_45_national = Phony::NationalSplitters::Variable.new 6, [
70
70
  '1382', # Dundee
71
71
  '1429', # Hartlepool
72
72
  '1482', # Hull
73
- '1527', # Redditch
73
+ '1527', # Redditch
74
74
  '1582', # Luton
75
75
  '1670', # Morpeth
76
76
  '1730', # Petersfield
@@ -104,4 +104,6 @@ area_code_45_national = Phony::NationalSplitters::Variable.new 6, [
104
104
  area_code_45_local = Phony::LocalSplitters::Fixed.instance_for [6]
105
105
  handlers << Phony::NationalCode.new(area_code_45_national, area_code_45_local)
106
106
 
107
- Phony::Countries::UnitedKingdom = Phony::Country.new *handlers
107
+ Phony.define do
108
+ country '44', Phony::Country.new(*handlers)
109
+ end
data/lib/phony/dsl.rb CHANGED
@@ -1,37 +1,47 @@
1
- # # Ideas for a DSL:
2
- # #
3
- #
4
- # # Switzerland
1
+ # # Switzerland (simplified):
5
2
  # #
6
3
  # country('41', fixed(2) >> local([3,2,2]))
7
- # country(match(/^1.*$/) >> [3,3] || none >> [2,2,2,2])
8
- #
9
- # # Germany. Too big.
10
- # #
11
- # country('49', Countries::Germany)
12
- # #
13
- # # … and in Germany:
4
+ #
5
+ # # Germany. Too big, we use a separate file:
14
6
  # #
15
7
  # include Phony::DSL
16
- # Countries::Germany = match(...) >> split([...]) ||
17
- # one_of([...], :max_length => 5) >> split([...])
18
- #
19
- # # Denmark.
8
+ # country '49', match(...) >> split([...]) ||
9
+ # one_of([...], :max_length => 5) >> split([...])
10
+ #
11
+ # # Denmark:
12
+ # #
13
+ # country('45', none >> split([2,2,2,2]))
14
+ #
15
+ # # Hungary:
20
16
  # #
21
- # country('45', none >> split([2,2,2,2])) # Denmark.
22
- #
23
- # # Hungary.
24
- # #
25
17
  # country('36',
26
18
  # match(/^104|105|107|112/) >> split([3,3]) ||
27
19
  # one_of([1], :max_length => 2) >> split([3,4])
28
20
  # )
29
21
 
30
22
  module Phony
31
-
32
- module DSL
33
23
 
34
- #
24
+ # For country definitions.
25
+ #
26
+ # There are two styles: With or without block.
27
+ # Use the block if you have multiple.
28
+ #
29
+ # Examples:
30
+ # Phony.define do
31
+ # country ...
32
+ # end
33
+ #
34
+ # Phony.define.country ...
35
+ #
36
+ def self.define
37
+ dsl = DSL.new
38
+ dsl.instance_eval &Proc.new if block_given?
39
+ dsl
40
+ end
41
+
42
+ class DSL
43
+
44
+ #
35
45
  #
36
46
  def country country_code, country
37
47
  Phony::CountryCodes.instance.add country_code, country
@@ -39,8 +49,8 @@ module Phony
39
49
 
40
50
  # National matcher & splitters.
41
51
  #
42
-
43
- #
52
+
53
+ #
44
54
  #
45
55
  def fixed size
46
56
  NationalSplitters::Fixed.instance_for size
@@ -50,20 +60,20 @@ module Phony
50
60
  end
51
61
  def one_of *ndcs
52
62
  options = Hash === ndcs.last ? ndcs.pop : {}
53
-
63
+
54
64
  # Ruby 1.8 compatibility mode.
55
65
  #
56
66
  ndcs = ndcs.first if Array === ndcs.first
57
-
67
+
58
68
  NationalSplitters::Variable.new options[:max_length], ndcs
59
69
  end
60
70
  def match regex, options = {}
61
71
  NationalSplitters::Regex.instance_for regex, options[:on_fail_take]
62
72
  end
63
-
73
+
64
74
  # Local splitters.
65
75
  #
66
-
76
+
67
77
  #
68
78
  #
69
79
  def split *local
@@ -72,7 +82,7 @@ module Phony
72
82
  def matched_split options = {}
73
83
  Phony::LocalSplitters::Regex.instance_for options
74
84
  end
75
-
85
+
76
86
  end
77
-
87
+
78
88
  end