torasup 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 888eca4d5a8db09f5784bbb6de787143393a5f0b
4
- data.tar.gz: 37652d228d0a9708775248cdef7f9f1ca496e163
2
+ SHA256:
3
+ metadata.gz: edd45e45ba249800acd5270089cc64b53592617106d1e51a27ccb852b405f1bd
4
+ data.tar.gz: 553054695cf8be0734cad6d576bf0a5d1c702fb58a512607e2611aee9fb35005
5
5
  SHA512:
6
- metadata.gz: e20aa4caea8eb67315a9425d1e27f19136aa6cdbac5c547008d1372b1a68e369efdc16cb37a12b16de8cd93bc3c9ee2c0e2a502620fea2bae43760cee7d00121
7
- data.tar.gz: af5fa3eb04a5e85b951d77b8c70ce9b7ffab012550b984d3ebe657355fc3427408c88410696ecd9c53933e387e74f0905500dd1d8a9b98d9deb1047ff6b89880
6
+ metadata.gz: 49f26e1e771ec9df3a1dd7f952295b9fb3efb38433597afb245ccbac9da0267915ce27a5bb7a8e96fd34a4b07b7103207599b7578030fe5b0f3f5b060ce336b1
7
+ data.tar.gz: 3413d6e025acbcf2b0599bff021ccba403ab1fe0c61f54b2a1a2cab853fdeedcf5c673373bea00b940160e5ce7b12b5288a604ba59e7c0c9e30c4bd5b1e3f59f
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ Style/FrozenStringLiteralComment:
2
+ Enabled: false
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Metrics/LineLength:
8
+ Max: 100
9
+
10
+ Style/Documentation:
11
+ Enabled: false
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.4.2
1
+ 2.5.0
data/README.md CHANGED
@@ -3,8 +3,8 @@
3
3
  Retuns metadata about a phone number such as operator info, area code and more.
4
4
 
