cfita 0.0.1 → 0.0.2

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: 4f8494a35ca96f3627c796b353ee4921eac293b15648161fb6fda3d8865b79d7
4
- data.tar.gz: 3f4d8759989ae4c6f45b011b841dfdfa2f9a200f2bc6d628bd939ef9f310a417
3
+ metadata.gz: 6000d09a1d72bf1f7138f063310857187f8ff248a2d3b753336e03952dcd4dc5
4
+ data.tar.gz: 3a0dfb634d4c8654eb22cd2f5b121bed83f71ff540e2ed0d13ec7137db165f95
5
5
  SHA512:
6
- metadata.gz: 9f36da75cd909de4d9d8a0d10f2693264b90672a4762d12e5f3e3404c74da7046fa4e8aa66ba071e3edfc0549dc22ae460988b96a9a35428b4ebc4b320c960c9
7
- data.tar.gz: 2c4fa36c314841e41a758330d3b730311a96408f58a7373a0b334d2e2024c5eee4eea556efd8dd1de6a1ea0dbc640fff66bf2815ffb017d44c419dd34e523cea
6
+ metadata.gz: 6635c4f248bdece2be0c10cdb3f20ee3635b161c2a9ee4daafd51c0d08b7b7d9a6b195e0c4681d7ca3bdd99279dfab944aa97a22ccb67b9c8bd3f430753bb28f
7
+ data.tar.gz: 6af7e7120a4a70ce72737ed3139e1a7ce28fd97d3922b2b759095a30effbd5e5f122f64f505fdb957042e305d409b0efaed42d29b72161a677d071d16ebbc6e2
data/README.md CHANGED
@@ -1,3 +1,9 @@
1
1
  # Cfita
2
2
 
3
3
  Check italian fiscal code
4
+
5
+ see:
6
+ https://www.agenziaentrate.gov.it/wps/content/Nsilib/Nsi/Schede/Istanze/Richiesta+TS_CF/Informazioni+codificazione+pf
7
+
8
+
9
+ today_year = 1965 ; (0..99).each {|year| p [year, [1800,1900,2000].map {|mill| today_year - (mill + year)}.reject{|x| x < 14 }.min ].flatten }
data/cfita.gemspec CHANGED
@@ -3,7 +3,7 @@
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.1'
6
+ s.version = '0.0.2'
7
7
  s.date = '2019-05-29'
8
8
  s.summary = 'Italian fiscal code checker'
9
9
  s.description = 'Controllo codici fiscali italiani'
@@ -1,21 +1,27 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support/all'
4
+ # require './belfiore.rb'
4
5
 
5
6
  module Cfita
6
7
  # Controllo codice fiscale italiano
7
8
  class CodiceFiscale
8
- attr_reader :codice_fiscale, :data, :errors, :sesso
9
-
10
- def initialize(codice_fiscale)
11
- @codice_fiscale = codice_fiscale.upcase.strip
9
+ attr_reader :fiscal_code,
10
+ :sex,
11
+ :birth_place,
12
+ :birth_date,
13
+ :data,
14
+ :errors
15
+
16
+ def initialize(fiscal_code)
17
+ @fiscal_code = fiscal_code.upcase.strip
12
18
  @data = {}
13
19
  @errors = []
14
20
  parse
15
21
  end
16
22
 
17
23
  def to_s
18
- codice_fiscale
24
+ fiscal_code
19
25
  end
20
26
 
21
27
  def valid?
@@ -32,34 +38,97 @@ module Cfita
32
38
  check_checksum
33
39
  return if errors.any?
34
40
 
35
- check_sex
41
+ check_birth_date
42
+ # check_birth_place
36
43
  end
37
44
 
38
45
  def check_size
39
- size = @codice_fiscale.size
46
+ size = @fiscal_code.size
40
47
  errors << "Lunghezza errata (#{size})" unless size == 16
41
48
  end
