phony 1.7.12 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -13,7 +13,7 @@ This gem can normalize, format and split E164 numbers.
13
13
 
14
14
  The (admittedly crazy) *goal* of this Gem is to be able to format/split all phone numbers in the world.
15
15
 
16
- Currently handles Abhas, Afghan, Algerian, Austrian, Australian, Belgian, Brazilian, Chilean, Chinese, Croatian, Cuban, Czech, Danish, Dutch, Egyptian, French, German, Ghanan, Greek, Hungarian, Italian, Irish, Kazakh, Lithuanian, Malaysian, Mexican, New Zealand, Norwegian, Peruvian, Polish, Romanian, Russian, Singapore, Slovakian, South African, South Korean, South Osetian, Spanish, Swedish, Swiss, Thailand, Tunisian, Turkish, Liechtenstein, UK, US, Venezuelan, and Vietnamese numbers.
16
+ Currently handles Abhas, Afghan, Algerian, Austrian, Australian, Belgian, Brazilian, Chilean, Chinese, Croatian, Cuban, Czech, Danish, Dutch, Egyptian, French, German, Ghanan, Greek, Hungarian, Italian, Irish, Kazakh, Lithuanian, Luxembourgian, Malaysian, Mexican, New Zealand, Nigerian, Norwegian, Peruvian, Polish, Romanian, Russian, Singapore, Slovakian, South African, South Korean, South Osetian, Spanish, Swedish, Swiss, Thailand, Tunisian, Turkish, Liechtenstein, UK, US, Venezuelan, and Vietnamese numbers.
17
17
  And to some extent, all others. Just try if it works for you.
18
18
 
19
19
  If it doesn't, please "enter an issue":http://github.com/floere/phony/issues.
@@ -295,7 +295,7 @@ Phony.define do
295
295
  country '244', todo # Angola
296
296
  country '245', todo # Guinea-Bissau
297
297
  country '246', todo # Diego Garcia
298
- country '247', todo # Ascension
298
+ country '247', none >> split(4) # Ascension
299
299
  country '248', todo # Seychelles
300
300
  country '249', todo # Sudan
301
301
 
data/lib/phony/dsl.rb CHANGED
@@ -44,7 +44,10 @@ module Phony
44
44
 
45
45
  class DSL
46
46
 
47
+ # Start defining a country.
47
48
  #
49
+ # Example:
50
+ # country '27', # CC, followed by rules, for example fixed(2) >> ...
48
51
  #
49
52
  def country country_code, country, validator = nil
50
53
  Phony::CountryCodes.instance.add country_code, country
@@ -62,13 +65,36 @@ module Phony
62
65
  # * length: Length of the fixed length NDC.
63
66
  # * options: Set :zero to false to not add a zero on format :national.
64
67
  #
68
+ # Example:
69
+ # country '33', fixed(1) >> split(2,2,2,2) # France, uses a fixed NDC of size 1.
70
+ #
65
71
  def fixed length, options = {}
66
72
  options[:zero] = true if options[:zero].nil?
67
73
  NationalSplitters::Fixed.instance_for length, options
68
74
  end
75
+
76
+ # This country does not use an NDC. This rule will always match.
77
+ #
78
+ # Examples:
79
+ # * Denmark
80
+ # * Norway
81
+ # * Iceland
82
+ # (and more)
83
+ #
69
84
  def none
70
85
  NationalSplitters::None.instance_for
71
86
  end
87
+
88
+ # If you have a number of (possibly) variable length NDCs
89
+ # that cannot be well expressed via regexp, use this.
90
+ #
91
+ # Parameters:
92
+ # * ndcs A list of ndcs of variable length.
93
+ # * Can contain :max_length => number to denote a maximum NDC length.
94
+ #
95
+ # Example:
96
+ # country '51', one_of('103', '105') >> split(3,3) # If it's either 103 or 105, then split ...
97
+ #
72
98
  def one_of *ndcs
73
99
  options = Hash === ndcs.last ? ndcs.pop : {}
74
100
 
@@ -78,6 +104,19 @@ module Phony
78
104
 
79
105
  NationalSplitters::Variable.new options[:max_length], ndcs
80
106
  end
