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 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.11.0'
13
- gem 'guard', '~> 1.2.0'
14
- gem 'guard-bundler', '~> 1.0.0'
15
- gem 'guard-rspec', '~> 1.2.0'
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- phony_rails (0.8.1)
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.6)
13
- activesupport (= 4.1.6)
12
+ activemodel (4.1.8)
13
+ activesupport (= 4.1.8)
14
14
  builder (~> 3.1)
15
- activerecord (4.1.6)
16
- activemodel (= 4.1.6)
17
- activesupport (= 4.1.6)
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.6)
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
- connection_pool (2.0.0)
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.1.3)
35
- ffi (1.9.5)
35
+ diff-lcs (1.2.5)
36
+ ffi (1.9.6)
37
+ formatador (0.2.5)
36
38
  growl (1.0.3)
37
- guard (1.2.3)
38
- listen (>= 0.4.2)
39
- thor (>= 0.14.6)
40
- guard-bundler (1.0.0)
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 (~> 1.1)
43
- guard-rspec (1.2.1)
44
- guard (>= 1.1)
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.7.11)
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
- minitest (5.4.2)
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.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.3)
65
- rake (10.3.2)
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.11.0)
70
- rspec-core (~> 2.11.0)
71
- rspec-expectations (~> 2.11.0)
72
- rspec-mocks (~> 2.11.0)
73
- rspec-core (2.11.1)
74
- rspec-expectations (2.11.3)
75
- diff-lcs (~> 1.1.3)
76
- rspec-mocks (2.11.3)
77
- sqlite3 (1.3.9)
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 (~> 1.2.0)
93
- guard-bundler (~> 1.0.0)
94
- guard-rspec (~> 1.2.0)
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.11.0)
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', :version => 2 do
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.7.0
130
- * Gem update.
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'
@@ -1,3 +1,3 @@
1
1
  module PhonyRails
2
- VERSION = "0.8.1"
2
+ VERSION = "0.8.2"
3
3
  end
@@ -7,37 +7,72 @@ describe PhonyRails do
7
7
 
8
8
  describe 'phony_format String extension' do
9
9
 
10
- describe 'the bang method phony_formatted!' do
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
- end
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
- describe 'with strict option' do
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
- it "should not return nil with plausible number" do
29
- number = '+31101234123' # valid
30
- Phony.plausible?(number).should be_true
31
- number.phony_formatted(:strict => true).should_not eql(nil)
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
- end
69
+ describe "specific tests from issues" do
35
70
 
36
- describe 'with normalize option' do
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 String with country code" do
50
- "31101234123".phony_formatted(:normalize => :NL).should eql('010 123 4123')
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 String with country code" do
54
- "31101234123".phony_formatted(:normalize => :NL).should eql('010 123 4123')
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 "should accept strings with non-digits in it" do
58
- "+31-10-1234123".phony_formatted(:normalize => :NL, :format => :international, :spaces => '-').should eql('+31-10-123-4123')
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 "should phony_format String with country code different than normalized value" do
62
- "+4790909090".phony_formatted(:normalize => :SE, :format => :international).should eql('+47 909 09 090')
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 "specific tests from issues" do
102
+ describe 'the phony_normalized method' do
68
103
 
69
- # https://github.com/joost/phony_rails/issues/42
70
- it "should pass issue Github issue #42" do
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
- end
75
-
76
- it "should not change original String" do
77
- s = "0101234123"
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
- it "should phony_format a digits string with spaces String" do
83
- "31 10 1234123".phony_formatted(:format => :international, :spaces => '-').should eql('+31-10-123-4123')
84
- end
113
+ it "should normalize without :country_code option" do
114
+ "010 1231234".phony_normalized.should eql("101231234")
115
+ end
116
+ end
85
117
 
86
- it "should phony_format a digits String" do
87
- "31101234123".phony_formatted(:format => :international, :spaces => '-').should eql('+31-10-123-4123')
88
- end
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(ArgumentError)
233
+ }.should_not raise_error
207
234
  end
208
235
 
209
236
  it "should accept a non existing attribute name" do
@@ -48,6 +48,8 @@ end
48
48
  class MongoidDummy < MongoidModel
49
49
  end
50
50
 
51
+ I18n.config.enforce_available_locales = true
52
+
51
53
  RSpec.configure do |config|
52
54
  # some (optional) config here
53
55
  end
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.1
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-11-26 00:00:00.000000000 Z
12
+ date: 2014-12-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: phony