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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3505c87c0825dc2fac77e7dd325deff7c648fa285bfdc7bee6b20adec7150764
4
- data.tar.gz: 5551cb45c9d2578194830f64e2748c9de66cf43cfba94ef5792653826a355e69
3
+ metadata.gz: 3238d535c1065f7c98282b6ddbd121b261f8aa915cb4f92ecccf644e7ad9637d
4
+ data.tar.gz: bc3d99070aa60d3fd9c9afb839f726bdc4e5a2b576c7b9d2a1f27d8392e108b0
5
5
  SHA512:
6
- metadata.gz: 3d2aa78422c30f3c99ceea04d011f5b0565d7e75b449a2fbc3d227ef4e0b895c118571f7120dc5e192a75350d251d7ae0b06c5eaa5123554041afd75a129a5ca
7
- data.tar.gz: 143d962e2e6c40737b91669ae311021ac388a5fa9c8226093ecff6e0139023ee74df022ea41828184bc3a91e4294f8435ad2890bb2814f22c5ef010eed66cb12
6
+ metadata.gz: 78a769a6c43855a3e0595a104f3b7f9e3d9d5435054141b2a89b6100cc2c74dfdd763732d7f8160d402e4b9c182e64e9c205684b5f3b60c67ae42b0b70f00a3c
7
+ data.tar.gz: 904cd9d5a24afdaed88ebb53b09796cb915818a0e4426d8579ee6520a7a3326123b3d9c82783895aceee58a56a05f46c5f4541386016df9c3402f86c17668598
@@ -1,6 +1,6 @@
1
1
  module Kontact
2
2
  module Brazil
3
- DDDs = %w[
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 = DDDs.sample
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
- match = number.match(/\A\+55 (\d{2}) (\d{4,5})-(\d{4})\z/)
35
- return false unless match
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
- ddd = match[1]
38
- prefix = match[2]
39
- return false unless DDDs.include?(ddd)
43
+ digits = number.gsub(/\D/, "")
40
44
 
41
- if prefix.length == 5
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 prefix.length == 4
58
+ elsif digits.length == 10
44
59
  prefix[0].to_i.between?(2, 5)
45
60
  else
46
61
  false
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Kontact
4
- VERSION = "0.2.0"
4
+ VERSION = "0.4.0"
5
5
  end
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
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.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thiago Chirana