42
49
 
43
50
  def check_chars
44
- test = @codice_fiscale == @codice_fiscale.parameterize.upcase[/^[A-Z0-9]*$/]
51
+ test = @fiscal_code == @fiscal_code[/^[A-Z0-9]*$/]
45
52
  errors << 'Caratteri non ammessi' unless test
46
53
  end
47
54
 
48
55
  def check_checksum
49
- errors << 'Checksum errato' if checksum != @codice_fiscale.last
56
+ errors << 'Checksum errato' if checksum != @fiscal_code.last
50
57
  end
51
58
 
52
59
  def check_sex
53
- case @codice_fiscale[9]
60
+ case @fiscal_code[9]
54
61
  when /[0-3LMNP]/
55
- @data[:sesso] = 'M'
62
+ @data[:sex] = 'M'
56
63
  when /[4-7QRST]/
57
- @data[:sesso] = 'F'
64
+ @data[:sex] = 'F'
58
65
  else
59
66
  @errors << 'Cifra decina giorno di nascita errata'
60
67
  end
61
68
  end
62
69
 
70
+ def check_birth_place
71
+ letter = @fiscal_code[11]
72
+ numbers =
73
+ @fiscal_code[12..14]
74
+ .split(//)
75
+ .map do |c|
76
+ i = OMOCODICI.index(c)
77
+ i ? i.to_s : c
78
+ end
79
+ .join
80
+ belfiore = letter + numbers
81
+
82
+ @birth_place = BELFIORE[belfiore]
83
+ @errors << "Codice istat #{belfiore} non trovato" unless @birth_place
84
+ end
85
+
86
+ MESI = 'ABCDEHLMPRST'.freeze
87
+
88
+ def check_birth_date
89
+ yy = cifre(6..7)
90
+ return if @errors.any?
91
+
92
+ day = cifre(9..10)
93
+ return if @errors.any?
94
+
95
+ @errors << 'Cifra decina giorno di nascita errata' if day > 71
96
+ return if @errors.any?
97
+
98
+ @data[:sex] = day > 40 ? 'F' : 'M'
99
+
100
+ month = MESI.index(@fiscal_code[8])
101
+ @errors << 'Mese errato' unless month
102
+ return if @errors.any?
103
+
104
+ @birth_date =
105
+ Date.new(yy2yyyy(yy), month + 1, day % 40) rescue nil
106
+
107
+ @errors << 'Data di nascita errata' unless @birth_date
108
+ return if @errors.any?
109
+
110
+ @data[:birth_date] = @birth_date
111
+ end
112
+
113
+ def yy2yyyy(yy)
114
+ Date.today.year -
115
+ (Date.today.year % 100 + 100 - yy ) % 100
116
+ end
117
+
118
+ def cifre(range)
119
+ result = 0
120
+ range.each do |position|
121
+ char = @fiscal_code[position]
122
+ value = CIFRE.index(char)
123
+ @errors << "Carattere '#{char}' errato in posizione #{position}" unless value
124
+ return nil unless value
125
+
126
+ result *= 10
127
+ result += value % 10
128
+ end
129
+ result
130
+ end
131
+
63
132
  DISPARI = [
64
133
  1, 0, 5, 7, 9,
65
134
  13, 15, 17, 19, 21,
@@ -69,10 +138,11 @@ module Cfita
69
138
  ].freeze
70
139
 
71
140
  OMOCODICI = 'LMNPQRSTUV'
141
+ CIFRE = ('0123456789' + OMOCODICI).freeze
72
142
 
73
143
  def checksum
74
144
  tot = 0
75
- @codice_fiscale[0..14].bytes.first(15).each.with_index do |byte, i|
145
+ @fiscal_code[0..14].bytes.first(15).each.with_index do |byte, i|
76
146
  next unless byte
77
147
 
78
148
  byte -= byte < 65 ? 48 : 65
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfita
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Savanelli