phone_number 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/VERSION +1 -1
- data/lib/phone_number/number.rb +10 -8
- data/phone_number.gemspec +1 -1
- data/spec/phone_number/number_spec.rb +2 -2
- metadata +3 -3
data/README.rdoc
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
data/lib/phone_number/number.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
module PhoneNumber
|
2
2
|
class Number
|
3
|
-
|
4
|
-
|
3
|
+
REGEX = /^\+?(\d{0,2})[ \.\-]?\(?(\d{3})\)?[ \.\-]?(\d{3})[ \.\-]?(\d{4})[ x]?(\d*)$/
|
4
|
+
INVALID_MESSAGE = "invalid format, try something like: 111-111-1111"
|
5
5
|
attr_reader :country_code, :area_code, :subscriber_number_prefix, :subscriber_number_postfix, :extension
|
6
6
|
|
7
7
|
def initialize( number )
|
8
|
-
if number.is_a?( String ) && m = number.match(
|
8
|
+
if number.is_a?( String ) && m = number.match( REGEX )
|
9
9
|
@country_code, @area_code, @subscriber_number_prefix, @subscriber_number_postfix, @extension = m[1], m[2], m[3], m[4], m[5]
|
10
10
|
elsif number.is_a?( Hash )
|
11
11
|
@country_code, @area_code, @subscriber_number_prefix, @subscriber_number_postfix, @extension = number[:country_code], number[:area_code], number[:subscriber_number_prefix], number[:subscriber_number_postfix], number[:extension]
|
12
12
|
end
|
13
13
|
|
14
|
-
|
14
|
+
@pattern_map = {
|
15
15
|
/%c/ => (@country_code || '').gsub( /0/, '' ) || "",
|
16
16
|
/%C/ => @country_code || "",
|
17
17
|
/%a/ => @area_code || "",
|
@@ -31,12 +31,14 @@ module PhoneNumber
|
|
31
31
|
|
32
32
|
def self.parse( number )
|
33
33
|
number.gsub!( / x /, 'x' )
|
34
|
-
if m = number.match(
|
34
|
+
if m = number.match( REGEX )
|
35
35
|
cntry_cd = m[1].size == 2 ? m[1] : "0#{m[1]}"
|
36
36
|
cntry_cd = '01' if cntry_cd.nil? || cntry_cd.empty? || cntry_cd == '0'
|
37
|
-
Number.new( :country_code => cntry_cd, :area_code => m[2], :subscriber_number_prefix => m[3],
|
38
|
-
|
37
|
+
return Number.new( :country_code => cntry_cd, :area_code => m[2], :subscriber_number_prefix => m[3],
|
38
|
+
:subscriber_number_postfix => m[4], :extension => m[5] )
|
39
39
|
end
|
40
|
+
|
41
|
+
Number.new( {} )
|
40
42
|
end
|
41
43
|
|
42
44
|
def empty?
|
@@ -56,7 +58,7 @@ module PhoneNumber
|
|
56
58
|
return '' if self.empty?
|
57
59
|
to_return = pattern.is_a?( Symbol ) ? @@patterns[pattern] : pattern
|
58
60
|
to_return = @@patterns[:us_short] if to_return.empty?
|
59
|
-
|
61
|
+
@pattern_map.each { |pat, replacement| to_return = to_return.gsub( pat, replacement ) }
|
60
62
|
to_return.strip
|
61
63
|
end
|
62
64
|
|
data/phone_number.gemspec
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
require File.expand_path( File.dirname(__FILE__) + '/number_parsing_shared_spec' )
|
3
3
|
|
4
4
|
|
5
5
|
describe "PhoneNumber::Number" do
|
6
6
|
it "should agree that the MATCH_REGEX is /^\+?(\d{0,2})[ \.\-]?\(?(\d{3})\)?[ \.\-]?(\d{3})[ \.\-]?(\d{4})[ x]?(\d*)$/" do
|
7
|
-
PhoneNumber::Number::
|
7
|
+
PhoneNumber::Number::REGEX.should == /^\+?(\d{0,2})[ \.\-]?\(?(\d{3})\)?[ \.\-]?(\d{3})[ \.\-]?(\d{4})[ x]?(\d*)$/
|
8
8
|
end
|
9
9
|
|
10
10
|
describe "when initializing from a raw string with an extension" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phone_number
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 1
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 1.
|
10
|
+
version: 1.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- C. Jason Harrelson (midas)
|