cfita 0.0.5 → 0.0.6

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
2
  SHA256:
3
- metadata.gz: 79fb90478a1c2e761048d863091a60dff5ea118a48b68527ecc2df1fb849d05f
4
- data.tar.gz: cb18f19a6a571135d0a7c60963524c5af2d8f1d144283c64892eb93d957b47f8
3
+ metadata.gz: ab4e090398ad28be565fc5dc2470eb89f2671d23ecc9e7fc6692140616248d53
4
+ data.tar.gz: 8ac27e3e98e65c3dbd414cb3e1345ddb91f4ee36f3eebe333b3967fd4b1632b2
5
5
  SHA512:
6
- metadata.gz: 896d41364afed131d83b36d5de1a606cf5e635c4273486c6d9daf20f2e0f272b3577fcf9d6125827286f5be1c0d7463b5ec019b0aa4d53708865e49bff5eb2bb
7
- data.tar.gz: 6fed50fdd92d079305eb28a5578bcc1f6d496fe8ad6805f2195e933c728f8684eeb32f0e98bfc4a7a171f72db403e79319ba3bcedfa6ee9594be285e6e48c52e
6
+ metadata.gz: b7b62b6ca733a0b89aacbaffcfd12e4249ca02463c5b650d074d55284096aecfdf39af980fb26a9c4cbb3b546c5fbac9e8b969831e71d1bc4574a5c1901f821b
7
+ data.tar.gz: 524c5a1c6d6d849e939a507706db4ab5ad05177008fddf22538778e4e53a0327acffd3193a00b9f112722612b2bcd24e925b7f8489898f89d1f9cc6b9dadc515
data/cfita.gemspec CHANGED
@@ -3,8 +3,8 @@
3
3
  Gem::Specification.new do |s|
4
4
  s.required_ruby_version = '>= 2.4'
5
5
  s.name = 'cfita'
6
- s.version = '0.0.5'
7
- s.date = '2019-06-17'
6
+ s.version = '0.0.6'
7
+ s.date = '2019-06-18'
8
8
  s.summary = 'Italian fiscal code checker'
9
9
  s.description = 'Controllo codici fiscali italiani'
10
10
  s.authors = ['Stefano Savanelli']
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support/all'
4
- require 'cfita/codici_catastali'
4
+ require 'cfita/codici_catastali.rb'
5
5
 
6
6
  module Cfita
7
7
  # Controllo codice fiscale italiano
@@ -16,11 +16,18 @@ module Cfita
16
16
  @ccat ||= JSON.parse(open('ccat.json'))
17
17
  end
18
18
 
19
- def initialize(fiscal_code, birth_place: nil, birth_date: nil)
19
+ def initialize(
20
+ fiscal_code,
21
+ birth_place: nil,
22
+ birth_date: nil,
23
+ name: nil,
24
+ surname: nil
25
+ )
20
26
  @fiscal_code = fiscal_code.upcase.strip
21
27
  @birth_place = birth_place&.upcase
22
28
  @birth_date = birth_date && (birth_date.is_a?(Date) ? birth_date : Date.parse(birth_date))
23
- @data = {}
29
+ @name = name&.parameterize&.upcase
30
+ @surname = surname&.parameterize&.upcase
24
31
  @errors = []
25
32
  parse
26
33
  end
@@ -29,10 +36,8 @@ module Cfita
29
36
  fiscal_code
30
37
  end
31
38
 
32
- def valid?(birth_place: nil)
33
- result = errors.empty?
34
- result = birth_place?(birth_place) if result && birth_place
35
- result
39
+ def valid?
40
+ errors.empty?
36
41
  end
37
42
 
38
43
  private
@@ -45,10 +50,48 @@ module Cfita
45
50
  check_checksum
46
51
  return if errors.any?
47
52
 
53
+ check_name
54
+ check_surname
48
55
  check_birth_date
49
56
  check_birth_place
50
57
  end
51
58
 
59
+ def check_name
60
+ return unless @name
61
+
62
+ a, b, c, d = consonants(@name)
63
+ name_code = (
64
+ (d ? [a, c, d] : [a, b, c]).compact.join +
65
+ vowels(@name).join +
66
+ 'XXX'
67
+ )[0..2]
68
+
69
+ errors << "Il nome non corrisponde al codice '#{name_code}'" unless name_code == @fiscal_code[3..5]
70
+ end
71
+
72
+ def check_surname
73
+ return unless @surname
74
+
75
+ surname_code = (
76
+ consonants(@surname).join +
77
+ vowels(@surname).join +
78
+ 'XXX'
79
+ )[0..2]
80
+
81
+ errors << "Il cognome non corrisponde al codice '#{surname_code}'" unless surname_code == @fiscal_code[0..2]
82
+ end
83
+
84
+ VOWELS = 'AEIOU'.chars.freeze
85
+ CONSONANTS = (('A'..'Z').to_a - VOWELS).freeze
86
+
87
+ def vowels(word)
88
+ word.chars.select { |char| char.in? VOWELS }
89
+ end
90
+
91
+ def consonants(word)
92
+ word.chars.select { |char| char.in? CONSONANTS }
93
+ end
94
+
52
95
  def check_size
53
96
  size = @fiscal_code.size
54
97
  errors << "Lunghezza errata (#{size})" unless size == 16
@@ -101,7 +144,7 @@ module Cfita
101
144
  end
102
145
  end
103
146
 
104
- MESI = 'ABCDEHLMPRST'.freeze
147
+ MESI = 'ABCDEHLMPRST'
105
148
 
106
149
  def check_birth_date
107
150
  yy = cifre(6..7)
@@ -129,9 +172,9 @@ module Cfita
129
172
  end
130
173
  end
131
174
 
132
- def yy2yyyy(yy)
175
+ def yy2yyyy(year_as_yy)
133
176
  Date.today.year -
134
- (Date.today.year % 100 + 100 - yy ) % 100
177
+ (Date.today.year % 100 + 100 - year_as_yy) % 100
135
178
  end
136
179
 
137
180
  def cifre(range)