torasup 0.0.9 → 0.0.10
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/.rspec +2 -0
- data/README.md +25 -17
- data/lib/torasup/data/pstn.yaml +99 -42
- data/lib/torasup/operator.rb +38 -21
- data/lib/torasup/test/helpers.rb +22 -6
- data/lib/torasup/version.rb +1 -1
- data/lib/torasup.rb +41 -8
- data/spec/spec_helper.rb +0 -10
- data/spec/support/pstn_helpers.rb +9 -7
- data/spec/support/pstn_spec.yaml +70 -42
- data/spec/torasup/configuration_spec.rb +15 -17
- data/spec/torasup/location_spec.rb +1 -3
- data/spec/torasup/operator_spec.rb +12 -10
- data/spec/torasup/phone_number_spec.rb +16 -18
- data/spec/torasup_spec.rb +20 -0
- data/torasup.gemspec +1 -2
- metadata +7 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9599e7603adef5065a28bad6206eb29ee3ff4dfe
|
4
|
+
data.tar.gz: 6b88f5093ccf748789ef22e0234de5be6745d8f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0822508de2801a60af3cdf654375695043590bec63feba622e37b404a1dfeb629b54e363575e37d628771cf261cd4283eee86da900e4a496386fa32f18fb373f
|
7
|
+
data.tar.gz: a44865824ca4faddc34ab13a77347acbbb2b39912e9278886be2b271b01ef670913faa4e30ee9490be34b7c2075215869d39a5a3fd630c270e86613a578591ff
|
data/.rspec
ADDED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Retuns metadata about a phone number such as operator info, area code and more.
|
4
4
|
|
5
|
-
[](https://travis-ci.org/dwilkie/torasup) [](https://gemnasium.com/dwilkie/torasup) [](https://travis-ci.org/dwilkie/torasup) [](https://gemnasium.com/dwilkie/torasup) [](https://codeclimate.com/github/dwilkie/torasup)
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -14,28 +14,28 @@ gem 'torasup'
|
|
14
14
|
|
15
15
|
And then execute:
|
16
16
|
|
17
|
-
```
|
17
|
+
```
|
18
18
|
$ bundle
|
19
19
|
```
|
20
20
|
|
21
21
|
Or install it yourself as:
|
22
22
|
|
23
|
-
```
|
23
|
+
```
|
24
24
|
$ gem install torasup
|
25
25
|
```
|
26
26
|
|
27
27
|
## Usage
|
28
28
|
|
29
|
-
###
|
29
|
+
### Returning info about a Phone Number
|
30
30
|
|
31
|
-
```
|
31
|
+
```
|
32
32
|
$ irb
|
33
33
|
```
|
34
34
|
|
35
35
|
```ruby
|
36
36
|
require 'torasup'
|
37
37
|
|
38
|
-
pn = Torasup::PhoneNumber.new("+855 (0) 62 451
|
38
|
+
pn = Torasup::PhoneNumber.new("+855 (0) 62 451 2345")
|
39
39
|
|
40
40
|
pn.number
|
41
41
|
=> "85562451234"
|
@@ -53,7 +53,7 @@ pn.prefix
|
|
53
53
|
=> "45"
|
54
54
|
|
55
55
|
pn.local_number
|
56
|
-
=> "
|
56
|
+
=> "12345"
|
57
57
|
|
58
58
|
loc = pn.location
|
59
59
|
loc.area
|
@@ -68,6 +68,20 @@ op.name
|
|
68
68
|
=> "Smart"
|
69
69
|
```
|
70
70
|
|
71
|
+
### Accessing Metadata by Operator
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
Torasup::Operator.all["kh"]["metfone"]["mobile_prefixes"]
|
75
|
+
=> {"85538"=>{"subscriber_number_min"=>2000000, "subscriber_number_max"=>9999999, "subscriber_number_pattern"=>"[2-9]\\d{6}"}}
|
76
|
+
```
|
77
|
+
|
78
|
+
### Accessing Metadata by Prefix
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
Torasup.prefixes["8552345"]
|
82
|
+
=> {"country_id"=>"kh", "id"=>"smart", "name"=>"Smart", "subscriber_number_min"=>0, "subscriber_number_max"=>99999, "subscriber_number_pattern"=>"\\d{5}", "type"=>"landline", "prefix"=>"45", "area_code"=>"23"}
|
83
|
+
```
|
84
|
+
|
71
85
|
## Configuration
|
72
86
|
|
73
87
|
### Overriding Data
|
@@ -101,7 +115,7 @@ Torasup.configure do |config|
|
|
101
115
|
config.custom_pstn_data_file = "my_pstn_data.yaml"
|
102
116
|
end
|
103
117
|
|
104
|
-
pn = Torasup::PhoneNumber.new("+855 (0) 62 451
|
118
|
+
pn = Torasup::PhoneNumber.new("+855 (0) 62 451 2345")
|
105
119
|
op = pn.operator
|
106
120
|
|
107
121
|
op.id
|
@@ -120,24 +134,18 @@ op.my_custom_boolean_property
|
|
120
134
|
=> true
|
121
135
|
```
|
122
136
|
|
123
|
-
### Accessing Operator Metadata
|
124
|
-
|
125
|
-
```ruby
|
126
|
-
Torasup::Operator.all["kh"]["metfone"]["prefixes"]
|
127
|
-
=> ["85597", "85588"]
|
128
|
-
```
|
129
|
-
|
130
137
|
### Registering Operators
|
131
138
|
|
132
139
|
Sometimes you may only be interested in certain prefixes. For example let's say you want to match phone numbers from a certain operator from the database. You can register operators for this purpose. e.g.
|
133
140
|
|
134
141
|
```ruby
|
135
142
|
Torasup.configure do |config|
|
136
|
-
config.register_operators("kh", "
|
143
|
+
config.register_operators("kh", "cootel")
|
137
144
|
end
|
138
145
|
|
139
146
|
Torasup::Operator.registered
|
140
|
-
=> {"kh"=>{"
|
147
|
+
=> {"kh"=>{"cootel"=>{"country_id"=>"kh", "id"=>"cootel", "name"=>"CooTel", "mobile_prefixes"=>{"85538"=>{"subscriber_number_min"=>2000000, "subscriber_number_max"=>9999999, "subscriber_number_pattern"=>"[2-9]\\d{6}"}}
|
148
|
+
}}}
|
141
149
|
```
|
142
150
|
|
143
151
|
### Default Operators
|
data/lib/torasup/data/pstn.yaml
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
kh:
|
3
3
|
# http://en.wikipedia.org/wiki/Telephone_numbers_in_Cambodia
|
4
4
|
international_dialing_code: "855"
|
5
|
+
default_mobile_subscriber_number_min: 200000
|
6
|
+
default_mobile_subscriber_number_max: 999999
|
7
|
+
default_mobile_subscriber_number_pattern: "[2-9]\\d{5}"
|
8
|
+
default_landline_subscriber_number_min: 0
|
9
|
+
default_landline_subscriber_number_max: 99999
|
10
|
+
default_landline_subscriber_number_pattern: "\\d{5}"
|
5
11
|
area_codes:
|
6
12
|
"23": "Phnom Penh"
|
7
13
|
"24": "Kandal"
|
@@ -9,7 +15,7 @@ kh:
|
|
9
15
|
"26": "Kampong Chhnang"
|
10
16
|
"32": "Takeo"
|
11
17
|
"33": "Kampot"
|
12
|
-
"34": "
|
18
|
+
"34": "Preah Sihanouk"
|
13
19
|
"35": "Koh Kong"
|
14
20
|
"36": "Kep"
|
15
21
|
"42": "Kampong Cham"
|
@@ -33,67 +39,118 @@ kh:
|
|
33
39
|
metadata:
|
34
40
|
name: "Smart"
|
35
41
|
prefixes:
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
42
|
+
- '10'
|
43
|
+
- '15'
|
44
|
+
- '16'
|
45
|
+
- '69'
|
46
|
+
- '70'
|
47
|
+
- '81'
|
48
|
+
- '86'
|
49
|
+
- '87'
|
50
|
+
- '93'
|
51
|
+
- '96':
|
52
|
+
subscriber_number_min: 2000000
|
53
|
+
subscriber_number_max: 9999999
|
54
|
+
subscriber_number_pattern: "[2-9]\\d{6}"
|
55
|
+
- '98'
|
47
56
|
area_code_prefixes:
|
48
|
-
|
57
|
+
- '45'
|
58
|
+
- '56'
|
49
59
|
mobitel:
|
50
60
|
metadata:
|
51
61
|
name: "Mobitel"
|
52
62
|
prefixes:
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
63
|
+
- '11'
|
64
|
+
- '12'
|
65
|
+
- '14'
|
66
|
+
- '17'
|
67
|
+
- '61'
|
68
|
+
- '76':
|
69
|
+
subscriber_number_min: 2000000
|
70
|
+
subscriber_number_max: 9999999
|
71
|
+
subscriber_number_pattern: "[2-9]\\d{6}"
|
72
|
+
- '77'
|
73
|
+
- '78'
|
74
|
+
- '85'
|
75
|
+
- '89'
|
76
|
+
- '92'
|
77
|
+
- '95'
|
78
|
+
- '99'
|
79
|
+
area_code_prefixes:
|
80
|
+
- '3'
|
81
|
+
- '50'
|
82
|
+
- '51'
|
83
|
+
- '52'
|
84
|
+
- '53'
|
85
|
+
- '54'
|
86
|
+
- '55'
|
65
87
|
metfone:
|
66
88
|
metadata:
|
67
89
|
name: "Metfone"
|
68
90
|
prefixes:
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
91
|
+
- '31':
|
92
|
+
subscriber_number_min: 2000000
|
93
|
+
subscriber_number_max: 9999999
|
94
|
+
subscriber_number_pattern: "[2-9]\\d{6}"
|
95
|
+
- '60'
|
96
|
+
- '66'
|
97
|
+
- '67'
|
98
|
+
- '68'
|
99
|
+
- '71':
|
100
|
+
subscriber_number_min: 2000000
|
101
|
+
subscriber_number_max: 9999999
|
102
|
+
subscriber_number_pattern: "[2-9]\\d{6}"
|
103
|
+
- '88':
|
104
|
+
subscriber_number_min: 2000000
|
105
|
+
subscriber_number_max: 9999999
|
106
|
+
subscriber_number_pattern: "[2-9]\\d{6}"
|
107
|
+
- '90'
|
108
|
+
- '97':
|
109
|
+
subscriber_number_min: 2000000
|
110
|
+
subscriber_number_max: 9999999
|
111
|
+
subscriber_number_pattern: "[2-9]\\d{6}"
|
78
112
|
area_code_prefixes:
|
79
|
-
|
113
|
+
- '6':
|
114
|
+
subscriber_number_max: 999999
|
115
|
+
subscriber_number_pattern: "\\d{6}"
|
116
|
+
- '46'
|
80
117
|
qb:
|
81
118
|
metadata:
|
82
119
|
name: "Qb"
|
83
120
|
prefixes:
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
121
|
+
- '13'
|
122
|
+
- '80'
|
123
|
+
- '83'
|
124
|
+
- '84'
|
125
|
+
area_code_prefixes:
|
126
|
+
- '49'
|
88
127
|
excell:
|
89
128
|
metadata:
|
90
129
|
name: "Excell"
|
91
130
|
prefixes:
|
92
|
-
|
131
|
+
- '18'
|
132
|
+
area_code_prefixes:
|
133
|
+
- '48'
|
93
134
|
cootel:
|
94
135
|
metadata:
|
95
136
|
name: "CooTel"
|
96
137
|
prefixes:
|
97
|
-
|
138
|
+
- '38':
|
139
|
+
subscriber_number_min: 2000000
|
140
|
+
subscriber_number_max: 9999999
|
141
|
+
subscriber_number_pattern: "[2-9]\\d{6}"
|
142
|
+
area_code_prefixes:
|
143
|
+
- '47'
|
144
|
+
camintel:
|
145
|
+
metadata:
|
146
|
+
name: "Camintel"
|
147
|
+
area_code_prefixes:
|
148
|
+
- '9'
|
149
|
+
tc:
|
150
|
+
metadata:
|
151
|
+
name: "Telecom Cambodia"
|
98
152
|
area_code_prefixes:
|
99
|
-
- '
|
153
|
+
- '2'
|
154
|
+
- '4'
|
155
|
+
- '7'
|
156
|
+
- '8'
|
data/lib/torasup/operator.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
module Torasup
|
2
2
|
class Operator
|
3
|
-
attr_accessor :prefix, :local_number, :area_code, :country_code
|
3
|
+
attr_accessor :prefix, :local_number, :area_code, :country_code, :full_number
|
4
4
|
|
5
5
|
def initialize(country_code, area_code_or_prefix, unresolved_local_number)
|
6
|
-
|
6
|
+
self.country_code = country_code
|
7
|
+
self.full_number = full_prefix(area_code_or_prefix, unresolved_local_number)
|
7
8
|
parse_phone_number(area_code_or_prefix, unresolved_local_number)
|
8
9
|
end
|
9
10
|
|
@@ -28,37 +29,53 @@ module Torasup
|
|
28
29
|
private
|
29
30
|
|
30
31
|
def parse_phone_number(area_code_or_prefix, unresolved_local_number)
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
32
|
+
resolve_number_parts(area_code_or_prefix, unresolved_local_number)
|
33
|
+
end
|
34
|
+
|
35
|
+
def resolve_number_parts(area_code_or_prefix, unresolved_local_number)
|
36
|
+
unresolved_local_number_length = unresolved_local_number.length
|
37
|
+
unresolved_local_number_length.times do |n|
|
38
|
+
return if set_number_parts(
|
39
|
+
full_prefix(area_code_or_prefix, unresolved_local_number[0..(unresolved_local_number_length - n - 1)])
|
40
|
+
)
|
40
41
|
end
|
42
|
+
|
43
|
+
set_number_parts(full_prefix(area_code_or_prefix))
|
41
44
|
end
|
42
45
|
|
43
|
-
def
|
44
|
-
|
46
|
+
def set_number_parts(test_prefix)
|
47
|
+
prefix_data = Torasup.prefix_data(test_prefix)
|
48
|
+
self.area_code = prefix_data["area_code"]
|
49
|
+
self.prefix = prefix_data["prefix"]
|
50
|
+
local_number = full_number.gsub(/^#{full_prefix}/, "")
|
51
|
+
self.local_number = local_number.empty? ? nil : local_number
|
52
|
+
prefix_data.any?
|
45
53
|
end
|
46
54
|
|
47
55
|
def full_prefix(*parts)
|
48
|
-
parts = [
|
49
|
-
|
56
|
+
parts = [area_code, prefix] if parts.empty?
|
57
|
+
country_code + parts.join
|
50
58
|
end
|
51
59
|
|
52
60
|
def self.build_metadata(operator_type)
|
53
61
|
operators = {}
|
54
|
-
Torasup.send(operator_type).each do |prefix,
|
55
|
-
prefix_country_id =
|
62
|
+
Torasup.send(operator_type).each do |prefix, prefix_metadata|
|
63
|
+
prefix_country_id = prefix_metadata["country_id"]
|
56
64
|
country_operators = operators[prefix_country_id] ||= {}
|
57
|
-
prefix_operator_id =
|
58
|
-
operator_metadata = country_operators[prefix_operator_id] ||=
|
65
|
+
prefix_operator_id = prefix_metadata["id"]
|
66
|
+
operator_metadata = country_operators[prefix_operator_id] ||= prefix_metadata.dup
|
59
67
|
operator_metadata.delete("prefix")
|
60
|
-
|
61
|
-
|
68
|
+
operator_metadata.delete("type")
|
69
|
+
operator_metadata.delete("subscriber_number_min")
|
70
|
+
operator_metadata.delete("subscriber_number_max")
|
71
|
+
operator_metadata.delete("subscriber_number_pattern")
|
72
|
+
type = prefix_metadata["type"]
|
73
|
+
typed_prefixes = operator_metadata["#{type}_prefixes"] ||= {}
|
74
|
+
typed_prefixes[prefix] = {
|
75
|
+
"subscriber_number_min" => prefix_metadata["subscriber_number_min"],
|
76
|
+
"subscriber_number_max" => prefix_metadata["subscriber_number_max"],
|
77
|
+
"subscriber_number_pattern" => prefix_metadata["subscriber_number_pattern"],
|
78
|
+
}
|
62
79
|
end
|
63
80
|
operators
|
64
81
|
end
|
data/lib/torasup/test/helpers.rb
CHANGED
@@ -33,14 +33,24 @@ module Torasup
|
|
33
33
|
local_number = country_data["local_number"]
|
34
34
|
default_assertions = {"country_code" => country_prefix}
|
35
35
|
with_operator_data(country_id, options) do |operator, operator_data|
|
36
|
-
default_operator_assertions = operator_data["assertions"].merge(
|
36
|
+
default_operator_assertions = operator_data["assertions"].merge(
|
37
|
+
"country_id" => country_id, "id" => operator
|
38
|
+
).merge(default_assertions)
|
37
39
|
with_operator_area_codes(country_data, operator_data) do |area_code_prefix, area_code, area|
|
40
|
+
if area_code_prefix.is_a?(Hash)
|
41
|
+
custom_local_number = area_code_prefix.values.first
|
42
|
+
area_code_prefix = area_code_prefix.keys.first
|
43
|
+
end
|
44
|
+
|
38
45
|
area_code_assertions = operator_assertions[country_prefix][area_code] ||= {}
|
39
46
|
area_code_assertions[area_code_prefix] = {}
|
40
|
-
custom_local_number
|
47
|
+
custom_local_number ||= local_number.dup[0..4]
|
41
48
|
unresolved_number = area_code_prefix + custom_local_number
|
42
49
|
area_code_assertions[area_code_prefix][custom_local_number] = default_operator_assertions.merge(
|
43
|
-
"area_code" => area_code,
|
50
|
+
"area_code" => area_code,
|
51
|
+
"prefix" => area_code_prefix,
|
52
|
+
"local_number" => custom_local_number,
|
53
|
+
"type" => "landline"
|
44
54
|
)
|
45
55
|
end
|
46
56
|
with_operator_prefixes(operator_data) do |prefix|
|
@@ -50,8 +60,14 @@ module Torasup
|
|
50
60
|
end
|
51
61
|
prefix_assertions = operator_assertions[country_prefix][prefix] = {}
|
52
62
|
no_area_code_assertions = prefix_assertions[nil] = {}
|
53
|
-
|
54
|
-
|
63
|
+
|
64
|
+
custom_local_number ||= local_number
|
65
|
+
|
66
|
+
no_area_code_assertions[custom_local_number] = default_operator_assertions.merge(
|
67
|
+
"area_code" => nil,
|
68
|
+
"prefix" => prefix,
|
69
|
+
"local_number" => custom_local_number,
|
70
|
+
"type" => "mobile"
|
55
71
|
)
|
56
72
|
end
|
57
73
|
end
|
@@ -91,7 +107,7 @@ module Torasup
|
|
91
107
|
end
|
92
108
|
|
93
109
|
def with_operator_prefixes(operator_data, &block)
|
94
|
-
operator_data["prefixes"].each do |prefix|
|
110
|
+
(operator_data["prefixes"] || {}).each do |prefix|
|
95
111
|
yield prefix
|
96
112
|
end
|
97
113
|
end
|
data/lib/torasup/version.rb
CHANGED
data/lib/torasup.rb
CHANGED
@@ -10,7 +10,6 @@ require "torasup/operator"
|
|
10
10
|
require "torasup/location"
|
11
11
|
|
12
12
|
module Torasup
|
13
|
-
|
14
13
|
module Test
|
15
14
|
autoload :Helpers, 'torasup/test/helpers'
|
16
15
|
end
|
@@ -47,6 +46,10 @@ module Torasup
|
|
47
46
|
@pstn_prefixes[prefix] || {}
|
48
47
|
end
|
49
48
|
|
49
|
+
def registered_operators
|
50
|
+
configuration.registered_operators
|
51
|
+
end
|
52
|
+
|
50
53
|
def registered_operator_prefixes
|
51
54
|
@registered_pstn_prefixes.dup
|
52
55
|
end
|
@@ -63,7 +66,12 @@ module Torasup
|
|
63
66
|
@pstn_data.each do |country_id, country_properties|
|
64
67
|
operators(country_id).each do |operator, operator_properties|
|
65
68
|
operator_prefixes(country_id, operator).each do |operator_prefix, prefix_data|
|
66
|
-
prefix_properties = operator_metadata(
|
69
|
+
prefix_properties = operator_metadata(
|
70
|
+
country_id, operator
|
71
|
+
).merge(
|
72
|
+
prefix_defaults(country_properties, operator_properties, prefix_data)
|
73
|
+
).merge(prefix_data)
|
74
|
+
|
67
75
|
@pstn_prefixes[operator_prefix] = prefix_properties
|
68
76
|
@registered_pstn_prefixes[operator_prefix] = prefix_properties if operator_registered?(country_id, operator)
|
69
77
|
end
|
@@ -80,7 +88,7 @@ module Torasup
|
|
80
88
|
end
|
81
89
|
|
82
90
|
def operator_registered?(country_id, operator)
|
83
|
-
(
|
91
|
+
(registered_operators[country_id] || []).include?(operator)
|
84
92
|
end
|
85
93
|
|
86
94
|
def country_data(country_id)
|
@@ -111,11 +119,27 @@ module Torasup
|
|
111
119
|
operator_data(country_id, operator)["area_code_prefixes"] || {}
|
112
120
|
end
|
113
121
|
|
122
|
+
def prefix_defaults(country_properties, operator_properties, prefix_properties)
|
123
|
+
defaults = {}
|
124
|
+
prefix_type = prefix_properties["type"]
|
125
|
+
[:min, :max, :pattern].each do |prefix_key|
|
126
|
+
result_key = "subscriber_number_#{prefix_key}"
|
127
|
+
default_key = "default_#{prefix_type}_#{result_key}"
|
128
|
+
result_value = prefix_properties[result_key] || operator_properties[default_key] || country_properties[default_key]
|
129
|
+
defaults.merge!(result_key => result_value)
|
130
|
+
end
|
131
|
+
defaults
|
132
|
+
end
|
133
|
+
|
114
134
|
def operator_mobile_prefixes(country_id, operator)
|
115
135
|
full_prefixes = {}
|
116
|
-
|
117
|
-
mobile_prefixes
|
118
|
-
|
136
|
+
operator_data = operator_data(country_id, operator)
|
137
|
+
mobile_prefixes = array_to_hash(operator_data["prefixes"] || [])
|
138
|
+
mobile_prefixes.each do |mobile_prefix, prefix_metadata|
|
139
|
+
full_prefixes[operator_full_prefix(country_id, mobile_prefix)] = {
|
140
|
+
"type" => "mobile",
|
141
|
+
"prefix" => mobile_prefix,
|
142
|
+
}.merge(prefix_metadata)
|
119
143
|
end
|
120
144
|
full_prefixes
|
121
145
|
end
|
@@ -126,13 +150,22 @@ module Torasup
|
|
126
150
|
|
127
151
|
def operator_prefixes(country_id, operator)
|
128
152
|
operator_prefixes = operator_mobile_prefixes(country_id, operator)
|
129
|
-
operator_area_code_prefixes(country_id, operator)
|
153
|
+
area_code_prefixes = array_to_hash(operator_area_code_prefixes(country_id, operator))
|
154
|
+
area_code_prefixes.each do |operator_area_code_prefix, prefix_metadata|
|
130
155
|
area_codes(country_id).each do |area_code, area|
|
131
|
-
operator_prefixes[operator_full_prefix(country_id, area_code, operator_area_code_prefix)] = {
|
156
|
+
operator_prefixes[operator_full_prefix(country_id, area_code, operator_area_code_prefix)] = {
|
157
|
+
"type" => "landline",
|
158
|
+
"prefix" => operator_area_code_prefix,
|
159
|
+
"area_code" => area_code
|
160
|
+
}.merge(prefix_metadata)
|
132
161
|
end
|
133
162
|
end
|
134
163
|
operator_prefixes
|
135
164
|
end
|
165
|
+
|
166
|
+
def array_to_hash(array)
|
167
|
+
array.map { |n| n.is_a?(Hash) ? n : {n => {}} }.reduce(Hash.new, :merge)
|
168
|
+
end
|
136
169
|
end
|
137
170
|
|
138
171
|
load_international_dialing_codes!
|
data/spec/spec_helper.rb
CHANGED
@@ -1,15 +1,5 @@
|
|
1
1
|
require 'torasup'
|
2
|
-
require 'coveralls'
|
3
|
-
Coveralls.wear!
|
4
2
|
|
5
3
|
RSpec.configure do |config|
|
6
4
|
Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
|
7
|
-
|
8
|
-
config.expect_with :rspec do |c|
|
9
|
-
c.syntax = [:should, :expect]
|
10
|
-
end
|
11
|
-
|
12
|
-
config.mock_with :rspec do |mocks|
|
13
|
-
mocks.syntax = [:should, :expect]
|
14
|
-
end
|
15
5
|
end
|
@@ -1,13 +1,6 @@
|
|
1
1
|
module PstnHelpers
|
2
2
|
include Torasup::Test::Helpers
|
3
3
|
|
4
|
-
RSpec.configure do |config|
|
5
|
-
config.before do
|
6
|
-
clear_pstn
|
7
|
-
clear_registered_operators
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
4
|
private
|
12
5
|
|
13
6
|
def yaml_file(filename)
|
@@ -38,3 +31,12 @@ module PstnHelpers
|
|
38
31
|
end
|
39
32
|
end
|
40
33
|
end
|
34
|
+
|
35
|
+
RSpec.configure do |config|
|
36
|
+
config.include(PstnHelpers)
|
37
|
+
|
38
|
+
config.before do
|
39
|
+
clear_pstn
|
40
|
+
clear_registered_operators
|
41
|
+
end
|
42
|
+
end
|
data/spec/support/pstn_spec.yaml
CHANGED
@@ -12,7 +12,7 @@ kh:
|
|
12
12
|
'26': Kampong Chhnang
|
13
13
|
'32': Takeo
|
14
14
|
'33': Kampot
|
15
|
-
'34':
|
15
|
+
'34': Preah Sihanouk
|
16
16
|
'35': Koh Kong
|
17
17
|
'36': Kep
|
18
18
|
'42': Kampong Cham
|
@@ -36,67 +36,95 @@ kh:
|
|
36
36
|
assertions:
|
37
37
|
name: Smart
|
38
38
|
prefixes:
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
39
|
+
- '10'
|
40
|
+
- '15'
|
41
|
+
- '16'
|
42
|
+
- '69'
|
43
|
+
- '70'
|
44
|
+
- '81'
|
45
|
+
- '86'
|
46
|
+
- '87'
|
47
|
+
- '93'
|
48
|
+
- '96': "2345678"
|
49
|
+
- '98'
|
50
50
|
area_code_prefixes:
|
51
|
-
|
51
|
+
- '45'
|
52
|
+
- '56'
|
52
53
|
mobitel:
|
53
54
|
assertions:
|
54
55
|
name: Mobitel
|
55
56
|
prefixes:
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
57
|
+
- '11'
|
58
|
+
- '12'
|
59
|
+
- '14'
|
60
|
+
- '17'
|
61
|
+
- '61'
|
62
|
+
- '76': "2345678"
|
63
|
+
- '77'
|
64
|
+
- '78'
|
65
|
+
- '85'
|
66
|
+
- '89'
|
67
|
+
- '92'
|
68
|
+
- '95'
|
69
|
+
- '99'
|
70
|
+
area_code_prefixes:
|
71
|
+
- '3'
|
72
|
+
- '50'
|
73
|
+
- '51'
|
74
|
+
- '52'
|
75
|
+
- '53'
|
76
|
+
- '54'
|
77
|
+
- '55'
|
68
78
|
metfone:
|
69
79
|
assertions:
|
70
80
|
name: Metfone
|
71
81
|
prefixes:
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
82
|
+
- '31': "2345678"
|
83
|
+
- '60'
|
84
|
+
- '66'
|
85
|
+
- '67'
|
86
|
+
- '68'
|
87
|
+
- '71': "2345678"
|
88
|
+
- '88': "2345678"
|
89
|
+
- '90'
|
90
|
+
- '97': "2345678"
|
81
91
|
area_code_prefixes:
|
82
|
-
|
92
|
+
- '6': "234567"
|
93
|
+
- '46'
|
83
94
|
qb:
|
84
95
|
assertions:
|
85
96
|
name: Qb
|
86
97
|
prefixes:
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
98
|
+
- '13'
|
99
|
+
- '80'
|
100
|
+
- '83'
|
101
|
+
- '84'
|
102
|
+
area_code_prefixes:
|
103
|
+
- '49'
|
91
104
|
excell:
|
92
105
|
assertions:
|
93
106
|
name: Excell
|
94
107
|
prefixes:
|
95
|
-
|
108
|
+
- '18'
|
109
|
+
area_code_prefixes:
|
110
|
+
- '48'
|
96
111
|
cootel:
|
97
112
|
assertions:
|
98
113
|
name: CooTel
|
99
114
|
prefixes:
|
100
|
-
|
115
|
+
- '38': "2345678"
|
116
|
+
area_code_prefixes:
|
117
|
+
- '47'
|
118
|
+
camintel:
|
119
|
+
assertions:
|
120
|
+
name: "Camintel"
|
121
|
+
area_code_prefixes:
|
122
|
+
- '9'
|
123
|
+
tc:
|
124
|
+
assertions:
|
125
|
+
name: "Telecom Cambodia"
|
101
126
|
area_code_prefixes:
|
102
|
-
- '
|
127
|
+
- '2'
|
128
|
+
- '4'
|
129
|
+
- '7'
|
130
|
+
- '8'
|
@@ -1,65 +1,63 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Torasup do
|
4
|
-
include PstnHelpers
|
5
|
-
|
6
4
|
describe ".configure" do
|
7
5
|
describe "#registered_operators=" do
|
8
6
|
before do
|
9
|
-
Torasup.
|
7
|
+
allow(Torasup).to receive(:load_pstn_data!)
|
10
8
|
end
|
11
9
|
|
12
10
|
it "should set the registered operators and clear" do
|
13
11
|
Torasup.configure do |config|
|
14
|
-
Torasup.
|
15
|
-
config.registered_operators.
|
12
|
+
expect(Torasup).to receive(:load_pstn_data!)
|
13
|
+
expect(config.registered_operators).to eq({})
|
16
14
|
config.registered_operators = {"foo" => "bar"}
|
17
|
-
config.registered_operators.
|
15
|
+
expect(config.registered_operators).to eq({"foo" => "bar"})
|
18
16
|
end
|
19
17
|
end
|
20
18
|
end
|
21
19
|
|
22
20
|
describe "#register_operators(country_id, *operators)" do
|
23
21
|
before do
|
24
|
-
Torasup.
|
22
|
+
allow(Torasup).to receive(:load_pstn_data!)
|
25
23
|
end
|
26
24
|
|
27
25
|
it "should set the registered operators" do
|
28
26
|
Torasup.configure do |config|
|
29
|
-
Torasup.
|
27
|
+
expect(Torasup).to receive(:load_pstn_data!)
|
30
28
|
config.registered_operators = {}
|
31
29
|
config.register_operators("kh", "smart", "beeline")
|
32
|
-
config.registered_operators.
|
30
|
+
expect(config.registered_operators).to eq({"kh" => ["smart", "beeline"]})
|
33
31
|
end
|
34
32
|
end
|
35
33
|
end
|
36
34
|
|
37
35
|
describe "#default_countries=('['US', 'AU']')" do
|
38
36
|
before do
|
39
|
-
Torasup.
|
37
|
+
allow(Torasup).to receive(:load_international_dialing_codes!)
|
40
38
|
end
|
41
39
|
|
42
40
|
it "should set the default countries and reload the data" do
|
43
41
|
Torasup.configure do |config|
|
44
|
-
Torasup.
|
45
|
-
config.default_countries.
|
42
|
+
expect(Torasup).to receive(:load_international_dialing_codes!)
|
43
|
+
expect(config.default_countries).to eq(["US", "GB", "AU", "IT", "RU", "NO"])
|
46
44
|
config.default_countries = ["US", "GB"]
|
47
|
-
config.default_countries.
|
45
|
+
expect(config.default_countries).to eq(["US", "GB"])
|
48
46
|
end
|
49
47
|
end
|
50
48
|
end
|
51
49
|
|
52
50
|
describe "#custom_pstn_data_file=('path_to_yaml_file.yaml')" do
|
53
51
|
before do
|
54
|
-
Torasup.
|
52
|
+
allow(Torasup).to receive(:load_pstn_data!)
|
55
53
|
end
|
56
54
|
|
57
55
|
it "should set a custom pstn data file and reload the data" do
|
58
56
|
Torasup.configure do |config|
|
59
|
-
Torasup.
|
60
|
-
config.custom_pstn_data_file.
|
57
|
+
expect(Torasup).to receive(:load_pstn_data!)
|
58
|
+
expect(config.custom_pstn_data_file).to be_nil
|
61
59
|
config.custom_pstn_data_file = "foo.yaml"
|
62
|
-
config.custom_pstn_data_file.
|
60
|
+
expect(config.custom_pstn_data_file).to eq("foo.yaml")
|
63
61
|
end
|
64
62
|
end
|
65
63
|
end
|
@@ -2,8 +2,6 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module Torasup
|
4
4
|
describe Location do
|
5
|
-
include PstnHelpers
|
6
|
-
|
7
5
|
def with_locations(options = {}, &block)
|
8
6
|
location_assertions = {}
|
9
7
|
with_pstn_data(options) do |country_id, country_data|
|
@@ -27,7 +25,7 @@ module Torasup
|
|
27
25
|
assertions.each do |method, assertion|
|
28
26
|
result = subject.send(method)
|
29
27
|
result_error = result.nil? ? "nil" : "'#{result}'"
|
30
|
-
result.
|
28
|
+
expect(result).to(eq(assertion), "expected Location.new('#{country_id}', '#{area_code}').#{method} to return '#{assertion}' but got #{result_error}")
|
31
29
|
end
|
32
30
|
end
|
33
31
|
end
|
@@ -2,8 +2,6 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module Torasup
|
4
4
|
describe Operator do
|
5
|
-
include PstnHelpers
|
6
|
-
|
7
5
|
let(:sample_operator) do
|
8
6
|
country = pstn_data.first
|
9
7
|
[country.first, country.last["operators"].first.first]
|
@@ -15,12 +13,16 @@ module Torasup
|
|
15
13
|
operators = Operator.send(method) # run it twice to highlight the duplication problem
|
16
14
|
with_operators do |number_parts, assertions|
|
17
15
|
operator = operators[assertions["country_id"]][assertions["id"]]
|
18
|
-
operator["country_id"].
|
19
|
-
operator["id"].
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
)
|
16
|
+
expect(operator["country_id"]).to eq(assertions["country_id"])
|
17
|
+
expect(operator["id"]).to eq(assertions["id"])
|
18
|
+
asserted_prefix = assertions["country_code"].to_s + assertions["area_code"].to_s + assertions["prefix"].to_s
|
19
|
+
asserted_type = assertions["type"]
|
20
|
+
asserted_prefixes = operator["#{asserted_type}_prefixes"]
|
21
|
+
expect(asserted_prefixes).to include(asserted_prefix)
|
22
|
+
asserted_prefix_metadata = asserted_prefixes[asserted_prefix]
|
23
|
+
expect(asserted_prefix_metadata).to have_key("subscriber_number_min")
|
24
|
+
expect(asserted_prefix_metadata).to have_key("subscriber_number_max")
|
25
|
+
expect(asserted_prefix_metadata).to have_key("subscriber_number_pattern")
|
24
26
|
end
|
25
27
|
end
|
26
28
|
end
|
@@ -38,7 +40,7 @@ module Torasup
|
|
38
40
|
end
|
39
41
|
|
40
42
|
it "should return an empty hash" do
|
41
|
-
Operator.registered.
|
43
|
+
expect(Operator.registered).to eq({})
|
42
44
|
end
|
43
45
|
end
|
44
46
|
|
@@ -66,7 +68,7 @@ module Torasup
|
|
66
68
|
args << {:interpolation => nil} unless subject.respond_to?(method)
|
67
69
|
result = subject.send(method, *args)
|
68
70
|
result_error = result.nil? ? "nil" : "'#{result}'"
|
69
|
-
result.
|
71
|
+
expect(result).to(eq(interpolated_assertion(assertion, :interpolation => nil)), "expected Operator.new('#{number_parts}').#{method} to return '#{assertion}' but got #{result_error}")
|
70
72
|
end
|
71
73
|
end
|
72
74
|
end
|
@@ -2,8 +2,6 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module Torasup
|
4
4
|
describe PhoneNumber do
|
5
|
-
include PstnHelpers
|
6
|
-
|
7
5
|
def with_phone_numbers(options = {}, &block)
|
8
6
|
phone_number_assertions = {}
|
9
7
|
with_operators(options) do |number_parts, assertions|
|
@@ -26,53 +24,53 @@ module Torasup
|
|
26
24
|
|
27
25
|
describe "#location" do
|
28
26
|
it "should return an instance of Torasup::Location" do
|
29
|
-
subject.location.
|
27
|
+
expect(subject.location).to be_a(Torasup::Location)
|
30
28
|
end
|
31
29
|
end
|
32
30
|
|
33
31
|
describe "#operator" do
|
34
32
|
it "should return an instance of Torasup::Operator" do
|
35
|
-
subject.operator.
|
33
|
+
expect(subject.operator).to be_a(Torasup::Operator)
|
36
34
|
end
|
37
35
|
end
|
38
36
|
|
39
37
|
describe "#area_code" do
|
40
38
|
before do
|
41
|
-
location.
|
42
|
-
Torasup::Location.
|
39
|
+
allow(location).to receive(:area_code).and_return("123")
|
40
|
+
allow(Torasup::Location).to receive(:new).and_return(location)
|
43
41
|
end
|
44
42
|
|
45
43
|
it "should delegate to location" do
|
46
|
-
subject.area_code.
|
44
|
+
expect(subject.area_code).to eq("123")
|
47
45
|
end
|
48
46
|
end
|
49
47
|
|
50
48
|
describe "#prefix" do
|
51
49
|
before do
|
52
|
-
operator.
|
53
|
-
Torasup::Operator.
|
50
|
+
allow(operator).to receive(:prefix).and_return("12")
|
51
|
+
allow(Torasup::Operator).to receive(:new).and_return(operator)
|
54
52
|
end
|
55
53
|
|
56
54
|
it "should delegate to operator" do
|
57
|
-
subject.prefix.
|
55
|
+
expect(subject.prefix).to eq("12")
|
58
56
|
end
|
59
57
|
end
|
60
58
|
|
61
59
|
describe "#local_number" do
|
62
60
|
before do
|
63
|
-
operator.
|
64
|
-
Torasup::Operator.
|
61
|
+
allow(operator).to receive(:local_number).and_return("234567")
|
62
|
+
allow(Torasup::Operator).to receive(:new).and_return(operator)
|
65
63
|
end
|
66
64
|
|
67
65
|
it "should delegate to operator" do
|
68
|
-
subject.local_number.
|
66
|
+
expect(subject.local_number).to eq("234567")
|
69
67
|
end
|
70
68
|
end
|
71
69
|
|
72
70
|
shared_examples_for "a phone number" do
|
73
71
|
before do
|
74
|
-
Torasup::Location.
|
75
|
-
Torasup::Operator.
|
72
|
+
allow(Torasup::Location).to receive(:new).and_return(location)
|
73
|
+
allow(Torasup::Operator).to receive(:new).and_return(operator)
|
76
74
|
end
|
77
75
|
|
78
76
|
it "should return all the phone number attributes" do
|
@@ -80,15 +78,15 @@ module Torasup
|
|
80
78
|
area_code_or_prefix = assertions.delete("area_code_or_prefix")
|
81
79
|
local_number = assertions.delete("local_number")
|
82
80
|
|
83
|
-
Torasup::Location.
|
84
|
-
Torasup::Operator.
|
81
|
+
expect(Torasup::Location).to receive(:new).with(assertions["country_id"], area_code_or_prefix)
|
82
|
+
expect(Torasup::Operator).to receive(:new).with(assertions["country_code"], area_code_or_prefix, local_number)
|
85
83
|
|
86
84
|
subject = PhoneNumber.new(sample_number)
|
87
85
|
|
88
86
|
assertions.each do |method, assertion|
|
89
87
|
result = subject.send(method)
|
90
88
|
result_error = result.nil? ? "nil" : "'#{result}'"
|
91
|
-
result.
|
89
|
+
expect(result).to(eq(assertion), "expected PhoneNumber.new('#{sample_number}').#{method} to return '#{assertion}' but got #{result_error}")
|
92
90
|
end
|
93
91
|
end
|
94
92
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Torasup do
|
4
|
+
describe "#prefixes" do
|
5
|
+
let(:prefixes) { described_class.prefixes }
|
6
|
+
|
7
|
+
context "metadata" do
|
8
|
+
it "should include the correct min, max and pattern values" do
|
9
|
+
with_operators do |number_parts, assertions|
|
10
|
+
prefix = assertions["country_code"].to_s + assertions["area_code"].to_s + assertions["prefix"].to_s
|
11
|
+
prefix_metadata = prefixes[prefix]
|
12
|
+
local_number = assertions["local_number"]
|
13
|
+
expect(local_number.to_i).to be >= prefix_metadata["subscriber_number_min"]
|
14
|
+
expect(local_number.to_i).to be <= prefix_metadata["subscriber_number_max"]
|
15
|
+
expect(local_number).to match(Regexp.new(prefix_metadata["subscriber_number_pattern"]))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/torasup.gemspec
CHANGED
@@ -19,10 +19,9 @@ Gem::Specification.new do |gem|
|
|
19
19
|
gem.require_paths = ["lib"]
|
20
20
|
|
21
21
|
gem.add_runtime_dependency "countries"
|
22
|
-
gem.add_runtime_dependency "phony", '>= 2.
|
22
|
+
gem.add_runtime_dependency "phony", '>= 2.14.3'
|
23
23
|
gem.add_runtime_dependency "deep_merge"
|
24
24
|
|
25
25
|
gem.add_development_dependency "rspec"
|
26
26
|
gem.add_development_dependency "rake"
|
27
|
-
gem.add_development_dependency "coveralls"
|
28
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: torasup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Wilkie
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-06-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: countries
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 2.
|
33
|
+
version: 2.14.3
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 2.
|
40
|
+
version: 2.14.3
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: deep_merge
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,20 +80,6 @@ dependencies:
|
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: coveralls
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - ">="
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - ">="
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '0'
|
97
83
|
description: '"Retuns metadata about a phone number such as operator, area code and
|
98
84
|
more"'
|
99
85
|
email:
|
@@ -103,6 +89,7 @@ extensions: []
|
|
103
89
|
extra_rdoc_files: []
|
104
90
|
files:
|
105
91
|
- ".gitignore"
|
92
|
+
- ".rspec"
|
106
93
|
- ".travis.yml"
|
107
94
|
- Gemfile
|
108
95
|
- LICENSE.txt
|
@@ -125,6 +112,7 @@ files:
|
|
125
112
|
- spec/torasup/location_spec.rb
|
126
113
|
- spec/torasup/operator_spec.rb
|
127
114
|
- spec/torasup/phone_number_spec.rb
|
115
|
+
- spec/torasup_spec.rb
|
128
116
|
- torasup.gemspec
|
129
117
|
homepage: https://github.com/dwilkie/torasup/
|
130
118
|
licenses:
|
@@ -160,3 +148,4 @@ test_files:
|
|
160
148
|
- spec/torasup/location_spec.rb
|
161
149
|
- spec/torasup/operator_spec.rb
|
162
150
|
- spec/torasup/phone_number_spec.rb
|
151
|
+
- spec/torasup_spec.rb
|