phony_rails 0.8.1 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -4
- data/Gemfile.lock +50 -35
- data/Guardfile +1 -1
- data/README.md +16 -67
- data/lib/phony_rails/string_extensions.rb +9 -0
- data/lib/phony_rails/version.rb +1 -1
- data/spec/lib/phony_rails_spec.rb +78 -51
- data/spec/spec_helper.rb +2 -0
- metadata +2 -2
data/Gemfile
CHANGED
@@ -9,10 +9,10 @@ gemspec # Specify your gem's dependencies in phony_number.gemspec
|
|
9
9
|
gem 'sqlite3'
|
10
10
|
|
11
11
|
gem 'rake'
|
12
|
-
gem 'rspec', '~> 2.
|
13
|
-
gem 'guard', '~>
|
14
|
-
gem 'guard-bundler', '~>
|
15
|
-
gem 'guard-rspec', '~>
|
12
|
+
gem 'rspec', '~> 2.14.0'
|
13
|
+
gem 'guard', '~> 2.10.1'
|
14
|
+
gem 'guard-bundler', '~> 2.0.0'
|
15
|
+
gem 'guard-rspec', '~> 4.2.0'
|
16
16
|
|
17
17
|
case RbConfig::CONFIG['host_os']
|
18
18
|
when /darwin/i
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
phony_rails (0.8.
|
4
|
+
phony_rails (0.8.2)
|
5
5
|
activesupport (>= 3.0)
|
6
6
|
countries (>= 0.8.2)
|
7
7
|
phony (~> 2.8.3)
|
@@ -9,14 +9,14 @@ PATH
|
|
9
9
|
GEM
|
10
10
|
remote: https://rubygems.org/
|
11
11
|
specs:
|
12
|
-
activemodel (4.1.
|
13
|
-
activesupport (= 4.1.
|
12
|
+
activemodel (4.1.8)
|
13
|
+
activesupport (= 4.1.8)
|
14
14
|
builder (~> 3.1)
|
15
|
-
activerecord (4.1.
|
16
|
-
activemodel (= 4.1.
|
17
|
-
activesupport (= 4.1.
|
15
|
+
activerecord (4.1.8)
|
16
|
+
activemodel (= 4.1.8)
|
17
|
+
activesupport (= 4.1.8)
|
18
18
|
arel (~> 5.0.0)
|
19
|
-
activesupport (4.1.
|
19
|
+
activesupport (4.1.8)
|
20
20
|
i18n (~> 0.6, >= 0.6.9)
|
21
21
|
json (~> 1.7, >= 1.7.7)
|
22
22
|
minitest (~> 5.1)
|
@@ -27,54 +27,69 @@ GEM
|
|
27
27
|
builder (3.2.2)
|
28
28
|
celluloid (0.16.0)
|
29
29
|
timers (~> 4.0.0)
|
30
|
-
|
30
|
+
coderay (1.1.0)
|
31
|
+
connection_pool (2.1.0)
|
31
32
|
countries (0.9.3)
|
32
33
|
currencies (~> 0.4.2)
|
33
34
|
currencies (0.4.2)
|
34
|
-
diff-lcs (1.
|
35
|
-
ffi (1.9.
|
35
|
+
diff-lcs (1.2.5)
|
36
|
+
ffi (1.9.6)
|
37
|
+
formatador (0.2.5)
|
36
38
|
growl (1.0.3)
|
37
|
-
guard (
|
38
|
-
|
39
|
-
|
40
|
-
|
39
|
+
guard (2.10.4)
|
40
|
+
formatador (>= 0.2.4)
|
41
|
+
listen (~> 2.7)
|
42
|
+
lumberjack (~> 1.0)
|
43
|
+
nenv (~> 0.1)
|
44
|
+
pry (>= 0.9.12)
|
45
|
+
thor (>= 0.18.1)
|
46
|
+
guard-bundler (2.0.0)
|
41
47
|
bundler (~> 1.0)
|
42
|
-
guard (~>
|
43
|
-
guard-rspec (
|
44
|
-
guard (
|
48
|
+
guard (~> 2.2)
|
49
|
+
guard-rspec (4.2.10)
|
50
|
+
guard (~> 2.1)
|
51
|
+
rspec (>= 2.14, < 4.0)
|
45
52
|
hitimes (1.2.2)
|
46
53
|
i18n (0.6.11)
|
47
54
|
json (1.8.1)
|
48
|
-
listen (2.
|
55
|
+
listen (2.8.3)
|
49
56
|
celluloid (>= 0.15.2)
|
50
57
|
rb-fsevent (>= 0.9.3)
|
51
58
|
rb-inotify (>= 0.9)
|
52
|
-
|
59
|
+
lumberjack (1.0.9)
|
60
|
+
method_source (0.8.2)
|
61
|
+
minitest (5.5.0)
|
53
62
|
mongoid (4.0.0)
|
54
63
|
activemodel (~> 4.0)
|
55
64
|
moped (~> 2.0.0)
|
56
65
|
origin (~> 2.1)
|
57
66
|
tzinfo (>= 0.3.37)
|
58
|
-
moped (2.0.
|
67
|
+
moped (2.0.2)
|
59
68
|
bson (~> 2.2)
|
60
69
|
connection_pool (~> 2.0)
|
61
70
|
optionable (~> 0.2.0)
|
71
|
+
nenv (0.1.1)
|
62
72
|
optionable (0.2.0)
|
63
73
|
origin (2.1.1)
|
64
|
-
phony (2.8.
|
65
|
-
|
74
|
+
phony (2.8.9)
|
75
|
+
pry (0.10.1)
|
76
|
+
coderay (~> 1.1.0)
|
77
|
+
method_source (~> 0.8.1)
|
78
|
+
slop (~> 3.4)
|
79
|
+
rake (10.4.2)
|
66
80
|
rb-fsevent (0.9.4)
|
67
81
|
rb-inotify (0.9.5)
|
68
82
|
ffi (>= 0.5.0)
|
69
|
-
rspec (2.
|
70
|
-
rspec-core (~> 2.
|
71
|
-
rspec-expectations (~> 2.
|
72
|
-
rspec-mocks (~> 2.
|
73
|
-
rspec-core (2.
|
74
|
-
rspec-expectations (2.
|
75
|
-
diff-lcs (
|
76
|
-
rspec-mocks (2.
|
77
|
-
|
83
|
+
rspec (2.14.1)
|
84
|
+
rspec-core (~> 2.14.0)
|
85
|
+
rspec-expectations (~> 2.14.0)
|
86
|
+
rspec-mocks (~> 2.14.0)
|
87
|
+
rspec-core (2.14.8)
|
88
|
+
rspec-expectations (2.14.5)
|
89
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
90
|
+
rspec-mocks (2.14.6)
|
91
|
+
slop (3.6.0)
|
92
|
+
sqlite3 (1.3.10)
|
78
93
|
thor (0.19.1)
|
79
94
|
thread_safe (0.3.4)
|
80
95
|
timers (4.0.1)
|
@@ -89,12 +104,12 @@ DEPENDENCIES
|
|
89
104
|
activerecord (>= 3.0)
|
90
105
|
countries
|
91
106
|
growl
|
92
|
-
guard (~>
|
93
|
-
guard-bundler (~>
|
94
|
-
guard-rspec (~>
|
107
|
+
guard (~> 2.10.1)
|
108
|
+
guard-bundler (~> 2.0.0)
|
109
|
+
guard-rspec (~> 4.2.0)
|
95
110
|
mongoid (>= 3.0)
|
96
111
|
phony_rails!
|
97
112
|
rake
|
98
113
|
rb-fsevent
|
99
|
-
rspec (~> 2.
|
114
|
+
rspec (~> 2.14.0)
|
100
115
|
sqlite3
|
data/Guardfile
CHANGED
@@ -5,7 +5,7 @@ guard 'bundler' do
|
|
5
5
|
watch(/^.+\.gemspec/)
|
6
6
|
end
|
7
7
|
|
8
|
-
guard 'rspec', :
|
8
|
+
guard 'rspec', cmd: 'bundle exec rspec' do
|
9
9
|
watch(%r{^spec/.+_spec\.rb$})
|
10
10
|
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
11
11
|
watch('spec/spec_helper.rb') { "spec" }
|
data/README.md
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# PhonyRails
|
2
2
|
|
3
|
-
(In its early days :) called PhonyNumber)
|
4
|
-
|
5
3
|
This small Gem adds useful methods to your Rails app to validate, display and save phone numbers.
|
6
4
|
It uses the super awesome Phony gem (https://github.com/floere/phony).
|
7
5
|
|
@@ -23,6 +21,8 @@ Or install it yourself as:
|
|
23
21
|
|
24
22
|
### Normalization / Model Usage
|
25
23
|
|
24
|
+
#### ActiveRecord
|
25
|
+
|
26
26
|
For **ActiveRecord**, in your model add:
|
27
27
|
|
28
28
|
class SomeModel < ActiveRecord::Base
|
@@ -36,6 +36,8 @@ For **ActiveRecord**, in your model add:
|
|
36
36
|
phony_normalized_method :fax_number
|
37
37
|
end
|
38
38
|
|
39
|
+
#### Mongoid
|
40
|
+
|
39
41
|
For **Mongoid**, in keeping with Mongoid plug-in conventions you must include the `Mongoid::Phony` module:
|
40
42
|
|
41
43
|
class SomeModel
|
@@ -45,6 +47,8 @@ For **Mongoid**, in keeping with Mongoid plug-in conventions you must include th
|
|
45
47
|
# methods are same as ActiveRecord usage
|
46
48
|
end
|
47
49
|
|
50
|
+
#### General info
|
51
|
+
|
48
52
|
The `:default_country_code` options is used to specify a country_code when normalizing.
|
49
53
|
|
50
54
|
PhonyRails will also check your model for a country_code method to use when normalizing the number. So `'070-12341234'` with `country_code` 'NL' will get normalized to `'317012341234'`.
|
@@ -60,6 +64,8 @@ You can also do-it-yourself and call:
|
|
60
64
|
PhonyRails.normalize_number('+4790909090', :country_code => 'SE') # => '464790909090' (forced to +46)
|
61
65
|
PhonyRails.normalize_number('+4790909090', :default_country_code => 'SE') # => '4790909090' (still +47 so not changed)
|
62
66
|
|
67
|
+
The country_code should always be a ISO 3166-1 alpha-2 (http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2).
|
68
|
+
|
63
69
|
### Validation
|
64
70
|
|
65
71
|
In your model use the Phony.plausible method to validate an attribute:
|
@@ -118,6 +124,12 @@ You can also use the bang method (phony_formatted!):
|
|
118
124
|
number.phony_formatted!(:normalize => :NL, :format => :international)
|
119
125
|
number # => "+31 10 123 41234"
|
120
126
|
|
127
|
+
You can also easily normalize a phone number String:
|
128
|
+
|
129
|
+
"+31 (0)30 1234 123".phony_normalized # => '31301234123'
|
130
|
+
"(0)30 1234 123".phony_normalized # => '301234123'
|
131
|
+
"(0)30 1234 123".phony_normalized(country_code: 'NL') # => '301234123'
|
132
|
+
|
121
133
|
### Find by normalized number
|
122
134
|
|
123
135
|
Say you want to find a record by a phone number. Best is to normalize user input and compare to an attribute stored in the db.
|
@@ -126,74 +138,11 @@ Say you want to find a record by a phone number. Best is to normalize user input
|
|
126
138
|
|
127
139
|
## Changelog
|
128
140
|
|
129
|
-
0.
|
130
|
-
*
|
131
|
-
* Pinned Phony to v2.3.0.
|
132
|
-
* Fixed regex.
|
133
|
-
|
134
|
-
0.6.0
|
135
|
-
* Support for Phony 2.1 by @pjg.
|
136
|
-
|
137
|
-
0.5.0
|
138
|
-
* Added :strict option to String#phony_formatted.
|
139
|
-
|
140
|
-
0.4.2
|
141
|
-
* Added @fareastside validation for country_code.
|
142
|
-
|
143
|
-
0.4.0/0.4.1
|
144
|
-
* Better Mongoid support by @johnnyshields.
|
145
|
-
|
146
|
-
0.3.0
|
147
|
-
* Now ability to force change a country_code.
|
148
|
-
See: https://github.com/joost/phony_rails/pull/23#issuecomment-17480463
|
149
|
-
|
150
|
-
0.2.1
|
151
|
-
* Better error handling by @k4nar.
|
152
|
-
|
153
|
-
0.1.12
|
154
|
-
* Further loosened gemspec dependencies.
|
155
|
-
|
156
|
-
0.1.11
|
157
|
-
* Better gemspec dependency versions by @rjhaveri.
|
158
|
-
|
159
|
-
0.1.10
|
160
|
-
* Changes from henning-koch.
|
161
|
-
* Some pending fixes.
|
162
|
-
|
163
|
-
0.1.8
|
164
|
-
* Improved validation methods by @ddidier.
|
165
|
-
|
166
|
-
0.1.6
|
167
|
-
* Added :as option to phony_normalize.
|
168
|
-
|
169
|
-
0.1.5
|
170
|
-
* some tests and a helper method by @ddidier.
|
171
|
-
|
172
|
-
0.1.2
|
173
|
-
* Using countries gem as suggested by @brutuscat.
|
174
|
-
* Fixes bugs mentioned by @ddidier.
|
175
|
-
|
176
|
-
0.1.0
|
177
|
-
* Added specs.
|
178
|
-
|
179
|
-
0.0.10
|
180
|
-
* Same fix as 0.0.9 but for phony_formatted method.
|
181
|
-
|
182
|
-
0.0.9
|
183
|
-
* Fix for using same options Hash for all models.
|
184
|
-
|
185
|
-
0.0.8
|
186
|
-
* Improved number cleaning not to remove '+' and leading 0's. Now works with national numbers starting with 0 followed by country_code. Eg. 032 in BE.
|
187
|
-
|
188
|
-
0.0.7
|
189
|
-
* Fixed problem with '+' number
|
190
|
-
|
191
|
-
0.0.6
|
192
|
-
* Fixed problem with '070-4157134' being parsed as US number
|
141
|
+
0.8.2
|
142
|
+
* Added String#phony_normalized method
|
193
143
|
|
194
144
|
## TODO
|
195
145
|
|
196
|
-
* Fix phony v2.x issues.
|
197
146
|
* Make this work: Home.find_by_normalized_phone_number(Home.normalize_number(params[:phone_number]))
|
198
147
|
So we use Home.normalize_number instead of PhonyRails.normalize_number. This way we can use the same default_country_code.
|
199
148
|
* Make country_code method configurable.
|
@@ -1,5 +1,14 @@
|
|
1
1
|
class String
|
2
2
|
|
3
|
+
# Usage:
|
4
|
+
# "+31 (0)30 1234 123".phony_normalized # => '31301234123'
|
5
|
+
# "(0)30 1234 123".phony_normalized # => '301234123'
|
6
|
+
# "(0)30 1234 123".phony_normalized(country_code: 'NL') # => '301234123'
|
7
|
+
def phony_normalized(options = {})
|
8
|
+
normalize_country_code = options.delete(:country_code)
|
9
|
+
PhonyRails.normalize_number(self, :default_country_code => normalize_country_code.to_s)
|
10
|
+
end
|
11
|
+
|
3
12
|
# Add a method to the String class so we can easily format phone numbers.
|
4
13
|
# This enables:
|
5
14
|
# "31612341234".phony_formatted # => '06 12341234'
|
data/lib/phony_rails/version.rb
CHANGED
@@ -7,37 +7,72 @@ describe PhonyRails do
|
|
7
7
|
|
8
8
|
describe 'phony_format String extension' do
|
9
9
|
|
10
|
-
describe 'the
|
10
|
+
describe 'the phony_formatted method' do
|
11
|
+
|
12
|
+
describe 'with the bang!' do
|
13
|
+
|
14
|
+
it "should change the String using the bang method" do
|
15
|
+
s = "0101234123"
|
16
|
+
s.phony_formatted!(:normalize => :NL, :format => :international).should eql('+31 10 123 4123')
|
17
|
+
s.should eql("+31 10 123 4123")
|
18
|
+
end
|
11
19
|
|
12
|
-
it "should change the String using the bang method" do
|
13
|
-
s = "0101234123"
|
14
|
-
s.phony_formatted!(:normalize => :NL, :format => :international).should eql('+31 10 123 4123')
|
15
|
-
s.should eql("+31 10 123 4123")
|
16
20
|
end
|
17
21
|
|
18
|
-
|
22
|
+
describe 'with strict option' do
|
23
|
+
|
24
|
+
it "should return nil with non plausible number" do
|
25
|
+
number = '+319090' # not valid
|
26
|
+
Phony.plausible?(number).should be_false
|
27
|
+
number.phony_formatted(:strict => true).should eql(nil)
|
28
|
+
end
|
19
29
|
|
20
|
-
|
30
|
+
it "should not return nil with plausible number" do
|
31
|
+
number = '+31101234123' # valid
|
32
|
+
Phony.plausible?(number).should be_true
|
33
|
+
number.phony_formatted(:strict => true).should_not eql(nil)
|
34
|
+
end
|
21
35
|
|
22
|
-
it "should return nil with non plausible number" do
|
23
|
-
number = '+319090' # not valid
|
24
|
-
Phony.plausible?(number).should be_false
|
25
|
-
number.phony_formatted(:strict => true).should eql(nil)
|
26
36
|
end
|
27
37
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
38
|
+
describe 'with normalize option' do
|
39
|
+
|
40
|
+
it "should phony_format" do
|
41
|
+
"101234123".phony_formatted(:normalize => :NL).should eql('010 123 4123')
|
42
|
+
"101234123".phony_formatted(:normalize => :NL, :format => :international).should eql('+31 10 123 4123')
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not change original String" do
|
46
|
+
s = "0101234123"
|
47
|
+
s.phony_formatted(:normalize => :NL).should eql('010 123 4123')
|
48
|
+
s.should eql("0101234123")
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should phony_format String with country code" do
|
52
|
+
"31101234123".phony_formatted(:normalize => :NL).should eql('010 123 4123')
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should phony_format String with country code" do
|
56
|
+
"31101234123".phony_formatted(:normalize => :NL).should eql('010 123 4123')
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should accept strings with non-digits in it" do
|
60
|
+
"+31-10-1234123".phony_formatted(:normalize => :NL, :format => :international, :spaces => '-').should eql('+31-10-123-4123')
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should phony_format String with country code different than normalized value" do
|
64
|
+
"+4790909090".phony_formatted(:normalize => :SE, :format => :international).should eql('+47 909 09 090')
|
65
|
+
end
|
66
|
+
|
32
67
|
end
|
33
68
|
|
34
|
-
|
69
|
+
describe "specific tests from issues" do
|
35
70
|
|
36
|
-
|
71
|
+
# https://github.com/joost/phony_rails/issues/42
|
72
|
+
it "should pass issue Github issue #42" do
|
73
|
+
PhonyRails.normalize_number("0606060606", default_country_code: 'FR').should eq('33606060606')
|
74
|
+
end
|
37
75
|
|
38
|
-
it "should phony_format" do
|
39
|
-
"101234123".phony_formatted(:normalize => :NL).should eql('010 123 4123')
|
40
|
-
"101234123".phony_formatted(:normalize => :NL, :format => :international).should eql('+31 10 123 4123')
|
41
76
|
end
|
42
77
|
|
43
78
|
it "should not change original String" do
|
@@ -46,54 +81,46 @@ describe PhonyRails do
|
|
46
81
|
s.should eql("0101234123")
|
47
82
|
end
|
48
83
|
|
49
|
-
it "should phony_format
|
50
|
-
"
|
84
|
+
it "should phony_format a digits string with spaces String" do
|
85
|
+
"31 10 1234123".phony_formatted(:format => :international, :spaces => '-').should eql('+31-10-123-4123')
|
51
86
|
end
|
52
87
|
|
53
|
-
it "should phony_format
|
54
|
-
"31101234123".phony_formatted(:
|
88
|
+
it "should phony_format a digits String" do
|
89
|
+
"31101234123".phony_formatted(:format => :international, :spaces => '-').should eql('+31-10-123-4123')
|
55
90
|
end
|
56
91
|
|
57
|
-
it "
|
58
|
-
"
|
92
|
+
it "returns nil if implausible phone" do
|
93
|
+
"this is not a phone".phony_formatted.should be_nil
|
59
94
|
end
|
60
95
|
|
61
|
-
it "
|
62
|
-
"
|
96
|
+
it "returns nil on blank string" do
|
97
|
+
"".phony_formatted.should be_nil
|
63
98
|
end
|
64
99
|
|
65
100
|
end
|
66
101
|
|
67
|
-
describe
|
102
|
+
describe 'the phony_normalized method' do
|
68
103
|
|
69
|
-
|
70
|
-
|
71
|
-
PhonyRails.normalize_number("0606060606", default_country_code: 'FR').should eq('33606060606')
|
104
|
+
it "returns blank on blank string" do
|
105
|
+
"".phony_normalized.should be_nil
|
72
106
|
end
|
73
107
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
s.phony_formatted(:normalize => :NL).should eql('010 123 4123')
|
79
|
-
s.should eql("0101234123")
|
80
|
-
end
|
108
|
+
context "when String misses a country_code" do
|
109
|
+
it "should normalize with :country_code option" do
|
110
|
+
"010 1231234".phony_normalized(:country_code => :NL).should eql("31101231234")
|
111
|
+
end
|
81
112
|
|
82
|
-
|
83
|
-
|
84
|
-
|
113
|
+
it "should normalize without :country_code option" do
|
114
|
+
"010 1231234".phony_normalized.should eql("101231234")
|
115
|
+
end
|
116
|
+
end
|
85
117
|
|
86
|
-
|
87
|
-
|
88
|
-
|
118
|
+
it "should normalize a String" do
|
119
|
+
"+31 (0)10 1231234".phony_normalized.should eql("31101231234")
|
120
|
+
end
|
89
121
|
|
90
|
-
it "returns nil if implausible phone" do
|
91
|
-
"this is not a phone".phony_formatted.should be_nil
|
92
122
|
end
|
93
123
|
|
94
|
-
it "returns nil on blank string" do
|
95
|
-
"".phony_formatted.should be_nil
|
96
|
-
end
|
97
124
|
end
|
98
125
|
|
99
126
|
describe 'PhonyRails#normalize_number' do
|
@@ -203,7 +230,7 @@ describe PhonyRails do
|
|
203
230
|
it "should accept :as option with single existing attribute name" do
|
204
231
|
lambda {
|
205
232
|
model_klass.phony_normalize(:phone_number, :as => 'phone_number_as_normalized')
|
206
|
-
}.should_not raise_error
|
233
|
+
}.should_not raise_error
|
207
234
|
end
|
208
235
|
|
209
236
|
it "should accept a non existing attribute name" do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phony_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.2
|
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: 2014-
|
12
|
+
date: 2014-12-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: phony
|