kontact 0.2.0 → 0.4.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 +4 -4
- data/lib/kontact/brazil.rb +25 -10
- data/lib/kontact/version.rb +1 -1
- data/lib/kontact.rb +28 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3238d535c1065f7c98282b6ddbd121b261f8aa915cb4f92ecccf644e7ad9637d
|
4
|
+
data.tar.gz: bc3d99070aa60d3fd9c9afb839f726bdc4e5a2b576c7b9d2a1f27d8392e108b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 78a769a6c43855a3e0595a104f3b7f9e3d9d5435054141b2a89b6100cc2c74dfdd763732d7f8160d402e4b9c182e64e9c205684b5f3b60c67ae42b0b70f00a3c
|
7
|
+
data.tar.gz: 904cd9d5a24afdaed88ebb53b09796cb915818a0e4426d8579ee6520a7a3326123b3d9c82783895aceee58a56a05f46c5f4541386016df9c3402f86c17668598
|
data/lib/kontact/brazil.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
module Kontact
|
2
2
|
module Brazil
|
3
|
-
|
3
|
+
DDD = %w[
|
4
4
|
11 12 13 14 15 16 17 18 19
|
5
5
|
21 22 24
|
6
6
|
27 28
|
@@ -15,10 +15,10 @@ module Kontact
|
|
15
15
|
79
|
16
16
|
81 82 83 84 85 86 87 88 89
|
17
17
|
91 92 93 94 95 96 97 98 99
|
18
|
-
]
|
18
|
+
].freeze
|
19
19
|
|
20
20
|
def self.generate(type = :mobile)
|
21
|
-
ddd =
|
21
|
+
ddd = DDD.sample
|
22
22
|
number = case type
|
23
23
|
when :mobile
|
24
24
|
"9#{rand(1000..9999)}-#{rand(1000..9999)}"
|
@@ -31,16 +31,31 @@ module Kontact
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def self.valid?(number)
|
34
|
-
|
35
|
-
return false
|
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\-+()]/)
|
36
42
|
|
37
|
-
|
38
|
-
prefix = match[2]
|
39
|
-
return false unless DDDs.include?(ddd)
|
43
|
+
digits = number.gsub(/\D/, "")
|
40
44
|
|
41
|
-
|
45
|
+
return false unless digits.start_with?("55")
|
46
|
+
|
47
|
+
digits = digits[2..]
|
48
|
+
|
49
|
+
return false unless [10, 11].include?(digits.length)
|
50
|
+
|
51
|
+
ddd = digits[0..1]
|
52
|
+
prefix = digits[2..-5]
|
53
|
+
|
54
|
+
return false unless DDD.include?(ddd)
|
55
|
+
|
56
|
+
if digits.length == 11
|
42
57
|
prefix.start_with?("9")
|
43
|
-
elsif
|
58
|
+
elsif digits.length == 10
|
44
59
|
prefix[0].to_i.between?(2, 5)
|
45
60
|
else
|
46
61
|
false
|
data/lib/kontact/version.rb
CHANGED
data/lib/kontact.rb
CHANGED
@@ -3,7 +3,7 @@ require_relative "kontact/brazil"
|
|
3
3
|
require_relative "kontact/usa"
|
4
4
|
|
5
5
|
module Kontact
|
6
|
-
def self.generate(country, type = :mobile)
|
6
|
+
def self.generate(country = detect_country, type = :mobile)
|
7
7
|
case country.to_sym
|
8
8
|
when :brazil
|
9
9
|
Brazil.generate(type)
|
@@ -13,4 +13,31 @@ module Kontact
|
|
13
13
|
raise ArgumentError, "Unsupported country: #{country}"
|
14
14
|
end
|
15
15
|
end
|
16
|
+
|
17
|
+
def self.valid?(number, country = detect_country)
|
18
|
+
raise ArgumentError, "Number can not empty or blank" unless number
|
19
|
+
|
20
|
+
case country.to_sym
|
21
|
+
when :brazil
|
22
|
+
Brazil.valid?(number)
|
23
|
+
when :usa
|
24
|
+
USA.valid?(number)
|
25
|
+
else
|
26
|
+
raise ArgumentError, "Unsupported country: #{country}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def self.detect_country
|
33
|
+
locale = ENV["LANG"] || ENV["LC_ALL"] || ENV["LC_MESSAGES"] || ""
|
34
|
+
case locale.downcase
|
35
|
+
when /pt[-_]br/
|
36
|
+
:brazil
|
37
|
+
when /en[-_]us/
|
38
|
+
:usa
|
39
|
+
else
|
40
|
+
:brazil # as a main fallback
|
41
|
+
end
|
42
|
+
end
|
16
43
|
end
|