5
5
  [![Build Status](https://travis-ci.org/somleng/torasup.png)](https://travis-ci.org/somleng/torasup)
6
- [![Test Coverage](https://codeclimate.com/github/somleng/torasup/badges/coverage.svg)](https://codeclimate.com/github/somleng/torasup/coverage)
7
- [![Code Climate](https://codeclimate.com/github/somleng/torasup/badges/gpa.svg)](https://codeclimate.com/github/somleng/torasup)
6
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/5598e3554335eb385bd8/test_coverage)](https://codeclimate.com/github/somleng/torasup/test_coverage)
7
+ [![Maintainability](https://api.codeclimate.com/v1/badges/5598e3554335eb385bd8/maintainability)](https://codeclimate.com/github/somleng/torasup/maintainability)
8
8
 
9
9
  ## Installation
10
10
 
@@ -1,5 +1,5 @@
1
1
  class Configuration
2
- DEFAULT_COUNTRIES = ["US", "GB", "AU", "IT", "RU", "NO"]
2
+ DEFAULT_COUNTRIES = %w[US GB AU IT RU NO].freeze
3
3
  attr_accessor :registered_operators, :default_countries, :custom_pstn_data_files
4
4
 
5
5
  def initialize
@@ -220,3 +220,7 @@ sl:
220
220
  - '30'
221
221
  - '77'
222
222
  - '88'
223
+ br:
224
+ # https://en.wikipedia.org/wiki/Telecommunications_in_Brazil
225
+ # https://en.wikipedia.org/wiki/List_of_mobile_network_operators_of_the_Americas#Brazil
226
+ international_dialing_code: "55"
@@ -1,10 +1,9 @@
1
1
  module Torasup
2
2
  module Test
3
3
  module Helpers
4
-
5
4
  private
6
5
 
7
- def yaml_file(filename)
6
+ def yaml_file(_filename)
8
7
  raise "Override this method to return the full path of the yaml spec"
9
8
  end
10
9
 
@@ -26,17 +25,17 @@ module Torasup
26
25
  custom_spec ? data.deeper_merge(load_yaml_file(custom_spec)) : data
27
26
  end
28
27
 
29
- def with_operators(options = {}, &block)
28
+ def with_operators(options = {})
30
29
  operator_assertions = {}
31
30
  with_pstn_data(options) do |country_id, country_data, country_prefix|
32
31
  operator_assertions[country_prefix] = {}
33
32
  local_number = country_data["local_number"]
34
- default_assertions = {"country_code" => country_prefix}
33
+ default_assertions = { "country_code" => country_prefix }
35
34
  with_operator_data(country_id, options) do |operator, operator_data|
36
35
  default_operator_assertions = operator_data["assertions"].merge(
37
36
  "country_id" => country_id, "id" => operator
38
37
  ).merge(default_assertions)
39
- with_operator_area_codes(country_data, operator_data) do |area_code_prefix, area_code, area|
38
+ with_operator_area_codes(country_data, operator_data) do |area_code_prefix, area_code, _area|
40
39
  if area_code_prefix.is_a?(Hash)
41
40
  custom_local_number = area_code_prefix.values.first
42
41
  area_code_prefix = area_code_prefix.keys.first
@@ -58,7 +57,7 @@ module Torasup
58
57
  custom_local_number = prefix.values.first
59
58
  prefix = prefix.keys.first
60
59
  end
61
- prefix_assertions = operator_assertions[country_prefix][prefix] = {}
60
+ prefix_assertions = operator_assertions[country_prefix][prefix] = {}
62
61
  no_area_code_assertions = prefix_assertions[nil] = {}
63
62
 
64
63
  custom_local_number ||= local_number
@@ -84,21 +83,21 @@ module Torasup
84
83
  end
85
84
  end
86
85
 
87
- def with_pstn_data(options = {}, &block)
86
+ def with_pstn_data(options = {})
88
87
  pstn_data(options[:with_custom_pstn_data]).each do |country_id, country_data|
89
88
  next if options[:only_registered] && !options[:only_registered].include?(country_id)
90
89
  yield country_id, country_data, country_data["prefix"]
91
90
  end
92
91
  end
93
92
 
94
- def with_operator_data(country_id, options = {}, &block)
93
+ def with_operator_data(country_id, options = {})
95
94
  country_data(country_id, options[:with_custom_pstn_data])["operators"].each do |operator, operator_data|
96
95
  next if options[:only_registered] && !options[:only_registered][country_id].include?(operator)
97
96
  yield operator, operator_data
98
97
  end
99
98
  end
100
99
 
101
- def with_operator_area_codes(country_data, operator_data, &block)
100
+ def with_operator_area_codes(country_data, operator_data)
102
101
  (operator_data["area_code_prefixes"] || {}).each do |area_code_prefix|
103
102
  country_data["area_codes"].each do |area_code, area|
104
103
  yield area_code_prefix, area_code, area
@@ -106,7 +105,7 @@ module Torasup
106
105
  end
107
106
  end
108
107
 
109
- def with_operator_prefixes(operator_data, &block)
108
+ def with_operator_prefixes(operator_data)
110
109
  (operator_data["prefixes"] || {}).each do |prefix|
111
110
  yield prefix
112
111
  end
@@ -1,3 +1,3 @@
1
1
  module Torasup
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/torasup.rb CHANGED
@@ -1,7 +1,7 @@
1
- require 'yaml'
2
- require 'phony'
3
- require 'countries'
4
- require 'deep_merge/rails_compat'
1
+ require "yaml"
2
+ require "phony"
3
+ require "countries"
4
+ require "deep_merge/rails_compat"
5
5
 
6
6
  require "torasup/version"
7
7
  require "torasup/configuration"
@@ -10,12 +10,15 @@ require "torasup/operator"
10
10
  require "torasup/location"
11
11
 
12
12
  module Torasup
13
+ ALL_PREFIXES_KEYS = ["*", "all"].freeze
14
+ DEFAULT_PREFIXES = ("10".."99").to_a.freeze
15
+
13
16
  module Test
14
- autoload :Helpers, 'torasup/test/helpers'
17
+ autoload :Helpers, "torasup/test/helpers"
15
18
  end
16
19
 
17
20
  class << self
18
- def configure(&block)
21
+ def configure
19
22
  yield(configuration)
20
23
  end
21
24
 
@@ -28,7 +31,7 @@ module Torasup
28
31
  end
29
32
 
30
33
  def load_pstn_data!
31
- @pstn_data = load_yaml_file(File.join(File.dirname(__FILE__), 'torasup/data/pstn.yaml'))
34
+ @pstn_data = load_yaml_file(File.join(File.dirname(__FILE__), "torasup/data/pstn.yaml"))
32
35
  configuration.custom_pstn_data_files.compact.each do |pstn_data_file|
33
36
  @pstn_data.deeper_merge!(
34
37
  load_yaml_file(pstn_data_file)
@@ -38,7 +41,7 @@ module Torasup
38
41
  end
39
42
 
40
43
  def country_id(country_code)
41
- @international_dialing_codes[country_code].downcase if @international_dialing_codes[country_code]
44
+ @international_dialing_codes[country_code]&.downcase
42
45
  end
43
46
 
44
47
  def area_code(country_id, code)
@@ -115,7 +118,7 @@ module Torasup
115
118
  end
116
119
 
117
120
  def operator_metadata(country_id, operator)
118
- {"country_id" => country_id, "id" => operator}.merge(operator_data(country_id, operator)["metadata"] || {})
121
+ { "country_id" => country_id, "id" => operator }.merge(operator_data(country_id, operator)["metadata"] || {})
119
122
  end
120
123
 
121
124
  def operator_area_code_prefixes(country_id, operator)
@@ -125,7 +128,7 @@ module Torasup
125
128
  def prefix_defaults(country_properties, operator_properties, prefix_properties)
126
129
  defaults = {}
127
130
  prefix_type = prefix_properties["type"]
128
- [:min, :max, :pattern].each do |prefix_key|
131
+ %i[min max pattern].each do |prefix_key|
129
132
  result_key = "subscriber_number_#{prefix_key}"
130
133
  default_key = "default_#{prefix_type}_#{result_key}"
131
134
  result_value = prefix_properties[result_key] || operator_properties[default_key] || country_properties[default_key]
@@ -137,11 +140,13 @@ module Torasup
137
140
  def operator_mobile_prefixes(country_id, operator)
138
141
  full_prefixes = {}
139
142
  operator_data = operator_data(country_id, operator)
140
- mobile_prefixes = array_to_hash(operator_data["prefixes"] || [])
143
+ prefixes = DEFAULT_PREFIXES if operator_data["prefixes"].is_a?(String) && ALL_PREFIXES_KEYS.include?(operator_data["prefixes"])
144
+ prefixes ||= operator_data["prefixes"] || []
145
+ mobile_prefixes = array_to_hash(prefixes)
141
146
  mobile_prefixes.each do |mobile_prefix, prefix_metadata|
142
147
  full_prefixes[operator_full_prefix(country_id, mobile_prefix)] = {
143
148
  "type" => "mobile",
144
- "prefix" => mobile_prefix,
149
+ "prefix" => mobile_prefix
145
150
  }.merge(prefix_metadata)
146
151
  end
147
152
  full_prefixes
@@ -155,7 +160,7 @@ module Torasup
155
160
  operator_prefixes = operator_mobile_prefixes(country_id, operator)
156
161
  area_code_prefixes = array_to_hash(operator_area_code_prefixes(country_id, operator))
157
162
  area_code_prefixes.each do |operator_area_code_prefix, prefix_metadata|
158
- area_codes(country_id).each do |area_code, area|
163
+ area_codes(country_id).each do |area_code, _area|
159
164
  operator_prefixes[operator_full_prefix(country_id, area_code, operator_area_code_prefix)] = {
160
165
  "type" => "landline",
161
166
  "prefix" => operator_area_code_prefix,
@@ -167,7 +172,7 @@ module Torasup
167
172
  end
168
173
 
169
174
  def array_to_hash(array)
170
- array.map { |n| n.is_a?(Hash) ? n : {n => {}} }.reduce(Hash.new, :merge)
175
+ array.map { |n| n.is_a?(Hash) ? n : { n => {} } }.reduce({}, :merge)
171
176
  end
172
177
  end
173
178
 
@@ -17,3 +17,9 @@ kh:
17
17
  - '87'
18
18
  area_code_prefixes:
19
19
  - '45'
20
+ br:
21
+ operators:
22
+ mundivox:
23
+ metadata:
24
+ name: "Mundivox"
25
+ prefixes: "*"
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  module Torasup
4
4
  describe Operator do
@@ -11,7 +11,7 @@ module Torasup
11
11
  it "should return the operators with their metadata" do
12
12
  operators = Operator.send(method)
13
13
  operators = Operator.send(method) # run it twice to highlight the duplication problem
14
- with_operators do |number_parts, assertions|
14
+ with_operators do |_number_parts, assertions|
15
15
  operator = operators[assertions["country_id"]][assertions["id"]]
16
16
  expect(operator["country_id"]).to eq(assertions["country_id"])
17
17
  expect(operator["id"]).to eq(assertions["id"])
@@ -53,7 +53,7 @@ module Torasup
53
53
  let(:method) { :registered }
54
54
 
55
55
  def with_operators(&block)
56
- super(:only_registered => {sample_operator[0] => [sample_operator[1]]}, &block)
56
+ super(only_registered: { sample_operator[0] => [sample_operator[1]] }, &block)
57
57
  end
58
58
  end
59
59
  end
@@ -65,10 +65,10 @@ module Torasup
65
65
  subject = Operator.new(*number_parts)
66
66
  assertions.each do |method, assertion|
67
67
  args = []
68
- args << {:interpolation => nil} unless subject.respond_to?(method)
68
+ args << { interpolation: nil } unless subject.respond_to?(method)
69
69
  result = subject.send(method, *args)
70
70
  result_error = result.nil? ? "nil" : "'#{result}'"
71
- expect(result).to(eq(interpolated_assertion(assertion, :interpolation => nil)), "expected Operator.new('#{number_parts}').#{method} to return '#{assertion}' but got #{result_error}")
71
+ expect(result).to(eq(interpolated_assertion(assertion, interpolation: nil)), "expected Operator.new('#{number_parts}').#{method} to return '#{assertion}' but got #{result_error}")
72
72
  end
73
73
  end
74
74
  end
@@ -87,14 +87,19 @@ module Torasup
87
87
 
88
88
  context "with a single configuration file" do
89
89
  let(:configuration_options) { {} }
90
+ let(:options) { { with_custom_pstn_data: true } }
90
91
 
91
- it_should_behave_like "an operator" do
92
- let(:options) { { :with_custom_pstn_data => true } }
92
+ it_should_behave_like "an operator"
93
+
94
+ it "should handle wildcard prefixes" do
95
+ torasup_number = Torasup::PhoneNumber.new("5582999489999")
96
+ operator = torasup_number.operator
97
+ expect(operator.id).to eq("mundivox")
93
98
  end
94
99
  end
95
100
 
96
101
  context "with multiple configuration files" do
97
- let(:configuration_options) { {:multiple_files => true} }
102
+ let(:configuration_options) { { multiple_files: true } }
98
103
 
99
104
  def assert_overridden_data!
100
105
  torasup_number = Torasup::PhoneNumber.new("85515234567")
data/spec/torasup_spec.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require "spec_helper"
2
2
 
3
3
  describe Torasup do
4
4
  describe "#prefixes" do
@@ -6,7 +6,7 @@ describe Torasup do
6
6
 
7
7
  context "metadata" do
8
8
  it "should include the correct min, max and pattern values" do
9
- with_operators do |number_parts, assertions|
9
+ with_operators do |_number_parts, assertions|
10
10
  prefix = assertions["country_code"].to_s + assertions["area_code"].to_s + assertions["prefix"].to_s
11
11
  prefix_metadata = prefixes[prefix]
12
12
  local_number = assertions["local_number"]
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.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Wilkie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-01 00:00:00.000000000 Z
11
+ date: 2018-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: countries
@@ -118,6 +118,7 @@ extra_rdoc_files: []
118
118
  files:
119
119
  - ".gitignore"
120
120
  - ".rspec"
121
+ - ".rubocop.yml"
121
122
  - ".ruby-version"
122
123
  - ".travis.yml"
123
124
  - Gemfile
@@ -164,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
165
  version: '0'
165
166
  requirements: []
166
167
  rubyforge_project:
167
- rubygems_version: 2.6.13
168
+ rubygems_version: 2.7.3
168
169
  signing_key:
169
170
  specification_version: 4
170
171
  summary: '"Retuns metadata about a phone number such as operator, area code and more"'