kontact 0.4.0 → 0.5.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3238d535c1065f7c98282b6ddbd121b261f8aa915cb4f92ecccf644e7ad9637d
4
- data.tar.gz: bc3d99070aa60d3fd9c9afb839f726bdc4e5a2b576c7b9d2a1f27d8392e108b0
3
+ metadata.gz: 16404ff6239dd1e82e910bd4005283bbeae32ebe1d0bb8f208732f9fd521ae64
4
+ data.tar.gz: 0d33957070f27bc246722299d37275ad198f18628b48fbf13832755b8a2c771d
5
5
  SHA512:
6
- metadata.gz: 78a769a6c43855a3e0595a104f3b7f9e3d9d5435054141b2a89b6100cc2c74dfdd763732d7f8160d402e4b9c182e64e9c205684b5f3b60c67ae42b0b70f00a3c
7
- data.tar.gz: 904cd9d5a24afdaed88ebb53b09796cb915818a0e4426d8579ee6520a7a3326123b3d9c82783895aceee58a56a05f46c5f4541386016df9c3402f86c17668598
6
+ metadata.gz: 5a858b95baaaa1521eb34cc5cc6346ba666b3a8f63d4c41c8483dbe090537ba349f5df2ee9e2b42359d4bc6f5a4d3c6e2116ecbdcda9e34207da819a724c91aa
7
+ data.tar.gz: c30309d06aa9c307e19093363ed7350d4c7593b8b43a1c775a9f6fc8e5c0aa6f392c50af407564934e60e758cdc8889baf174bbb234fa8c86ed4fa1bf180cf1a
@@ -17,35 +17,40 @@ module Kontact
17
17
  91 92 93 94 95 96 97 98 99
18
18
  ].freeze
19
19
 
20
- def self.generate(type = :mobile)
20
+ PATTERN = [
21
+ /\A\+55 \((\d{2})\) 9\d{4}-\d{4}\z/,
22
+ /\A\((\d{2})\) 9\d{4}-\d{4}\z/,
23
+ /\A\+55 \((\d{2})\) [2-5]\d{3}-\d{4}\z/,
24
+ /\A\((\d{2})\) [2-5]\d{3}-\d{4}\z/,
25
+ /\A\+55(\d{2})9\d{8}\z/,
26
+ /\A\+55(\d{2})[2-5]\d{7}\z/
27
+ ].freeze
28
+
29
+ def self.generate(type: :mobile, formatted: false)
21
30
  ddd = DDD.sample
22
31
  number = case type
23
32
  when :mobile
24
- "9#{rand(1000..9999)}-#{rand(1000..9999)}"
33
+ "9#{rand(1000..9999)}#{formatted ? "-" : ""}#{rand(1000..9999)}"
25
34
  when :landline
26
- "#{rand(2000..5999)}-#{rand(1000..9999)}"
35
+ "#{rand(2000..5999)}#{formatted ? "-" : ""}#{rand(1000..9999)}"
27
36
  else
28
37
  raise ArgumentError, "Invalid type: #{type}"
29
38
  end
30
- "+55 #{ddd} #{number}"
39
+ formatted ? "+55 (#{ddd}) #{number}" : "+55#{ddd}#{number}"
31
40
  end
32
41
 
33
42
  def self.valid?(number)
34
- return false unless number.match?(/\A\+?55[ ()\d-]+\z/)
35
- return false if number.include?("--") || number.include?("++") || number.include?("-+")
36
- return false unless number.match?(/\A[\d\s\-+()]+\z/)
37
- return false if number.count("+") > 1
38
- return false if number.count("-") > 1
39
- return false if number.count("(") > 1
40
- return false if number.count(")") > 1
41
- return false if number.match?(/[^\d\s\-+()]/)
42
-
43
- digits = number.gsub(/\D/, "")
43
+ raise ArgumentError, "Number can not empty or blank" unless number
44
44
 
45
- return false unless digits.start_with?("55")
45
+ number = number.strip
46
46
 
