phonie 3.0.0 → 3.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -1
- data/Changelog.md +5 -0
- data/Readme.rdoc +14 -14
- data/lib/phonie/configuration.rb +10 -1
- data/lib/phonie/formatter.rb +13 -7
- data/lib/phonie/version.rb +1 -1
- data/test/phone_test.rb +6 -0
- metadata +4 -4
data/.travis.yml
CHANGED
data/Changelog.md
CHANGED
data/Readme.rdoc
CHANGED
@@ -33,6 +33,7 @@ Alternatively Phonie can be configured via a block
|
|
33
33
|
Phonie.configure do |config|
|
34
34
|
config.default_country_code = '385'
|
35
35
|
config.default_area_code = '47'
|
36
|
+
config.add_custom_named_format :short, '%A/%n1-%n2'
|
36
37
|
end
|
37
38
|
|
38
39
|
Phonie::Phone.parse '451-588'
|
@@ -64,31 +65,30 @@ When given a string, it interpolates the string with the following fields:
|
|
64
65
|
pn.format("%A/%f-%l") # => "091/512-5486"
|
65
66
|
pn.format("+ %c (%a) %n") # => "+ 385 (91) 5125486"
|
66
67
|
|
67
|
-
When given a symbol it is used as a lookup for the format in
|
68
|
+
When given a symbol it is used as a lookup for the format in <tt>Phonie::Formatter</tt>.
|
68
69
|
pn.format(:europe) # => "+385 (0) 91 512 5486"
|
69
|
-
pn.format(:us) # => "(234) 123 4567"
|
70
|
+
pn.format(:us) # => "(234) 123 4567"
|
70
71
|
pn.format(:default_with_extension) # => "+3851234567x143"
|
71
72
|
|
72
73
|
You can add your own custom named formats like so:
|
73
|
-
Phonie
|
74
|
+
Phonie.configuration.add_custom_named_format :short, '%A/%n1-%n2'
|
74
75
|
pn.format(:short) # => 091/512-5486
|
75
76
|
|
76
77
|
== ActiveModel validator
|
77
78
|
|
78
79
|
Phonie includes an ActiveModel validator. If you are using ActiveModel you can validate phone numbers like so:
|
79
80
|
|
80
|
-
|
81
|
-
|
81
|
+
class SomeModel
|
82
|
+
include ActiveModel::Validations
|
82
83
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
model = SomeModel.new(phone_number: '')
|
87
|
-
model.valid? # false
|
84
|
+
validates :phone_number, phone: true
|
85
|
+
end
|
88
86
|
|
89
|
-
|
90
|
-
|
87
|
+
model = SomeModel.new(phone_number: '')
|
88
|
+
model.valid? # false
|
91
89
|
|
90
|
+
model = SomeModel.new(phone_number: '+1 251 123 4567')
|
91
|
+
model.valid? # true
|
92
92
|
|
93
93
|
= TODO
|
94
94
|
Add definitions for more countries
|
@@ -96,7 +96,7 @@ Add definitions for more countries
|
|
96
96
|
Currently tested on:
|
97
97
|
[AE] UAE
|
98
98
|
[AF] Afghanistan
|
99
|
-
[AL] Albania
|
99
|
+
[AL] Albania
|
100
100
|
[AM] Armenia
|
101
101
|
[AR] Argentina
|
102
102
|
[AT] Austria
|
@@ -187,7 +187,7 @@ Currently tested on:
|
|
187
187
|
= How you can contribute
|
188
188
|
More testing is needed to add support for missing countries, and improve support for tested countries. In many cases only minimal testing is done on area codes, local number formats and number length where more exact matching is possible.
|
189
189
|
|
190
|
-
The best places to start is to read through the country tests and data/phone_countries.rb
|
190
|
+
The best places to start is to read through the country tests and data/phone_countries.rb
|
191
191
|
|
192
192
|
= Other libraries
|
193
193
|
This is based off a fork of the Phone gem (https://github.com/carr/phone), and was extensively modified for better support of country detection, and supports far more countries.
|
data/lib/phonie/configuration.rb
CHANGED
@@ -5,10 +5,19 @@ module Phonie
|
|
5
5
|
include Singleton
|
6
6
|
|
7
7
|
attr_accessor :data_file_path, :default_area_code, :default_country_code, :n1_length
|
8
|
-
|
8
|
+
|
9
9
|
def initialize
|
10
10
|
@data_file_path = File.join(File.dirname(__FILE__), 'data', 'phone_countries.yml')
|
11
11
|
@n1_length = 3
|
12
|
+
@named_formats = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_custom_named_format(name, format_string)
|
16
|
+
@named_formats[name.to_sym] = format_string
|
17
|
+
end
|
18
|
+
|
19
|
+
def custom_named_formats
|
20
|
+
@named_formats
|
12
21
|
end
|
13
22
|
end
|
14
23
|
|
data/lib/phonie/formatter.rb
CHANGED
@@ -18,7 +18,7 @@ module Phonie
|
|
18
18
|
|
19
19
|
def initialize(params)
|
20
20
|
@phone_number = params[:phone_number]
|
21
|
-
|
21
|
+
|
22
22
|
format = params[:format]
|
23
23
|
@format = if format.respond_to?(:gsub)
|
24
24
|
format
|
@@ -31,12 +31,7 @@ module Phonie
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def self.named_formats
|
34
|
-
|
35
|
-
default: "+%c%a%n",
|
36
|
-
default_with_extension: "+%c%a%nx%x",
|
37
|
-
europe: '+%c (0) %a %f %l',
|
38
|
-
us: "(%a) %f-%l"
|
39
|
-
}
|
34
|
+
default_named_formats.merge(Phonie.configuration.custom_named_formats)
|
40
35
|
end
|
41
36
|
|
42
37
|
def to_s
|
@@ -50,5 +45,16 @@ module Phonie
|
|
50
45
|
gsub("%l", pn.number2.to_s).
|
51
46
|
gsub("%x", pn.extension.to_s)
|
52
47
|
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def self.default_named_formats
|
52
|
+
{
|
53
|
+
default: "+%c%a%n",
|
54
|
+
default_with_extension: "+%c%a%nx%x",
|
55
|
+
europe: '+%c (0) %a %f %l',
|
56
|
+
us: "(%a) %f-%l"
|
57
|
+
}
|
58
|
+
end
|
53
59
|
end
|
54
60
|
end
|
data/lib/phonie/version.rb
CHANGED
data/test/phone_test.rb
CHANGED
@@ -96,6 +96,12 @@ class PhoneTest < Phonie::TestCase
|
|
96
96
|
assert_equal '+385 (0) 91 512 5486', pn.format(:europe)
|
97
97
|
end
|
98
98
|
|
99
|
+
def test_format_with_custom_symbol_specifier
|
100
|
+
Phonie.configuration.add_custom_named_format :internal, 'ext.%x'
|
101
|
+
pn = Phonie::Phone.new '5125486', '91', '385', '1234'
|
102
|
+
assert_equal 'ext.1234', pn.format(:internal)
|
103
|
+
end
|
104
|
+
|
99
105
|
def test_valid
|
100
106
|
assert Phonie::Phone.valid?('915125486', :country_code => '385')
|
101
107
|
assert Phonie::Phone.valid?('385915125486')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phonie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ authors:
|
|
13
13
|
autorequire:
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
|
-
date: 2014-
|
16
|
+
date: 2014-03-10 00:00:00.000000000 Z
|
17
17
|
dependencies:
|
18
18
|
- !ruby/object:Gem::Dependency
|
19
19
|
name: activemodel
|
@@ -217,7 +217,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
217
217
|
version: '0'
|
218
218
|
segments:
|
219
219
|
- 0
|
220
|
-
hash:
|
220
|
+
hash: -3226221258435617191
|
221
221
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
222
222
|
none: false
|
223
223
|
requirements:
|
@@ -226,7 +226,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
226
226
|
version: '0'
|
227
227
|
segments:
|
228
228
|
- 0
|
229
|
-
hash:
|
229
|
+
hash: -3226221258435617191
|
230
230
|
requirements: []
|
231
231
|
rubyforge_project:
|
232
232
|
rubygems_version: 1.8.23
|