as_dialed_from 0.1.4 → 0.2.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/VERSION +1 -1
- data/as_dialed_from.gemspec +1 -1
- data/lib/as_dialed_from/number.rb +10 -8
- data/test/as_dialed_from/number_test.rb +13 -4
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/as_dialed_from.gemspec
CHANGED
@@ -17,8 +17,10 @@ module AsDialedFrom
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def as_dialed_from(from_country)
|
20
|
+
from_country = determine_country_code(from_country) if from_country.size > 3
|
21
|
+
|
20
22
|
# Convert numeric country code to region id
|
21
|
-
from_country = Metadata.country_code_to_region[from_country][0] if from_country.is_a? Integer or from_country.to_i.nonzero?
|
23
|
+
from_country = Metadata.country_code_to_region[from_country.to_s][0] if from_country.is_a? Integer or from_country.to_i.nonzero?
|
22
24
|
|
23
25
|
from_metadata = Metadata.for_region(from_country)
|
24
26
|
raise "Could not find valid metadata for from_country" unless from_metadata
|
@@ -33,16 +35,18 @@ module AsDialedFrom
|
|
33
35
|
end
|
34
36
|
|
35
37
|
def country_code
|
36
|
-
@country_code ||= determine_country_code
|
38
|
+
@country_code ||= determine_country_code(@number)
|
37
39
|
end
|
38
40
|
|
39
|
-
def determine_country_code
|
40
|
-
|
41
|
+
def determine_country_code(number)
|
42
|
+
number = number.to_s
|
43
|
+
|
44
|
+
number.delete! "+"
|
41
45
|
|
42
46
|
# Test the leading digits to find a valid country_code
|
43
47
|
# Country codes are not ambiguous (ex. 1 is valid, and there is no 1X or 1XX)
|
44
48
|
(1..3).each do |length|
|
45
|
-
possible_country_code =
|
49
|
+
possible_country_code = number[0,length]
|
46
50
|
|
47
51
|
return possible_country_code if Metadata.country_code_to_region[possible_country_code]
|
48
52
|
end
|
@@ -65,9 +69,7 @@ module AsDialedFrom
|
|
65
69
|
end
|
66
70
|
|
67
71
|
def determine_national_number
|
68
|
-
|
69
|
-
n.match metadata[:national_number_format] if metadata[:national_number_format]
|
70
|
-
n
|
72
|
+
@number.gsub /^(\+)?(#{country_code})/, ""
|
71
73
|
end
|
72
74
|
|
73
75
|
def leading_zero
|
@@ -37,10 +37,6 @@ class Number < Test::Unit::TestCase
|
|
37
37
|
assert AsDialedFrom::Number.new US_NUMBER
|
38
38
|
end
|
39
39
|
|
40
|
-
test "should raise an error when no + is passed" do
|
41
|
-
assert_raise(RuntimeError) { AsDialedFrom::Number.new "12155551212" }
|
42
|
-
end
|
43
|
-
|
44
40
|
test "should determine country code from string" do
|
45
41
|
assert_equal "1", AsDialedFrom::Number.new(US_NUMBER ).country_code
|
46
42
|
assert_equal "52", AsDialedFrom::Number.new(MX_NUMBER1).country_code
|
@@ -75,4 +71,17 @@ class Number < Test::Unit::TestCase
|
|
75
71
|
assert_equal "0115491187654321", AsDialedFrom::Number.new(AR_MOBILE).as_dialed_from("US")
|
76
72
|
end
|
77
73
|
|
74
|
+
test "as_dialed_from should accept a country code string as input" do
|
75
|
+
assert_equal "16502530000", AsDialedFrom::Number.new(US_NUMBER).as_dialed_from("1")
|
76
|
+
end
|
77
|
+
|
78
|
+
test "as_dialed_from should accept a country code integer as input" do
|
79
|
+
assert_equal "16502530000", AsDialedFrom::Number.new(US_NUMBER).as_dialed_from(1)
|
80
|
+
end
|
81
|
+
|
82
|
+
test "as_dialed_from should accept a caller id number as input" do
|
83
|
+
assert_equal "16502530000", AsDialedFrom::Number.new(US_NUMBER).as_dialed_from(US_NUMBER)
|
84
|
+
assert_equal "0016502530000", AsDialedFrom::Number.new(US_NUMBER).as_dialed_from(MX_NUMBER1)
|
85
|
+
end
|
86
|
+
|
78
87
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: as_dialed_from
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Justin Campbell
|