cfita 0.0.1
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 +7 -0
- data/.rubocop.yml +2 -0
- data/Gemfile +7 -0
- data/README.md +3 -0
- data/cfita.gemspec +20 -0
- data/lib/cfita/codice_fiscale.rb +85 -0
- data/lib/cfita.rb +6 -0
- data/test/cfs.txt.gz +0 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4f8494a35ca96f3627c796b353ee4921eac293b15648161fb6fda3d8865b79d7
|
4
|
+
data.tar.gz: 3f4d8759989ae4c6f45b011b841dfdfa2f9a200f2bc6d628bd939ef9f310a417
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9f36da75cd909de4d9d8a0d10f2693264b90672a4762d12e5f3e3404c74da7046fa4e8aa66ba071e3edfc0549dc22ae460988b96a9a35428b4ebc4b320c960c9
|
7
|
+
data.tar.gz: 2c4fa36c314841e41a758330d3b730311a96408f58a7373a0b334d2e2024c5eee4eea556efd8dd1de6a1ea0dbc640fff66bf2815ffb017d44c419dd34e523cea
|
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
data/cfita.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.required_ruby_version = '>= 2.4'
|
5
|
+
s.name = 'cfita'
|
6
|
+
s.version = '0.0.1'
|
7
|
+
s.date = '2019-05-29'
|
8
|
+
s.summary = 'Italian fiscal code checker'
|
9
|
+
s.description = 'Controllo codici fiscali italiani'
|
10
|
+
s.authors = ['Stefano Savanelli']
|
11
|
+
s.email = 'stefano@savanelli.it'
|
12
|
+
s.homepage = 'https://rubygems.org/gems/cfita'
|
13
|
+
s.license = 'MIT'
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`
|
18
|
+
.split("\n").map { |f| File.basename(f) }
|
19
|
+
s.require_paths = ['lib']
|
20
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/all'
|
4
|
+
|
5
|
+
module Cfita
|
6
|
+
# Controllo codice fiscale italiano
|
7
|
+
class CodiceFiscale
|
8
|
+
attr_reader :codice_fiscale, :data, :errors, :sesso
|
9
|
+
|
10
|
+
def initialize(codice_fiscale)
|
11
|
+
@codice_fiscale = codice_fiscale.upcase.strip
|
12
|
+
@data = {}
|
13
|
+
@errors = []
|
14
|
+
parse
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
codice_fiscale
|
19
|
+
end
|
20
|
+
|
21
|
+
def valid?
|
22
|
+
errors.empty?
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def parse
|
28
|
+
check_size
|
29
|
+
check_chars
|
30
|
+
return if errors.any?
|
31
|
+
|
32
|
+
check_checksum
|
33
|
+
return if errors.any?
|
34
|
+
|
35
|
+
check_sex
|
36
|
+
end
|
37
|
+
|
38
|
+
def check_size
|
39
|
+
size = @codice_fiscale.size
|
40
|
+
errors << "Lunghezza errata (#{size})" unless size == 16
|
41
|
+
end
|
42
|
+
|
43
|
+
def check_chars
|
44
|
+
test = @codice_fiscale == @codice_fiscale.parameterize.upcase[/^[A-Z0-9]*$/]
|
45
|
+
errors << 'Caratteri non ammessi' unless test
|
46
|
+
end
|
47
|
+
|
48
|
+
def check_checksum
|
49
|
+
errors << 'Checksum errato' if checksum != @codice_fiscale.last
|
50
|
+
end
|
51
|
+
|
52
|
+
def check_sex
|
53
|
+
case @codice_fiscale[9]
|
54
|
+
when /[0-3LMNP]/
|
55
|
+
@data[:sesso] = 'M'
|
56
|
+
when /[4-7QRST]/
|
57
|
+
@data[:sesso] = 'F'
|
58
|
+
else
|
59
|
+
@errors << 'Cifra decina giorno di nascita errata'
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
DISPARI = [
|
64
|
+
1, 0, 5, 7, 9,
|
65
|
+
13, 15, 17, 19, 21,
|
66
|
+
2, 4, 18, 20, 11,
|
67
|
+
3, 6, 8, 12, 14,
|
68
|
+
16, 10, 22, 25, 24, 23
|
69
|
+
].freeze
|
70
|
+
|
71
|
+
OMOCODICI = 'LMNPQRSTUV'
|
72
|
+
|
73
|
+
def checksum
|
74
|
+
tot = 0
|
75
|
+
@codice_fiscale[0..14].bytes.first(15).each.with_index do |byte, i|
|
76
|
+
next unless byte
|
77
|
+
|
78
|
+
byte -= byte < 65 ? 48 : 65
|
79
|
+
sign = (i % 2).zero?
|
80
|
+
tot += sign ? DISPARI[byte] : byte
|
81
|
+
end
|
82
|
+
(tot % 26 + 65).chr
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/lib/cfita.rb
ADDED
data/test/cfs.txt.gz
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cfita
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stefano Savanelli
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-29 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Controllo codici fiscali italiani
|
14
|
+
email: stefano@savanelli.it
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".rubocop.yml"
|
20
|
+
- Gemfile
|
21
|
+
- README.md
|
22
|
+
- cfita.gemspec
|
23
|
+
- lib/cfita.rb
|
24
|
+
- lib/cfita/codice_fiscale.rb
|
25
|
+
- test/cfs.txt.gz
|
26
|
+
homepage: https://rubygems.org/gems/cfita
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '2.4'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubygems_version: 3.0.3
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: Italian fiscal code checker
|
49
|
+
test_files:
|
50
|
+
- test/cfs.txt.gz
|