107
+
108
+ # If you have a number of (possibly) variable length NDCs
109
+ # that can be well expressed via regexp, use this.
110
+ #
111
+ # Parameters:
112
+ # * regex A regexp describing the NDCs (First group must contain the NDC, eg. /^(0\d{2})\d+$/) contains all NDCs with 0xx...
113
+ #
114
+ # Example:
115
+ # country '52',
116
+ # match(/^(0\d{2})\d+$/) >> split(2,2,2,2) |
117
+ # match(/^(33|55|81)\d+$/) >> split(2,2,2,2) |
118
+ # match(/^(\d{3})\d+$/) >> split(3,2,2)
119
+ #
81
120
  def match regex, options = {}
82
121
  # Check if regexp has a group in it.
83
122
  #
@@ -86,6 +125,9 @@ module Phony
86
125
  on_fail_take = options.delete :on_fail_take
87
126
  NationalSplitters::Regex.instance_for regex, options[:on_fail_take], options
88
127
  end
128
+
129
+ # This country still uses a default NDC (and needs to be done, hence the todo).
130
+ #
89
131
  def todo
90
132
  none >> NationalSplitters::Default.instance_for
91
133
  end
@@ -93,18 +135,44 @@ module Phony
93
135
  # Local splitters.
94
136
  #
95
137
 
138
+ # Splits the number into groups of given sizes.
96
139
  #
140
+ # Example:
141
+ # match(/^(0\d{2})\d+$/) >> split(2,2,2,2) # If it matches, split in 4 groups of size 2.
97
142
  #
98
143
  def split *local
99
144
  local << local.pop + 10 # Allow for call-through numbers with an arbitrary size.
100
145
  LocalSplitters::Fixed.instance_for local
101
146
  end
147
+
148
+ # Matches on the rest of the number and splits according
149
+ # to the given value for the regexp key.
150
+ #
151
+ # Options:
152
+ # * fallback A fallback amount of group sizes in case it doesn't match.
153
+ #
154
+ # Example:
155
+ # country '47',
156
+ # none >> matched_split(/^[1].*$/ => [3],
157
+ # /^[489].*$/ => [3,2,3],
158
+ # :fallback => [2,2,2,2])
159
+ #
102
160
  def matched_split options = {}
103
161
  Phony::LocalSplitters::Regex.instance_for options
104
162
  end
105
163
 
106
164
  # Validators
107
165
  #
166
+
167
+ # Which NDCs are explicitly invalid?
168
+ #
169
+ # Takes a regexp or a string.
170
+ #
171
+ # Example:
172
+ # country '1',
173
+ # fixed(3, :zero => false) >> split(3,4),
174
+ # invalid_ndcs('911') # The regexp /911/ would also work.
175
+ #
108
176
  def invalid_ndcs ndc
109
177
  Validator.new.ndc_check ndc
110
178
  end
@@ -9,7 +9,7 @@ module Phony
9
9
  end
10
10
 
11
11
  def split national_number
12
- [nil, nil, national_number]
12
+ [national_number]
13
13
  end
14
14
 
15
15
  end
@@ -9,6 +9,9 @@ describe 'country descriptions' do
9
9
  end
10
10
 
11
11
  describe 'splitting' do
12
+ describe 'Ascension Island' do
13
+ it_splits '2473551', ['247', false, '3551']
14
+ end
12
15
  describe 'Afghanistan' do
13
16
  it_splits '93201234567', ['93', '20', '1234567'] # Kabul
14
17
  end
@@ -8,7 +8,7 @@ describe Phony::DSL do
8
8
 
9
9
  it 'has a todo' do
10
10
  Phony.define do
11
- todo.split("123456789012345").should == [nil, false, nil, nil, '123456789012345']
11
+ todo.split("123456789012345").should == [nil, false, '123456789012345']
12
12
  end
13
13
  end
14
14
 
@@ -22,6 +22,9 @@ describe 'validations' do
22
22
  # it 'is correct' do
23
23
  # Phony.plausible?('+1911').should be_false
24
24
  # end
25
+ it "correctly plausibilizes to#{}do countries" do
26
+ Phony.plausible?('6327332350').should be_true
27
+ end
25
28
  it 'is correct' do
26
29
  Phony.plausible?('45 44 11 22 33').should be_true
27
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phony
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.12
4
+ version: 1.8.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-25 00:00:00.000000000 Z
12
+ date: 2012-08-30 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'Fast international phone number (E164 standard) normalizing, splitting
15
15
  and formatting. Lots of formatting options: International (+.., 00..), national