br_documents 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +4 -0
  5. data/README.md +146 -0
  6. data/Rakefile +1 -0
  7. data/br_documents.gemspec +30 -0
  8. data/config/locales/pt-BR.yml +8 -0
  9. data/lib/br_documents.rb +4 -0
  10. data/lib/br_documents/cnpj_cpf/cnpj.rb +21 -0
  11. data/lib/br_documents/cnpj_cpf/cpf.rb +21 -0
  12. data/lib/br_documents/cnpj_cpf/document.rb +35 -0
  13. data/lib/br_documents/cnpj_validator.rb +8 -0
  14. data/lib/br_documents/commons/mod11.rb +20 -0
  15. data/lib/br_documents/cpf_validator.rb +8 -0
  16. data/lib/br_documents/ie/ac.rb +17 -0
  17. data/lib/br_documents/ie/al.rb +17 -0
  18. data/lib/br_documents/ie/am.rb +17 -0
  19. data/lib/br_documents/ie/ap.rb +52 -0
  20. data/lib/br_documents/ie/ba.rb +75 -0
  21. data/lib/br_documents/ie/base.rb +17 -0
  22. data/lib/br_documents/ie/ce.rb +12 -0
  23. data/lib/br_documents/ie/df.rb +17 -0
  24. data/lib/br_documents/ie/es.rb +12 -0
  25. data/lib/br_documents/ie/factory.rb +12 -0
  26. data/lib/br_documents/ie/go.rb +17 -0
  27. data/lib/br_documents/ie/ma.rb +12 -0
  28. data/lib/br_documents/ie/mg.rb +53 -0
  29. data/lib/br_documents/ie/ms.rb +12 -0
  30. data/lib/br_documents/ie/mt.rb +18 -0
  31. data/lib/br_documents/ie/pa.rb +17 -0
  32. data/lib/br_documents/ie/pattern1.rb +26 -0
  33. data/lib/br_documents/ie/pattern2.rb +30 -0
  34. data/lib/br_documents/ie/pb.rb +12 -0
  35. data/lib/br_documents/ie/pe.rb +51 -0
  36. data/lib/br_documents/ie/pi.rb +12 -0
  37. data/lib/br_documents/ie/pr.rb +19 -0
  38. data/lib/br_documents/ie/rj.rb +18 -0
  39. data/lib/br_documents/ie/rn.rb +47 -0
  40. data/lib/br_documents/ie/ro.rb +49 -0
  41. data/lib/br_documents/ie/rr.rb +23 -0
  42. data/lib/br_documents/ie/rs.rb +18 -0
  43. data/lib/br_documents/ie/sc.rb +17 -0
  44. data/lib/br_documents/ie/se.rb +12 -0
  45. data/lib/br_documents/ie/sp.rb +55 -0
  46. data/lib/br_documents/ie/to.rb +18 -0
  47. data/lib/br_documents/ie_validator.rb +50 -0
  48. data/lib/br_documents/version.rb +3 -0
  49. data/spec/cnpj_cpf/cnpj_spec.rb +45 -0
  50. data/spec/cnpj_cpf/cpf_spec.rb +45 -0
  51. data/spec/cnpj_validator_spec.rb +36 -0
  52. data/spec/cpf_validator_spec.rb +36 -0
  53. data/spec/ie/ac_spec.rb +40 -0
  54. data/spec/ie/al_spec.rb +33 -0
  55. data/spec/ie/am_spec.rb +40 -0
  56. data/spec/ie/ap_spec.rb +33 -0
  57. data/spec/ie/ba_spec.rb +63 -0
  58. data/spec/ie/ce_spec.rb +13 -0
  59. data/spec/ie/df_spec.rb +40 -0
  60. data/spec/ie/es_spec.rb +13 -0
  61. data/spec/ie/factory_spec.rb +203 -0
  62. data/spec/ie/go_spec.rb +40 -0
  63. data/spec/ie/ma_spec.rb +13 -0
  64. data/spec/ie/mg_spec.rb +40 -0
  65. data/spec/ie/ms_spec.rb +13 -0
  66. data/spec/ie/mt_spec.rb +40 -0
  67. data/spec/ie/pa_spec.rb +40 -0
  68. data/spec/ie/pb_spec.rb +13 -0
  69. data/spec/ie/pe_spec.rb +59 -0
  70. data/spec/ie/pi_spec.rb +13 -0
  71. data/spec/ie/pr_spec.rb +33 -0
  72. data/spec/ie/rj_spec.rb +40 -0
  73. data/spec/ie/rn_spec.rb +59 -0
  74. data/spec/ie/ro_spec.rb +59 -0
  75. data/spec/ie/rr_spec.rb +40 -0
  76. data/spec/ie/rs_spec.rb +38 -0
  77. data/spec/ie/sc_spec.rb +40 -0
  78. data/spec/ie/se_spec.rb +13 -0
  79. data/spec/ie/shared_examples_for_pattern1.rb +33 -0
  80. data/spec/ie/sp_spec.rb +42 -0
  81. data/spec/ie/to_spec.rb +40 -0
  82. data/spec/ie_validator_spec.rb +65 -0
  83. data/spec/spec_helper.rb +21 -0
  84. data/spec/support/utilities.rb +3 -0
  85. metadata +248 -0