47
- digits = digits[2..]
47
+ PATTERN.any? do |regex|
48
+ match = number.match(regex)
49
+ match && DDD.include?(match[1])
50
+ end
48
51
 
52
+ digits = number.gsub(/\D/, "")
53
+ digits = digits[2..] if digits.start_with?("55")
49
54
  return false unless [10, 11].include?(digits.length)
50
55
 
51
56
  ddd = digits[0..1]
data/lib/kontact/usa.rb CHANGED
@@ -45,12 +45,17 @@ module Kontact
45
45
  941 947 948 949 951 952 954 956 957
46
46
  959 970 971 972 973 975 976 978 979
47
47
  980 984 985 986 989
48
+ ].freeze
49
+
50
+ PATTERN = [
51
+ /\A\+1 \((\d{3})\) (\d{3})-(\d{4})\z/, # formatado
52
+ /\A\+1(\d{3})(\d{3})(\d{4})\z/ # compacto
48
53
  ]
49
54
 
50
- def self.generate(_type = :mobile)
55
+ def self.generate(formatted: false)
51
56
  area = AREAS.sample
52
57
  line_number = rand(1000..9999)
53
- "+1 #{area} #{valid_prefix}-#{line_number}"
58
+ formatted ? "+1 (#{area}) #{valid_prefix}-#{line_number}" : "+1#{area}#{valid_prefix}#{line_number}"
54
59
  end
55
60
 
56
61
  def self.valid_prefix
@@ -61,16 +66,16 @@ module Kontact
61
66
  end
62
67
 
63
68
  def self.valid?(number)
64
- match = number.match(/\A\+1 (\d{3}) (\d{3})-(\d{4})\z/)
65
- return false unless match
69
+ raise ArgumentError, "Number can not be empty or blank" if number.nil? || number.strip.empty?
66
70
 
67
- area = match[1]
68
- prefix = match[2].to_i
69
- return false unless AREAS.include?(area)
70
- return false if [911, 411, 555].include?(prefix)
71
- return false unless prefix.to_s[0].match?(/[2-9]/)
71
+ PATTERN.any? do |regex|
72
+ match = number.match(regex)
73
+ next false unless match
72
74
 
73
- true
75
+ area = match[1]
76
+ prefix = match[2].to_i
77
+ AREAS.include?(area) && ![911, 411, 555].include?(prefix) && prefix.to_s[0].match?(/[2-9]/)
78
+ end
74
79
  end
75
80
  end
76
81
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kontact
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/kontact.rb CHANGED
@@ -3,18 +3,20 @@ require_relative "kontact/brazil"
3
3
  require_relative "kontact/usa"
4
4
 
5
5
  module Kontact
6
- def self.generate(country = detect_country, type = :mobile)
6
+ def self.generate(country: :brazil, type: :mobile, formatted: false)
7
+ validate_country(country)
7
8
  case country.to_sym
8
9
  when :brazil
9
- Brazil.generate(type)
10
+ Brazil.generate(type: type, formatted: formatted)
10
11
  when :usa
11
- USA.generate(type)
12
+ USA.generate(formatted: formatted)
12
13
  else
13
14
  raise ArgumentError, "Unsupported country: #{country}"
14
15
  end
15
16
  end
16
17
 
17
- def self.valid?(number, country = detect_country)
18
+ def self.valid?(number, country: :brazil)
19
+ validate_country(country)
18
20
  raise ArgumentError, "Number can not empty or blank" unless number
19
21
 
20
22
  case country.to_sym
@@ -40,4 +42,15 @@ module Kontact
40
42
  :brazil # as a main fallback
41
43
  end
42
44
  end
45
+
46
+ def self.validate_country(country)
47
+ case country.to_sym
48
+ when :brazil
49
+ :brazil
50
+ when :usa
51
+ :usa
52
+ else
53
+ raise ArgumentError, "Unsupported country: #{country}"
54
+ end
55
+ end
43
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kontact
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thiago Chirana