phone_parser 0.2.0 → 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.
- checksums.yaml +4 -4
- data/README.md +34 -15
- data/lib/phone_parser.rb +1 -23
- data/lib/phone_parser/country_codes.rb +45 -1
- data/lib/phone_parser/version.rb +1 -1
- 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: 87f79272eff9c337110401fe623934a2a60ce695
|
4
|
+
data.tar.gz: 892757c73bab635c270e52afe8a69a36d84ec450
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec8bf45930b7f85bf45df8d0ee3f8ba8108f18f842142a2cb8bf1bfd41546f75d970ec6334e3e5b3e7b29fcd4db2b2332609a5ec15d5dd8339d2697a6b9bd203
|
7
|
+
data.tar.gz: 7978a8508ba920b34709ab8e021eadfb3f535262ba7ca3e032b464b02ff7807631d8468d86cd71477a64be734da8a2db7168a426af176ef84f7882eb5b2f7e6d
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# PhoneParser
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
A phone parsing gem that takes in phone numbers and removes any formatting to ensure uniform phone numbers. It also includes a country code validation component.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,34 +20,55 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
To parse phone numbers without
|
23
|
+
To parse phone numbers without a country code, a default country code of `1` is assumed:
|
26
24
|
|
27
25
|
```ruby
|
28
|
-
PhoneParser.parse('555-555-5555')
|
29
|
-
PhoneParser.parse('5555555555')
|
30
|
-
PhoneParser.parse('(555) 555-5555') # => '
|
31
|
-
PhoneParser.parse('555.555.5555')
|
32
|
-
PhoneParser.parse('555 555.5555')
|
33
|
-
PhoneParser.parse('555 555.5555')
|
26
|
+
PhoneParser.parse('555-555-5555') # => '15555555555'
|
27
|
+
PhoneParser.parse('5555555555') # => '15555555555'
|
28
|
+
PhoneParser.parse('(555) 555-5555') # => '15555555555'
|
29
|
+
PhoneParser.parse('555.555.5555') # => '15555555555'
|
30
|
+
PhoneParser.parse('555 555.5555') # => '15555555555'
|
31
|
+
PhoneParser.parse('555 555.5555') # => '15555555555'
|
34
32
|
```
|
35
33
|
|
36
34
|
It will throw an error if an invalid phone number is passed to the parser:
|
37
35
|
|
38
36
|
```ruby
|
39
37
|
PhoneParser.parse('5555555') # => PhoneLengthError
|
40
|
-
PhoneParser.parse('')
|
38
|
+
PhoneParser.parse('') # => PhoneLengthError
|
41
39
|
```
|
42
40
|
|
43
41
|
If a country code is supplied, the parser will verify that the code is valid, it will throw a `CountryCodeError` if the country code isn't valid:
|
44
42
|
|
45
43
|
```ruby
|
46
|
-
PhoneParser.parse('+15555555555')
|
47
|
-
PhoneParser.parse('1-7845555555555')
|
44
|
+
PhoneParser.parse('+15555555555') # => '15555555555'
|
45
|
+
PhoneParser.parse('1-7845555555555') # => '17845555555555'
|
48
46
|
PhoneParser.parse('Country Code: 379, Phone: 555-555-5555') # => '3795555555555'
|
49
|
-
PhoneParser.parse('3795555555555')
|
50
|
-
PhoneParser.parse('999 555 555 5555')
|
47
|
+
PhoneParser.parse('3795555555555') # => '3795555555555'
|
48
|
+
PhoneParser.parse('999 555 555 5555') # => CountryCodeError
|
49
|
+
```
|
50
|
+
|
51
|
+
If you want to supply a different default country code, you can pass in an optional argument, this will not override country codes found in the provided number, it will only be used if a country code is not found.
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
PhoneParser.parse('5555555555', country_code: '379') # => '3795555555555'
|
55
|
+
```
|
56
|
+
|
57
|
+
You can also check to see if a phone number is from a specific country using it's country code abbreviation, as shown below:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
CountryCodes.va?('379 555-555-5555') # => true
|
61
|
+
CountryCodes.us?('379 555-555-5555') # => false
|
51
62
|
```
|
52
63
|
|
64
|
+
Alternatively, if you want to extract the country code, you can query it directly from the phone number:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
CountryCodes.country_code('379 555-555-5555') # => '379'
|
68
|
+
CountryCodes.country_code('1 555-555-5555') # => '1'
|
69
|
+
```
|
70
|
+
|
71
|
+
|
53
72
|
## Development
|
54
73
|
|
55
74
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/phone_parser.rb
CHANGED
@@ -2,32 +2,15 @@ require "phone_parser/version"
|
|
2
2
|
require "phone_parser/country_codes"
|
3
3
|
|
4
4
|
module PhoneParser
|
5
|
-
include CountryCodes
|
6
|
-
|
7
5
|
def self.parse(number, country_code: '1')
|
8
6
|
number.delete!("^0-9")
|
9
7
|
digit_length_validator(number)
|
10
|
-
country_code_validator(number, country_code)
|
8
|
+
CountryCodes.country_code_validator(number, country_code)
|
11
9
|
end
|
12
10
|
|
13
11
|
def self.digit_length_validator number
|
14
12
|
number.length < 10 ? (raise PhoneLengthError) : (number)
|
15
13
|
end
|
16
|
-
|
17
|
-
def self.country_code_validator number, country_code
|
18
|
-
if number.length == 10
|
19
|
-
country_code + number
|
20
|
-
else
|
21
|
-
extracted_number = number[-10..-1]
|
22
|
-
country_code = number.gsub(extracted_number, '')
|
23
|
-
|
24
|
-
country_codes.include?(country_code) ? (number) : (raise CountryCodeError)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.country_codes
|
29
|
-
CountryCodes.code_array.flatten.keep_if { |e| e =~ /\d+/ }
|
30
|
-
end
|
31
14
|
end
|
32
15
|
|
33
16
|
class PhoneLengthError < StandardError
|
@@ -36,8 +19,3 @@ class PhoneLengthError < StandardError
|
|
36
19
|
end
|
37
20
|
end
|
38
21
|
|
39
|
-
class CountryCodeError < StandardError
|
40
|
-
def message
|
41
|
-
'Country code not recognized'
|
42
|
-
end
|
43
|
-
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
class CountryCodes
|
2
2
|
def self.code_array
|
3
3
|
[
|
4
4
|
["93", "AF"],
|
@@ -246,4 +246,48 @@ module CountryCodes
|
|
246
246
|
["263", "ZW"]
|
247
247
|
]
|
248
248
|
end
|
249
|
+
|
250
|
+
def self.country_code_validator number, country_code
|
251
|
+
if number.length == 10
|
252
|
+
country_code + number
|
253
|
+
else
|
254
|
+
extracted_number = number[-10..-1]
|
255
|
+
country_code = number.gsub(extracted_number, '')
|
256
|
+
|
257
|
+
country_codes.include?(country_code) ? (number) : (raise CountryCodeError)
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
def self.country_codes
|
262
|
+
code_array.flatten.keep_if { |e| e =~ /\d+/ }
|
263
|
+
end
|
264
|
+
|
265
|
+
def self.country_code number
|
266
|
+
number.delete!("^0-9")
|
267
|
+
country_code_validator(number, nil)
|
268
|
+
extracted_number = number[-10..-1]
|
269
|
+
country_code = number.gsub(extracted_number, '')
|
270
|
+
end
|
271
|
+
|
272
|
+
def self.method_missing(method_name, *arguments, &block)
|
273
|
+
if method_name.to_s =~ /(.*)?/
|
274
|
+
if code_array.map(&:last).index(method_name.to_s[0..-2].upcase) == code_array.map(&:first).index(country_code(arguments.first))
|
275
|
+
true
|
276
|
+
else
|
277
|
+
false
|
278
|
+
end
|
279
|
+
else
|
280
|
+
super
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
def self.respond_to_missing?(method_name, include_private = false)
|
285
|
+
method_name.to_s.end_with?('?') || super
|
286
|
+
end
|
287
|
+
end
|
288
|
+
|
289
|
+
class CountryCodeError < StandardError
|
290
|
+
def message
|
291
|
+
'Country code not recognized'
|
292
|
+
end
|
249
293
|
end
|
data/lib/phone_parser/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phone_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jordan Hudgens
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|