as_dialed_from 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +6 -0
- data/VERSION +1 -1
- data/as_dialed_from.gemspec +2 -2
- data/lib/as_dialed_from/number.rb +15 -17
- data/test/as_dialed_from/number_test.rb +10 -2
- metadata +4 -4
data/README.md
CHANGED
@@ -3,11 +3,17 @@
|
|
3
3
|
Figure out how a number should be dialed from another country.
|
4
4
|
A fork of a port of Google's libphonenumber.
|
5
5
|
|
6
|
+
## Warning
|
7
|
+
|
8
|
+
Not all country combinations work correctly. Contributions welcome.
|
9
|
+
|
6
10
|
## Examples
|
7
11
|
|
8
12
|
as_dialed_from prototypes the String class with an `as_dialed_from` method.
|
9
13
|
|
10
14
|
```rb
|
15
|
+
require 'as_dialed_from'
|
16
|
+
|
11
17
|
"+12155551212".as_dialed_from "US"
|
12
18
|
=> "12155551212"
|
13
19
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/as_dialed_from.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{as_dialed_from}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.1"
|
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"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-08-04}
|
13
13
|
s.description = %q{Figure out how a number should be dialed from another country. A fork of a port of Google's libphonenumber.}
|
14
14
|
s.email = %q{jcampbell@movitas.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -17,7 +17,7 @@ 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
|
20
|
+
from_country = self.class.determine_country_code(from_country) if from_country.size > 3
|
21
21
|
|
22
22
|
# Convert numeric country code to region id
|
23
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?
|
@@ -30,15 +30,25 @@ module AsDialedFrom
|
|
30
30
|
"#{leading_zero}#{metadata[:national_prefix]}#{national_number}"
|
31
31
|
else
|
32
32
|
# If we're calling out of country, we need to dial the exit code and the destination country code before the number
|
33
|
-
"#{exit_code(from_metadata[:international_prefix])}#{country_code}#{leading_zero}#{national_number}"
|
33
|
+
"#{self.class.exit_code(from_metadata[:international_prefix])}#{country_code}#{leading_zero}#{national_number}"
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
37
|
def country_code
|
38
|
-
@country_code ||= determine_country_code(@number)
|
38
|
+
@country_code ||= self.class.determine_country_code(@number)
|
39
39
|
end
|
40
40
|
|
41
|
-
def
|
41
|
+
def national_number
|
42
|
+
@number.gsub /^(\+)?(#{country_code})/, ""
|
43
|
+
end
|
44
|
+
|
45
|
+
def leading_zero
|
46
|
+
"0" if metadata[:leading_zero_possible]
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def self.determine_country_code(number)
|
42
52
|
number = number.to_s
|
43
53
|
|
44
54
|
number.delete! "+"
|
@@ -54,7 +64,7 @@ module AsDialedFrom
|
|
54
64
|
raise "No valid country code was present"
|
55
65
|
end
|
56
66
|
|
57
|
-
def exit_code(international_prefix)
|
67
|
+
def self.exit_code(international_prefix)
|
58
68
|
international_prefix.delete("[]").match international_prefix
|
59
69
|
end
|
60
70
|
|
@@ -64,18 +74,6 @@ module AsDialedFrom
|
|
64
74
|
@metadata
|
65
75
|
end
|
66
76
|
|
67
|
-
def national_number
|
68
|
-
@national_number ||= determine_national_number
|
69
|
-
end
|
70
|
-
|
71
|
-
def determine_national_number
|
72
|
-
@number.gsub /^(\+)?(#{country_code})/, ""
|
73
|
-
end
|
74
|
-
|
75
|
-
def leading_zero
|
76
|
-
"0" if metadata[:leading_zero_possible]
|
77
|
-
end
|
78
|
-
|
79
77
|
end
|
80
78
|
|
81
79
|
end
|
@@ -48,18 +48,26 @@ class Number < Test::Unit::TestCase
|
|
48
48
|
end
|
49
49
|
|
50
50
|
test "should return national number" do
|
51
|
-
assert_equal "6502530000", AsDialedFrom::Number.new(US_NUMBER).national_number
|
51
|
+
assert_equal "6502530000", AsDialedFrom::Number.new(US_NUMBER).send(:national_number)
|
52
52
|
end
|
53
53
|
|
54
54
|
test "should return a number to dial using as_dialed_from" do
|
55
|
+
# Intra-country calls
|
55
56
|
assert_equal "16502530000", AsDialedFrom::Number.new(US_NUMBER).as_dialed_from("US")
|
57
|
+
assert_equal "013312345678", AsDialedFrom::Number.new(MX_NUMBER1).as_dialed_from("MX")
|
56
58
|
|
59
|
+
# MX cell numbers
|
60
|
+
assert_equal "0112345678900", AsDialedFrom::Number.new(MX_MOBILE1).as_dialed_from("MX")
|
61
|
+
assert_equal "0115212345678900", AsDialedFrom::Number.new(MX_MOBILE1).as_dialed_from("US")
|
62
|
+
|
63
|
+
# US <-> MX
|
57
64
|
assert_equal "0016502530000", AsDialedFrom::Number.new(US_NUMBER).as_dialed_from("MX")
|
58
65
|
assert_equal "011523312345678", AsDialedFrom::Number.new(MX_NUMBER1).as_dialed_from("US")
|
59
66
|
|
60
67
|
assert_equal "0019002530000", AsDialedFrom::Number.new(US_PREMIUM).as_dialed_from("DE")
|
61
68
|
# assert_equal "16502530000", AsDialedFrom::Number.new(US_NUMBER).as_dialed_from("BS")
|
62
|
-
|
69
|
+
assert_equal "0016502530000", AsDialedFrom::Number.new(US_NUMBER).as_dialed_from("PL")
|
70
|
+
assert_equal "8~1016502530000", AsDialedFrom::Number.new(US_NUMBER).as_dialed_from("RU")
|
63
71
|
assert_equal "011447912345678", AsDialedFrom::Number.new(GB_MOBILE).as_dialed_from("US")
|
64
72
|
assert_equal "00491234", AsDialedFrom::Number.new(DE_SHORT_NUMBER).as_dialed_from("GB")
|
65
73
|
|
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: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 1
|
10
|
+
version: 0.2.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Justin Campbell
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-08-04 00:00:00 -04:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|