as_dialed_from 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.2.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{as_dialed_from}
8
- s.version = "0.1.4"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Justin Campbell"]
@@ -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
- raise "+<country_code> is required at the beginning of the number" unless @number[0,1] == "+"
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 = @number[1,length]
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
- n = @number.gsub "+#{country_code}", ""
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: 19
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 4
10
- version: 0.1.4
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Justin Campbell