global_phone 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +69 -41
- data/lib/global_phone.rb +1 -1
- data/lib/global_phone/format.rb +3 -3
- data/lib/global_phone/number.rb +15 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 22b1542e445e1f158ffaeb0115d0cdc5de251246
|
4
|
+
data.tar.gz: 69f61d4a5d562aeb508c75090d3cbb8af057da66
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80b970b279a2c53777b6e8dc58c18eff30e0a0e587f045b39587fe33383de8d39858b803951ff4c6b55dace57dcec1841506a9fcbb5572e36b611091b5c162a8
|
7
|
+
data.tar.gz: 29732d6e2760cbf943425355a304bdd45d28fd93cbc5ec2bb042d97104272de98eb4aaa7a96a7dca730605cd80c037981b79df0c932d207ba72546d81287de2d
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
GlobalPhone parses, validates, and formats local and international phone numbers according to the [E.164 standard](http://en.wikipedia.org/wiki/E.164).
|
4
4
|
|
5
|
-
**Store and display phone numbers in your app.** Accept phone number input in national or international format. Convert phone numbers to international
|
5
|
+
**Store and display phone numbers in your app.** Accept phone number input in national or international format. Convert phone numbers to international strings (`+13125551212`) for storage and retrieval. Present numbers in national format (`(312) 555-1212`) in your UI.
|
6
6
|
|
7
7
|
**Designed with the future in mind.** GlobalPhone uses format specifications from Google's open-source [libphonenumber](http://code.google.com/p/libphonenumber/) database. No need to upgrade the library when a new phone format is introduced—just generate a new copy of the database and check it into your app.
|
8
8
|
|
@@ -22,87 +22,109 @@ GlobalPhone parses, validates, and formats local and international phone numbers
|
|
22
22
|
|
23
23
|
3. Tell GlobalPhone where to find the database. For example, in a Rails app, create an initializer in `config/initializers/global_phone.rb`:
|
24
24
|
|
25
|
-
|
26
|
-
|
25
|
+
```ruby
|
26
|
+
require 'global_phone'
|
27
|
+
GlobalPhone.db_path = Rails.root.join('db/global_phone.json')
|
28
|
+
```
|
27
29
|
|
28
30
|
## Examples
|
29
31
|
|
30
32
|
Parse an international number string into a `GlobalPhone::Number` object:
|
31
33
|
|
32
|
-
|
33
|
-
|
34
|
+
```ruby
|
35
|
+
number = GlobalPhone.parse('+1-312-555-1212')
|
36
|
+
# => #<GlobalPhone::Number territory=#<GlobalPhone::Territory country_code=1 name=US> national_string="3125551212">
|
37
|
+
```
|
34
38
|
|
35
39
|
Query the country code and likely territory name of the number:
|
36
40
|
|
37
|
-
|
38
|
-
|
41
|
+
```ruby
|
42
|
+
number.country_code
|
43
|
+
# => "1"
|
39
44
|
|
40
|
-
|
41
|
-
|
45
|
+
number.territory.name
|
46
|
+
# => "US"
|
47
|
+
```
|
42
48
|
|
43
49
|
Present the number in national and international formats:
|
44
50
|
|
45
|
-
|
46
|
-
|
51
|
+
```ruby
|
52
|
+
number.national_format
|
53
|
+
# => "(312) 555-1212"
|
47
54
|
|
48
|
-
|
49
|
-
|
55
|
+
number.international_format
|
56
|
+
# => "+1 312-555-1212"
|
57
|
+
```
|
50
58
|
|
51
59
|
Is the number valid? (Note: this is not definitive. For example, the number here is "valid" by format, but there are no US numbers that start with 555. The `valid?` method may return false positives, but *should not* return false negatives unless the database is out of date.)
|
52
60
|
|
53
|
-
|
54
|
-
|
61
|
+
```ruby
|
62
|
+
number.valid?
|
63
|
+
# => true
|
64
|
+
```
|
55
65
|
|
56
66
|
Get the number's normalized E.164 international string:
|
57
67
|
|
58
|
-
|
59
|
-
|
68
|
+
```ruby
|
69
|
+
number.international_string
|
70
|
+
# => "+13125551212"
|
71
|
+
```
|
60
72
|
|
61
73
|
Parse a number in national format for a given territory:
|
62
74
|
|
63
|
-
|
64
|
-
|
75
|
+
```ruby
|
76
|
+
number = GlobalPhone.parse("(0) 20-7031-3000", :gb)
|
77
|
+
# => #<GlobalPhone::Number territory=#<GlobalPhone::Territory country_code=44 name=GB> national_string="2070313000">
|
78
|
+
```
|
65
79
|
|
66
80
|
Parse an international number using a territory's international dialing prefix:
|
67
81
|
|
68
|
-
|
69
|
-
|
82
|
+
```ruby
|
83
|
+
number = GlobalPhone.parse("00 1 3125551212", :gb)
|
84
|
+
# => #<GlobalPhone::Number territory=#<GlobalPhone::Territory country_code=1 name=US> national_string="3125551212">
|
85
|
+
```
|
70
86
|
|
71
87
|
Set the default territory to Great Britain (territory names are [ISO 3166-1 Alpha-2](http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2) codes):
|
72
88
|
|
73
|
-
|
74
|
-
|
89
|
+
```ruby
|
90
|
+
GlobalPhone.default_territory_name = :gb
|
91
|
+
# => :gb
|
75
92
|
|
76
|
-
|
77
|
-
|
93
|
+
GlobalPhone.parse("(0) 20-7031-3000")
|
94
|
+
# => #<GlobalPhone::Number territory=#<GlobalPhone::Territory country_code=44 name=GB> national_string="2070313000">
|
95
|
+
```
|
78
96
|
|
79
97
|
Shortcuts for validating a phone number:
|
80
98
|
|
81
|
-
|
82
|
-
|
99
|
+
```ruby
|
100
|
+
GlobalPhone.validate("+1 312-555-1212")
|
101
|
+
# => true
|
83
102
|
|
84
|
-
|
85
|
-
|
103
|
+
GlobalPhone.validate("+442070313000")
|
104
|
+
# => true
|
86
105
|
|
87
|
-
|
88
|
-
|
106
|
+
GlobalPhone.validate("(0) 20-7031-3000")
|
107
|
+
# => false
|
89
108
|
|
90
|
-
|
91
|
-
|
109
|
+
GlobalPhone.validate("(0) 20-7031-3000", :gb)
|
110
|
+
# => true
|
111
|
+
```
|
92
112
|
|
93
113
|
Shortcuts for normalizing a phone number in E.164 format:
|
94
114
|
|
95
|
-
|
96
|
-
|
115
|
+
```ruby
|
116
|
+
GlobalPhone.normalize("(312) 555-1212")
|
117
|
+
# => "+13125551212"
|
97
118
|
|
98
|
-
|
99
|
-
|
119
|
+
GlobalPhone.normalize("+442070313000")
|
120
|
+
# => "+442070313000"
|
100
121
|
|
101
|
-
|
102
|
-
|
122
|
+
GlobalPhone.normalize("(0) 20-7031-3000")
|
123
|
+
# => nil
|
103
124
|
|
104
|
-
|
105
|
-
|
125
|
+
GlobalPhone.normalize("(0) 20-7031-3000", :gb)
|
126
|
+
# => "+442070313000"
|
127
|
+
```
|
106
128
|
|
107
129
|
## Caveats
|
108
130
|
|
@@ -124,6 +146,12 @@ GlobalPhone is heavily inspired by Andreas Gal's [PhoneNumber.js](https://github
|
|
124
146
|
|
125
147
|
### Version History
|
126
148
|
|
149
|
+
**1.0.1** (May 29, 2013)
|
150
|
+
|
151
|
+
* GlobalPhone::Number#to_s returns the E.164 international string.
|
152
|
+
* Ensure GlobalPhone::Number always returns strings for #national_format, #international_format, and #international_string, regardless of validity.
|
153
|
+
* Relax format restrictions to more loosely match available national number patterns.
|
154
|
+
|
127
155
|
**1.0.0** (May 28, 2013)
|
128
156
|
|
129
157
|
* Initial public release.
|
data/lib/global_phone.rb
CHANGED
data/lib/global_phone/format.rb
CHANGED
@@ -8,14 +8,14 @@ module GlobalPhone
|
|
8
8
|
field 3, :national_prefix_formatting_rule
|
9
9
|
field 4, :international_format_rule, :fallback => :national_format_rule
|
10
10
|
|
11
|
-
def match(national_string)
|
12
|
-
return false if leading_digits && national_string !~ leading_digits
|
11
|
+
def match(national_string, match_leading_digits = true)
|
12
|
+
return false if match_leading_digits && leading_digits && national_string !~ leading_digits
|
13
13
|
national_string =~ pattern
|
14
14
|
end
|
15
15
|
|
16
16
|
def format_replacement_string(type)
|
17
17
|
format_rule = send(:"#{type}_format_rule")
|
18
|
-
format_rule.gsub("$", "\\")
|
18
|
+
format_rule.to_s.gsub("$", "\\") unless format_rule == "NA"
|
19
19
|
end
|
20
20
|
|
21
21
|
def apply(national_string, type)
|
data/lib/global_phone/number.rb
CHANGED
@@ -33,22 +33,22 @@ module GlobalPhone
|
|
33
33
|
@national_format ||= begin
|
34
34
|
if format && result = format.apply(national_string, :national)
|
35
35
|
apply_national_prefix_format(result)
|
36
|
+
else
|
37
|
+
national_string
|
36
38
|
end
|
37
39
|
end
|
38
40
|
end
|
39
41
|
|
40
42
|
def international_string
|
41
|
-
@international_string ||=
|
42
|
-
if international_format
|
43
|
-
international_format.gsub(NON_DIALABLE_CHARS, "")
|
44
|
-
end
|
45
|
-
end
|
43
|
+
@international_string ||= international_format.gsub(NON_DIALABLE_CHARS, "")
|
46
44
|
end
|
47
45
|
|
48
46
|
def international_format
|
49
47
|
@international_format ||= begin
|
50
48
|
if format && formatted_number = format.apply(national_string, :international)
|
51
49
|
"+#{country_code} #{formatted_number}"
|
50
|
+
else
|
51
|
+
"+#{country_code} #{national_string}"
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
@@ -61,9 +61,18 @@ module GlobalPhone
|
|
61
61
|
"#<#{self.class.name} territory=#{territory.inspect} national_string=#{national_string.inspect}>"
|
62
62
|
end
|
63
63
|
|
64
|
+
def to_s
|
65
|
+
international_string
|
66
|
+
end
|
67
|
+
|
64
68
|
protected
|
65
69
|
def format
|
66
|
-
@format ||=
|
70
|
+
@format ||= find_format_for(national_string)
|
71
|
+
end
|
72
|
+
|
73
|
+
def find_format_for(string)
|
74
|
+
region.formats.detect { |format| format.match(string) } ||
|
75
|
+
region.formats.detect { |format| format.match(string, false) }
|
67
76
|
end
|
68
77
|
|
69
78
|
def apply_national_prefix_format(result)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: global_phone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Stephenson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: GlobalPhone parses, validates, and formats local and international phone
|
14
14
|
numbers according to the E.164 standard using the rules specified in Google's libphonenumber
|