phony_rails 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Gemfile +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +8 -1
- data/lib/phony_rails.rb +5 -2
- data/lib/phony_rails/version.rb +1 -1
- data/spec/lib/phony_rails_spec.rb +29 -10
- metadata +2 -2
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -44,11 +44,14 @@ PhonyRails will also check your model for a country_code method to use when norm
|
|
44
44
|
You can also do-it-yourself and call:
|
45
45
|
|
46
46
|
# Options:
|
47
|
-
# :country_code => The country code we should use.
|
47
|
+
# :country_code => The country code we should use (forced).
|
48
48
|
# :default_country_code => Some fallback code (eg. 'NL') that can be used as default (comes from phony_normalize_numbers method).
|
49
49
|
|
50
50
|
PhonyRails.normalize_number('some number', :country_code => 'NL')
|
51
51
|
|
52
|
+
PhonyRails.normalize_number('+4790909090', :country_code => 'SE') # => '464790909090' (forced to +46)
|
53
|
+
PhonyRails.normalize_number('+4790909090', :default_country_code => 'SE') # => '4790909090' (still +47 so not changed)
|
54
|
+
|
52
55
|
### Validation
|
53
56
|
|
54
57
|
In your model use the Phony.plausible method to validate an attribute:
|
@@ -98,6 +101,10 @@ Say you want to find a record by a phone number. Best is to normalize user input
|
|
98
101
|
|
99
102
|
## Changelog
|
100
103
|
|
104
|
+
0.3.0
|
105
|
+
* Now ability to force change a country_code.
|
106
|
+
See: https://github.com/joost/phony_rails/pull/23#issuecomment-17480463
|
107
|
+
|
101
108
|
0.2.1
|
102
109
|
* Better error handling by @k4nar
|
103
110
|
|
data/lib/phony_rails.rb
CHANGED
@@ -21,9 +21,12 @@ module PhonyRails
|
|
21
21
|
number = number.clone # Just to be sure, we don't want to change the original.
|
22
22
|
number.gsub!(/[^\d\+]/, '') # Strips weird stuff from the number
|
23
23
|
return if number.blank?
|
24
|
-
if country_number = country_number_for(options[:country_code]
|
25
|
-
#
|
24
|
+
if country_number = country_number_for(options[:country_code])
|
25
|
+
# (Force) add country_number if missing
|
26
26
|
number = "#{country_number}#{number}" if not number =~ /^(00|\+)?#{country_number}/
|
27
|
+
elsif default_country_number = country_number_for(options[:default_country_code])
|
28
|
+
# Add default_country_number if missing
|
29
|
+
number = "#{default_country_number}#{number}" if not number =~ /^(00|\+)/
|
27
30
|
end
|
28
31
|
number = Phony.normalize(number) if Phony.plausible?(number)
|
29
32
|
return number.to_s
|
data/lib/phony_rails/version.rb
CHANGED
@@ -68,19 +68,38 @@ describe PhonyRails do
|
|
68
68
|
end
|
69
69
|
|
70
70
|
describe 'PhonyRails#normalize_number' do
|
71
|
-
|
72
|
-
|
73
|
-
|
71
|
+
context 'number with a country code' do
|
72
|
+
|
73
|
+
it "should not add default_country_code" do
|
74
|
+
PhonyRails.normalize_number('+4790909090', :default_country_code => 'SE').should eql('4790909090') # SE = +46
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should force add country_code" do
|
78
|
+
PhonyRails.normalize_number('+4790909090', :country_code => 'SE').should eql('464790909090')
|
79
|
+
end
|
74
80
|
|
75
|
-
it "should normalize a number with a country_code" do
|
76
|
-
PhonyRails.normalize_number('010-1234123', :country_code => 'NL', :default_country_code => 'DE').should eql('31101234123')
|
77
|
-
PhonyRails.normalize_number('010-1234123', :country_code => 'NL').should eql('31101234123')
|
78
81
|
end
|
79
82
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
83
|
+
context 'number without a country code' do
|
84
|
+
|
85
|
+
it "should normalize with a default_country_code" do
|
86
|
+
PhonyRails.normalize_number('010-1234123', :default_country_code => 'NL').should eql('31101234123')
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should normalize with a country_code" do
|
90
|
+
PhonyRails.normalize_number('010-1234123', :country_code => 'NL', :default_country_code => 'DE').should eql('31101234123')
|
91
|
+
PhonyRails.normalize_number('010-1234123', :country_code => 'NL').should eql('31101234123')
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should handle different countries" do
|
95
|
+
PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE').should eql('49308612906')
|
96
|
+
PhonyRails.normalize_number('0203 330 8897', :country_code => 'GB').should eql('442033308897')
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should prefer country_code over default_country_code" do
|
100
|
+
PhonyRails.normalize_number('(030) 8 61 29 06', :country_code => 'DE', :default_country_code => 'NL').should eql('49308612906')
|
101
|
+
end
|
102
|
+
|
84
103
|
end
|
85
104
|
|
86
105
|
it "should handle some edge cases" do
|
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.
|
4
|
+
version: 0.3.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: 2013-
|
12
|
+
date: 2013-05-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: phony
|