@@ -0,0 +1,75 @@
1
+ require_relative "base"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class BA < Base
6
+ include Commons::Mod11
7
+
8
+ private
9
+ def format_ie(number)
10
+ if number.gsub(/-/, "").length == 8
11
+ number.sub(/(\d{6})(\d{2})/, "\\1-\\2")
12
+ else
13
+ number.sub(/(\d{7})(\d{2})/, "\\1-\\2")
14
+ end
15
+ end
16
+
17
+ def valid_format?
18
+ valid_8_digits_format or valid_9_digits_format
19
+ end
20
+
21
+ def valid_9_digits_format
22
+ regex = /^(\d{6}\-\d{2})$|^(\d{8})$/
23
+ regex.match(@number).present?
24
+ end
25
+
26
+ def valid_8_digits_format
27
+ regex = /^(\d{7}\-\d{2})$|^(\d{9})$/
28
+ regex.match(@number).present?
29
+ end
30
+
31
+ def valid_digital_check?
32
+ @number.gsub!(/-/, "")
33
+
34
+ weight1 = make_weight(total_size)
35
+ weight2 = make_weight(total_size-1)
36
+
37
+ digital_check2 = digital_check_generator(@number, weight2)
38
+ number = "#{@number[0, body_size]}#{digital_check2}"
39
+ digital_check1 = digital_check_generator(number, weight1)
40
+
41
+ @number[-2, 2].eql? "#{digital_check1}#{digital_check2}"
42
+ end
43
+
44
+ def make_weight(size)
45
+ weight = []
46
+ size.downto(2) { |w| weight << w }
47
+
48
+ weight
49
+ end
50
+
51
+ def total_size
52
+ @number.gsub(/-/, "").length
53
+ end
54
+
55
+ def body_size
56
+ digital_check_size = 2
57
+ total_size - digital_check_size
58
+ end
59
+
60
+ def digital_check_generator(number, weight)
61
+ if @number[0].to_i <= 5 or @number[0].to_i == 8
62
+ generate_digital_check_mod10(number, weight)
63
+ else
64
+ generate_digital_check(number, weight)
65
+ end
66
+ end
67
+
68
+ def generate_digital_check_mod10(values, weights)
69
+ sum = reduce_weights(values, weights)
70
+ mod = sum % 10
71
+ mod == 0 ? 0 : (10 - mod)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,17 @@
1
+ module BrDocuments
2
+ module IE
3
+ class Base
4
+ def initialize(number)
5
+ @number = number
6
+ end
7
+
8
+ def valid?
9
+ valid_format? and valid_digital_check?
10
+ end
11
+
12
+ def formatted
13
+ format_ie(@number)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "pattern1"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class CE < Pattern1
6
+ private
7
+ def format_ie(number)
8
+ number.sub(/(\d{8})(\d{1})/, "\\1-\\2")
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ require_relative "pattern2"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class DF < Pattern2
6
+ def initialize(number)
7
+ super
8
+ @mask = /^(\d{11}\-\d{2})$|^(\d{13})$/
9
+ end
10
+
11
+ private
12
+ def format_ie(number)
13
+ number.sub(/(\d{11})(\d{2})/, "\\1-\\2")
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "pattern1"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class ES < Pattern1
6
+ private
7
+ def format_ie(number)
8
+ number
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module BrDocuments
2
+ module IE
3
+ class Factory
4
+ def self.create(uf, number)
5
+ klass = BrDocuments::IE.const_get("#{uf}")
6
+ klass.new(number)
7
+ rescue NameError
8
+ raise(ArgumentError, I18n.t("validator.ie.uf.invalid"))
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ require_relative "pattern1"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class GO < Pattern1
6
+ def initialize(number)
7
+ super
8
+ @mask = /^(\d{2}\.\d{3}\.\d{3}\-\d{1})$|^(\d{9})$/
9
+ end
10
+
11
+ private
12
+ def format_ie(number)
13
+ number.sub(/(\d{2})(\d{3})(\d{3})(\d{1})/, "\\1.\\2.\\3-\\4")
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "pattern1"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class MA < Pattern1
6
+ private
7
+ def format_ie(number)
8
+ number
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,53 @@
1
+ require_relative "base"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class MG < Base
6
+ include Commons::Mod11
7
+
8
+ private
9
+ def format_ie(number)
10
+ number.sub(/(\d{3})(\d{3})(\d{3})(\d{4})/, "\\1.\\2.\\3/\\4")
11
+ end
12
+
13
+ def valid_format?
14
+ regex = /^(\d{3}\.\d{3}\.\d{3}\.\d{2}\-\d{2})$|^(\d{13})$/
15
+ regex.match(@number).present?
16
+ end
17
+
18
+ def valid_digital_check?
19
+ @number.gsub!(/-/, "")
20
+
21
+ number1 = @number[0, 3] + "0" + @number[3..10]
22
+ weight1 = [1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
23
+ digital_check1 = generate_first_digital_check(number1, weight1)
24
+
25
+ number2 = "#{@number[0, 11]}#{digital_check1}"
26
+ weight2 = [3, 2, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
27
+ digital_check2 = generate_digital_check(number2, weight2)
28
+
29
+ @number[-2, 2].eql? "#{digital_check1}#{digital_check2}"
30
+ end
31
+
32
+ def generate_first_digital_check(values, weights)
33
+ sum = 0
34
+ weights.each_index do |i|
35
+ number = weights[i] * values[i].to_i
36
+ if number < 10
37
+ sum += number
38
+ else
39
+ sum += number.to_s[0].to_i + number.to_s[1].to_i
40
+ end
41
+ end
42
+ next_dozen(sum) - sum
43
+ end
44
+
45
+ def next_dozen(number)
46
+ while number % 10 != 0
47
+ number = number + 1
48
+ end
49
+ number
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "pattern1"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class MS < Pattern1
6
+ private
7
+ def format_ie(number)
8
+ number
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ require_relative "pattern1"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class MT < Pattern1
6
+ def initialize(number)
7
+ super
8
+ @mask = /^(\d{10}\-\d{1})$|^(\d{11})$/
9
+ @weight = [3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
10
+ end
11
+
12
+ private
13
+ def format_ie(number)
14
+ number.sub(/(\d{10})(\d{1})/, "\\1-\\2")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ require_relative "pattern1"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class PA < Pattern1
6
+ def initialize(number)
7
+ super
8
+ @mask = /^(\d{2}\-\d{6}\-\d{1})$|^(\d{9})$/
9
+ end
10
+
11
+ private
12
+ def format_ie(number)
13
+ number.sub(/(\d{2})(\d{6})(\d{1})/, "\\1-\\2-\\3")
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,26 @@
1
+ require_relative "base"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class Pattern1 < Base
6
+ include Commons::Mod11
7
+
8
+ def initialize(number)
9
+ super
10
+ @mask = /^(\d{8}\-\d{1})$|^(\d{9})$/
11
+ @weight = [9, 8, 7, 6, 5, 4, 3, 2]
12
+ end
13
+
14
+ private
15
+ def valid_format?
16
+ regex = @mask
17
+ regex.match(@number).present?
18
+ end
19
+
20
+ def valid_digital_check?
21
+ @number.gsub!(/[\.\/-]/, "")
22
+ @number[-1].eql? generate_digital_check(@number, @weight).to_s
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,30 @@
1
+ require_relative "base"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class Pattern2 < Base
6
+ include Commons::Mod11
7
+
8
+ def initialize(number)
9
+ super
10
+ @weight1 = [4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
11
+ @weight2 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
12
+ end
13
+
14
+ private
15
+ def valid_format?
16
+ regex = @mask
17
+ regex.match(@number).present?
18
+ end
19
+
20
+ def valid_digital_check?
21
+ @number.gsub!(/-/, "")
22
+
23
+ digital_check1 = generate_digital_check(@number, @weight1)
24
+ digital_check2 = generate_digital_check(@number, @weight2)
25
+
26
+ @number[-2, 2].eql? "#{digital_check1}#{digital_check2}"
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "pattern1"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class PB < Pattern1
6
+ private
7
+ def format_ie(number)
8
+ number.sub(/(\d{8})(\d{1})/, "\\1-\\2")
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,51 @@
1
+ require_relative "pattern1"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class PE
6
+ def initialize(number)
7
+ if number.gsub(/-/, "").length <= 9
8
+ @validator = PE9.new(number)
9
+ else
10
+ @validator = PE14.new(number)
11
+ end
12
+ end
13
+
14
+ def valid?
15
+ @validator.valid?
16
+ end
17
+
18
+ def formatted
19
+ @validator.formatted
20
+ end
21
+
22
+ private
23
+ class PE14 < Pattern1
24
+ def initialize(number)
25
+ super
26
+ @mask = /^(\d{2}\.\d{1}\.\d{3}\.\d{7})$|^(\d{14})$/
27
+ @weight = [5, 4, 3, 2, 1, 9, 8, 7, 6, 5, 4, 3, 2]
28
+ end
29
+
30
+ private
31
+ def format_ie(number)
32
+ number.sub(/(\d{2})(\d{1})(\d{3})(\d{7})(\d{1})/, "\\1.\\2.\\3.\\4-\\5")
33
+ end
34
+ end
35
+
36
+ class PE9 < Pattern2
37
+ def initialize(number)
38
+ super
39
+ @mask = /^(\d{7}\-\d{2})$|^(\d{9})$/
40
+ @weight1 = [8, 7, 6, 5, 4, 3, 2]
41
+ @weight2 = [9, 8, 7, 6, 5, 4, 3, 2]
42
+ end
43
+
44
+ private
45
+ def format_ie(number)
46
+ number.sub(/(\d{7})(\d{2})/, "\\1-\\2")
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "pattern1"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class PI < Pattern1
6
+ private
7
+ def format_ie(number)
8
+ number
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ require_relative "pattern2"
2
+
3
+ module BrDocuments
4
+ module IE
5
+ class PR < Pattern2
6
+ def initialize(number)
7
+ super
8
+ @mask = /^(\d{8}-\d{2})$|^(\d{10})$/
9
+ @weight1 = [3, 2, 7, 6, 5, 4, 3, 2]
10
+ @weight2 = [4, 3, 2, 7, 6, 5, 4, 3, 2]
11
+ end
12
+
13
+ private
14
+ def format_ie(number)
15
+ number.sub(/(\d{8})(\d{2})/, "\\1-\\2")
16
+ end
17
+ end
18
+ end
19
+ end