codice-fiscale 0.0.9 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +12 -0
- data/Guardfile +63 -26
- data/README.md +43 -34
- data/Rakefile +4 -0
- data/codice_fiscale.gemspec +5 -6
- data/lib/codice_fiscale/codes/city_codes.csv +7924 -1
- data/lib/codice_fiscale/codes/country_codes.csv +216 -1
- data/lib/codice_fiscale/codes.rb +3 -3
- data/lib/codice_fiscale/version.rb +1 -1
- data/lib/codice_fiscale.rb +79 -0
- data/spec/lib/codice_fiscale/alphabet_spec.rb +16 -8
- data/spec/lib/codice_fiscale/codes_spec.rb +7 -3
- data/spec/lib/codice_fiscale/fiscal_code_spec.rb +39 -23
- data/spec/lib/codice_fiscale/italian_citizen_spec.rb +8 -8
- data/spec/lib/codice_fiscale_spec.rb +16 -7
- data/spec/spec_helper.rb +0 -2
- metadata +20 -26
- data/spec/fixtures/city_codes.csv +0 -1
- data/spec/fixtures/country_codes.csv +0 -1
- data/spec/lib/codice_fiscale/helpers_spec.rb +0 -4
@@ -1 +1,216 @@
|
|
1
|
-
|
1
|
+
Albania,Albania,Z100
|
2
|
+
Andorra,Andorra,Z101
|
3
|
+
Austria,Austria,Z102
|
4
|
+
Belgio,Belgium,Z103
|
5
|
+
Bulgaria,Bulgaria,Z104
|
6
|
+
Danimarca,Denmark,Z107
|
7
|
+
Finlandia,Finland,Z109
|
8
|
+
Francia,France,Z110
|
9
|
+
Germania,Germany,Z112
|
10
|
+
Regno Unito,United Kingdom,Z114
|
11
|
+
Grecia,Greece,Z115
|
12
|
+
Irlanda,Ireland,Z116
|
13
|
+
Islanda,Iceland,Z117
|
14
|
+
Liechtenstein,Liechtenstein,Z119
|
15
|
+
Lussemburgo,Luxembourg,Z120
|
16
|
+
Malta,Malta,Z121
|
17
|
+
Monaco,Monaco,Z123
|
18
|
+
Norvegia,Norway,Z125
|
19
|
+
Paesi Bassi,Netherlands,Z126
|
20
|
+
Polonia,Poland,Z127
|
21
|
+
Portogallo,Portugal,Z128
|
22
|
+
Romania,Romania,Z129
|
23
|
+
San Marino,San Marino,Z130
|
24
|
+
Spagna,Spain,Z131
|
25
|
+
Svezia,Sweden,Z132
|
26
|
+
Svizzera,Switzerland,Z133
|
27
|
+
Ucraina,Ukraine,Z138
|
28
|
+
Ungheria,Hungary,Z134
|
29
|
+
Federazione russa,Russian Federation,Z154
|
30
|
+
Stato della Città del Vaticano,Vatican City State,Z106
|
31
|
+
Estonia,Estonia,Z144
|
32
|
+
Lettonia,Latvia,Z145
|
33
|
+
Lituania,Lithuania,Z146
|
34
|
+
Croazia,Croatia,Z149
|
35
|
+
Slovenia,Slovenia,Z150
|
36
|
+
Bosnia-Erzegovina,Bosnia and Herzegovina,Z153
|
37
|
+
Ex Repubblica Jugoslava di Macedonia,The former Yugoslav Republic of Macedonia,Z148
|
38
|
+
Moldova,Moldova,Z140
|
39
|
+
Slovacchia,Slovakia,Z155
|
40
|
+
Bielorussia,Belarus,Z139
|
41
|
+
Repubblica ceca,Czech Republic,Z156
|
42
|
+
Montenegro,Montenegro,Z159
|
43
|
+
Serbia,Serbia,Z158
|
44
|
+
Kosovo,Kosovo,Z160
|
45
|
+
Afghanistan,Afghanistan,Z200
|
46
|
+
Arabia Saudita,Saudi Arabia,Z203
|
47
|
+
Bahrein,Bahrain,Z204
|
48
|
+
Bangladesh,Bangladesh,Z249
|
49
|
+
Bhutan,Bhutan,Z205
|
50
|
+
Myanmar/Birmania,Myanmar/Burma,Z206
|
51
|
+
Brunei Darussalam,Brunei Darussalam,Z207
|
52
|
+
Cambogia,Cambodia,Z208
|
53
|
+
Sri Lanka,Sri Lanka,Z209
|
54
|
+
Cina,China,Z210
|
55
|
+
Cipro,Cyprus,Z211
|
56
|
+
Corea del Nord,North Korea,Z214
|
57
|
+
Corea del Sud,South Korea,Z213
|
58
|
+
Emirati Arabi Uniti,United Arab Emirates,Z215
|
59
|
+
Filippine,Philippines,Z216
|
60
|
+
Palestina,Palestine,Z161
|
61
|
+
Giappone,Japan,Z219
|
62
|
+
Giordania,Jordan,Z220
|
63
|
+
India,India,Z222
|
64
|
+
Indonesia,Indonesia,Z223
|
65
|
+
Iran,Iran,Z224
|
66
|
+
Iraq,Iraq,Z225
|
67
|
+
Israele,Israel,Z226
|
68
|
+
Kuwait,Kuwait,Z227
|
69
|
+
Laos,Laos,Z228
|
70
|
+
Libano,Lebanon,Z229
|
71
|
+
Timor Leste,Timor Leste,Z242
|
72
|
+
Maldive,Maldives,Z232
|
73
|
+
Malaysia,Malaysia,Z247
|
74
|
+
Mongolia,Mongolia,Z233
|
75
|
+
Nepal,Nepal,Z234
|
76
|
+
Oman,Oman,Z235
|
77
|
+
Pakistan,Pakistan,Z236
|
78
|
+
Qatar,Qatar,Z237
|
79
|
+
Singapore,Singapore,Z248
|
80
|
+
Siria,Syria,Z240
|
81
|
+
Thailandia,Thailand,Z241
|
82
|
+
Turchia,Turkey,Z243
|
83
|
+
Vietnam,Vietnam,Z251
|
84
|
+
Yemen,Yemen,Z246
|
85
|
+
Kazakhstan,Kazakhstan,Z255
|
86
|
+
Uzbekistan,Uzbekistan,Z259
|
87
|
+
Armenia,Armenia,Z252
|
88
|
+
Azerbaigian,Azerbaijan,Z253
|
89
|
+
Georgia,Georgia,Z254
|
90
|
+
Kirghizistan,Kyrgyzstan,Z256
|
91
|
+
Tagikistan,Tajikistan,Z257
|
92
|
+
Taiwan,Taiwan,Z217
|
93
|
+
Turkmenistan,Turkmenistan,Z258
|
94
|
+
Algeria,Algeria,Z301
|
95
|
+
Angola,Angola,Z302
|
96
|
+
Costa d'Avorio,Côte d'Ivoire,Z313
|
97
|
+
Benin,Benin,Z314
|
98
|
+
Botswana,Botswana,Z358
|
99
|
+
Burkina Faso,Burkina Faso,Z354
|
100
|
+
Burundi,Burundi,Z305
|
101
|
+
Camerun,Cameroon,Z306
|
102
|
+
Capo Verde,Cape Verde,Z307
|
103
|
+
Repubblica Centrafricana,Central African Republic,Z308
|
104
|
+
Ciad,Chad,Z309
|
105
|
+
Comore,Comoros,Z310
|
106
|
+
Congo,Congo,Z311
|
107
|
+
Egitto,Egypt,Z336
|
108
|
+
Etiopia,Ethiopia,Z315
|
109
|
+
Gabon,Gabon,Z316
|
110
|
+
Gambia,Gambia,Z317
|
111
|
+
Ghana,Ghana,Z318
|
112
|
+
Gibuti,Djibouti,Z361
|
113
|
+
Guinea,Guinea,Z319
|
114
|
+
Guinea-Bissau,Guinea-Bissau,Z320
|
115
|
+
Guinea equatoriale,Equatorial Guinea,Z321
|
116
|
+
Kenya,Kenya,Z322
|
117
|
+
Lesotho,Lesotho,Z359
|
118
|
+
Liberia,Liberia,Z325
|
119
|
+
Libia,Libya,Z326
|
120
|
+
Madagascar,Madagascar,Z327
|
121
|
+
Malawi,Malawi,Z328
|
122
|
+
Mali,Mali,Z329
|
123
|
+
Marocco,Morocco,Z330
|
124
|
+
Mauritania,Mauritania,Z331
|
125
|
+
Maurizio,Mauritius,Z332
|
126
|
+
Mozambico,Mozambique,Z333
|
127
|
+
Namibia,Namibia,Z300
|
128
|
+
Niger,Niger,Z334
|
129
|
+
Nigeria,Nigeria,Z335
|
130
|
+
Ruanda,Rwanda,Z338
|
131
|
+
Sao Tomé e Principe,Sao Tome and Principe,Z341
|
132
|
+
Seychelles,Seychelles,Z342
|
133
|
+
Senegal,Senegal,Z343
|
134
|
+
Sierra Leone,Sierra Leone,Z344
|
135
|
+
Somalia,Somalia,Z345
|
136
|
+
Sudafrica,South Africa,Z347
|
137
|
+
Sudan,Sudan,Z348
|
138
|
+
Eswatini,Eswatini,Z349
|
139
|
+
Tanzania,Tanzania,Z357
|
140
|
+
Togo,Togo,Z351
|
141
|
+
Tunisia,Tunisia,Z352
|
142
|
+
Uganda,Uganda,Z353
|
143
|
+
Repubblica Democratica del Congo,Democratic Republic of the Congo,Z312
|
144
|
+
Zambia,Zambia,Z355
|
145
|
+
Zimbabwe,Zimbabwe,Z337
|
146
|
+
Eritrea,Eritrea,Z368
|
147
|
+
Sud Sudan,South Sudan,Z907
|
148
|
+
Antigua e Barbuda,Antigua and Barbuda,Z532
|
149
|
+
Bahamas,Bahamas,Z502
|
150
|
+
Barbados,Barbados,Z522
|
151
|
+
Belize,Belize,Z512
|
152
|
+
Canada,Canada,Z401
|
153
|
+
Costa Rica,Costa Rica,Z503
|
154
|
+
Cuba,Cuba,Z504
|
155
|
+
Dominica,Dominica,Z526
|
156
|
+
Repubblica Dominicana,Dominican Republic,Z505
|
157
|
+
El Salvador,El Salvador,Z506
|
158
|
+
Giamaica,Jamaica,Z507
|
159
|
+
Grenada,Grenada,Z524
|
160
|
+
Guatemala,Guatemala,Z509
|
161
|
+
Haiti,Haiti,Z510
|
162
|
+
Honduras,Honduras,Z511
|
163
|
+
Messico,Mexico,Z514
|
164
|
+
Nicaragua,Nicaragua,Z515
|
165
|
+
Panama,Panama,Z516
|
166
|
+
Santa Lucia,Saint Lucia,Z527
|
167
|
+
Saint Vincent e Grenadine,Saint Vincent and the Grenadines,Z528
|
168
|
+
Saint Kitts e Nevis,Saint Kitts and Nevis,Z533
|
169
|
+
Stati Uniti d'America,United States of America,Z404
|
170
|
+
Argentina,Argentina,Z600
|
171
|
+
Bolivia,Bolivia,Z601
|
172
|
+
Brasile,Brazil,Z602
|
173
|
+
Cile,Chile,Z603
|
174
|
+
Colombia,Colombia,Z604
|
175
|
+
Ecuador,Ecuador,Z605
|
176
|
+
Guyana,Guyana,Z606
|
177
|
+
Paraguay,Paraguay,Z610
|
178
|
+
Perù,Peru,Z611
|
179
|
+
Suriname,Suriname,Z608
|
180
|
+
Trinidad e Tobago,Trinidad and Tobago,Z612
|
181
|
+
Uruguay,Uruguay,Z613
|
182
|
+
Venezuela,Venezuela,Z614
|
183
|
+
Australia,Australia,Z700
|
184
|
+
Figi,Fiji,Z704
|
185
|
+
Kiribati,Kiribati,Z731
|
186
|
+
Isole Marshall,Marshall Islands,Z711
|
187
|
+
Stati Federati di Micronesia,Federated States of Micronesia,Z735
|
188
|
+
Nauru,Nauru,Z713
|
189
|
+
Nuova Zelanda,New Zealand,Z719
|
190
|
+
Palau,Palau,Z734
|
191
|
+
Papua Nuova Guinea,Papua New Guinea,Z730
|
192
|
+
Isole Salomone,Solomon Islands,Z724
|
193
|
+
Samoa,Samoa,Z726
|
194
|
+
Tonga,Tonga,Z728
|
195
|
+
Tuvalu,Tuvalu,Z732
|
196
|
+
Vanuatu,Vanuatu,Z733
|
197
|
+
Nuova Caledonia,New Caledonia,Z716
|
198
|
+
Sahara occidentale,Western Sahara,Z339
|
199
|
+
Bermuda,Bermuda,Z400
|
200
|
+
Isole Cook (NZ),Cook Islands (NZ),Z703
|
201
|
+
Gibilterra,Gibraltar,Z113
|
202
|
+
Isole Cayman,Cayman Islands,Z530
|
203
|
+
Anguilla,Anguilla,Z529
|
204
|
+
Polinesia francese,French Polynesia,Z723
|
205
|
+
Isole Fær Øer,Faroe Islands,Z108
|
206
|
+
Aruba,Aruba,Z501
|
207
|
+
Groenlandia,Greenland,Z402
|
208
|
+
Isole Falkland (Malvine),Falkland Islands (Malvinas),Z609
|
209
|
+
Isola di Man,Isle of Man,Z122
|
210
|
+
Montserrat,Montserrat,Z531
|
211
|
+
Isole Pitcairn,Pitcairn,Z722
|
212
|
+
Saint Pierre e Miquelon,Saint Pierre and Miquelon,Z403
|
213
|
+
Sant'Elena,Saint Helena,Z340
|
214
|
+
Isole Turks e Caicos,Turks and Caicos Islands,Z519
|
215
|
+
Isole Vergini britanniche,British Virgin Islands,Z525
|
216
|
+
Wallis e Futuna,Wallis and Futuna Islands,Z729
|
data/lib/codice_fiscale/codes.rb
CHANGED
@@ -24,8 +24,8 @@ module CodiceFiscale
|
|
24
24
|
def city city_name, province_code
|
25
25
|
return config.city_code.call(city_name, province_code) if config.city_code
|
26
26
|
CSV.foreach config.city_codes_csv_path do |row|
|
27
|
-
if city_name.casecmp(row[
|
28
|
-
return row[
|
27
|
+
if city_name.casecmp(row[0]).zero? and province_code.casecmp(row[1]).zero?
|
28
|
+
return row[2].upcase
|
29
29
|
end
|
30
30
|
end
|
31
31
|
nil
|
@@ -34,7 +34,7 @@ module CodiceFiscale
|
|
34
34
|
def country country_name
|
35
35
|
return config.country_code.call(country_name) if config.country_code
|
36
36
|
CSV.foreach config.country_codes_csv_path do |row|
|
37
|
-
return row[
|
37
|
+
return row[2].upcase if country_name.casecmp(row[0]).zero? or country_name.casecmp(row[1]).zero?
|
38
38
|
end
|
39
39
|
nil
|
40
40
|
end
|
data/lib/codice_fiscale.rb
CHANGED
@@ -21,4 +21,83 @@ module CodiceFiscale
|
|
21
21
|
raise ArgumentError.new(citizen.errors.full_messages.join(', ')) unless citizen.valid?
|
22
22
|
citizen.fiscal_code
|
23
23
|
end
|
24
|
+
|
25
|
+
# This method is used by me to update the csv and repack the gem
|
26
|
+
def self.update_csv_files(are_you_sure = false)
|
27
|
+
unless are_you_sure
|
28
|
+
warn("Nothing was done, call me with are_you_sure = true")
|
29
|
+
return
|
30
|
+
end
|
31
|
+
|
32
|
+
require "net/http"
|
33
|
+
require "tmpdir"
|
34
|
+
require "tmpdir"
|
35
|
+
require "fileutils"
|
36
|
+
|
37
|
+
# download the CSV with all the city codes
|
38
|
+
# @see https://www.istat.it/it/archivio/6789 for more info
|
39
|
+
url = "https://www.istat.it/storage/codici-unita-amministrative/Elenco-comuni-italiani.csv"
|
40
|
+
res = Net::HTTP.get_response(URI(url))
|
41
|
+
raise("Failed to GET #{url}") unless res.is_a?(Net::HTTPSuccess)
|
42
|
+
data = res.body
|
43
|
+
|
44
|
+
# Covert to utf-8
|
45
|
+
data.force_encoding("iso-8859-1")
|
46
|
+
data.encode!("utf-8")
|
47
|
+
raise("Failed to convert to utf-8") unless data.valid_encoding?
|
48
|
+
|
49
|
+
# parse it and rebuild a new CSV selecting only the columns I need
|
50
|
+
CSV.open(config.city_codes_csv_path, "wb") do |csv|
|
51
|
+
CSV.parse(data, :col_sep => ";", :row_sep => "\r\n").each_with_index do |r, index|
|
52
|
+
next if index == 0
|
53
|
+
|
54
|
+
c, p, x = *[r[5], r[13], r[18]].map(&:strip)
|
55
|
+
|
56
|
+
raise("Check the number of columns: #{r}") if r.size != 23
|
57
|
+
raise("Invalid city name: #{c}") if c.include?(',') or c.blank?
|
58
|
+
raise("Invalid province code: #{p}") if p !~ /^[A-Z]{2}$/
|
59
|
+
raise("Invalid city code: #{x}") if x !~ /^[A-Z]{1}[0-9]{3}$/
|
60
|
+
|
61
|
+
csv << [c, p, x]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# checkout the CSV with all the country codes
|
66
|
+
# @see https://www.istat.it/it/archivio/6747
|
67
|
+
url = "https://www.istat.it/it/files//2011/01/Elenco-codici-e-denominazioni-unita-territoriali-estere.zip"
|
68
|
+
res = Net::HTTP.get_response(URI(url))
|
69
|
+
raise("Failed to GET #{url}") unless res.is_a?(Net::HTTPSuccess)
|
70
|
+
|
71
|
+
# store the zip in temporary folder and extract it
|
72
|
+
tmpdir = Dir.mktmpdir
|
73
|
+
filename = "Elenco-codici-e-denominazioni-unita-territoriali-estere"
|
74
|
+
system("cd #{tmpdir} && unzip -q -o #{tmpdir}/#{filename}.zip")
|
75
|
+
data = File.open(Dir["#{tmpdir}/**/*.csv"][0], "rb", &:read)
|
76
|
+
|
77
|
+
# Covert the csv to utf-8
|
78
|
+
data.force_encoding("iso-8859-1")
|
79
|
+
data.encode!("utf-8")
|
80
|
+
raise("Failed to convert to utf-8") unless data.valid_encoding?
|
81
|
+
|
82
|
+
# parse it and rebuild a new CSV selecting only the columns I need
|
83
|
+
CSV.open(config.country_codes_csv_path, "wb") do |csv|
|
84
|
+
CSV.parse(data, :col_sep => ";", :row_sep => "\r\n").each_with_index do |r, index|
|
85
|
+
next if index == 0
|
86
|
+
next if r.compact.empty?
|
87
|
+
|
88
|
+
c1, c2, x = *[r[6], r[7], r[9]].map(&:strip)
|
89
|
+
|
90
|
+
next if x.casecmp('n.d.').zero?
|
91
|
+
|
92
|
+
raise("Check the number of columns: #{r}") if r.size != 15
|
93
|
+
raise("Invalid country name: #{c1}") if c1.include?(',') or c1.blank?
|
94
|
+
raise("Invalid country name: #{c2}") if c2.include?(',') or c2.blank?
|
95
|
+
raise("Invalid country code: #{x}") if x !~ /^[A-Z]{1}[0-9]{3}$/
|
96
|
+
|
97
|
+
csv << [c1, c2, x]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
ensure
|
101
|
+
FileUtils.rm_rf(tmpdir) if tmpdir and Dir.exists?(tmpdir)
|
102
|
+
end
|
24
103
|
end
|
@@ -3,23 +3,31 @@ require 'spec_helper'
|
|
3
3
|
describe CodiceFiscale::Alphabet do
|
4
4
|
describe '#letters' do
|
5
5
|
it 'is 26 long' do
|
6
|
-
subject.letters.size.
|
6
|
+
expect(subject.letters.size).to eq 26
|
7
7
|
end
|
8
8
|
|
9
|
-
it '
|
10
|
-
subject.letters.
|
9
|
+
it 'consists only of upcased letters' do
|
10
|
+
expect(subject.letters.join).to match /^[A-Z]{1,}$/
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
14
|
describe '#consonants' do
|
15
15
|
it 'is 21 long' do
|
16
|
-
subject.consonants.size.
|
16
|
+
expect(subject.consonants.size).to eq 21
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'consists only of upcased letters' do
|
20
|
+
expect(subject.vowels.join).to match /^[A-Z]{1,}$/
|
17
21
|
end
|
18
22
|
end
|
19
23
|
|
20
|
-
describe '#
|
21
|
-
it '
|
22
|
-
subject.
|
24
|
+
describe '#vowels' do
|
25
|
+
it 'is 5 long' do
|
26
|
+
expect(subject.vowels.size).to eq 5
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'consists only of upcased letters' do
|
30
|
+
expect(subject.vowels.join).to match /^[A-Z]{1,}$/
|
23
31
|
end
|
24
32
|
end
|
25
|
-
end
|
33
|
+
end
|
@@ -4,19 +4,23 @@ describe CodiceFiscale::Codes do
|
|
4
4
|
describe '#month_letter' do
|
5
5
|
context 'the given number is greater than 0 and less than 12' do
|
6
6
|
it 'return a letter' do
|
7
|
-
|
7
|
+
1.upto(12).each do |n|
|
8
|
+
expect(subject.month_letter(1)).to match /^[A-Z]{1,}$/
|
9
|
+
end
|
8
10
|
end
|
9
11
|
end
|
10
12
|
|
11
13
|
context 'the given number is greater than 12' do
|
12
14
|
it 'return nil' do
|
13
|
-
subject.month_letter(13).
|
15
|
+
expect(subject.month_letter(13)).to be_nil
|
14
16
|
end
|
15
17
|
end
|
16
18
|
|
17
19
|
context 'the given number is less than 1' do
|
18
20
|
it 'return nil' do
|
19
|
-
|
21
|
+
-1.upto(0).each do |n|
|
22
|
+
expect(subject.month_letter(n)).to be_nil
|
23
|
+
end
|
20
24
|
end
|
21
25
|
end
|
22
26
|
end
|
@@ -1,23 +1,33 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe CodiceFiscale::FiscalCode do
|
4
|
-
let(:citizen_marco_galli)
|
4
|
+
let(:citizen_marco_galli) do
|
5
|
+
CodiceFiscale::ItalianCitizen.new(
|
6
|
+
:name => 'Marco',
|
7
|
+
:surname => 'Galli',
|
8
|
+
:gender => :male,
|
9
|
+
:birthdate => Date.new(1983, 5, 3),
|
10
|
+
:city_name => 'Oggiono',
|
11
|
+
:province_code => 'LC'
|
12
|
+
)
|
13
|
+
end
|
14
|
+
|
5
15
|
let(:fiscal_code) { described_class.new citizen_marco_galli }
|
6
16
|
|
7
17
|
describe '#surname_part' do
|
8
18
|
it 'takes the first 3 consonants' do
|
9
|
-
fiscal_code.surname_part.
|
19
|
+
expect(fiscal_code.surname_part).to eq 'GLL'
|
10
20
|
end
|
11
21
|
|
12
22
|
it 'is 3 chrs long' do
|
13
|
-
fiscal_code.surname_part.size.
|
23
|
+
expect(fiscal_code.surname_part.size).to eq 3
|
14
24
|
end
|
15
25
|
|
16
26
|
context 'when surname has only 1 consonant' do
|
17
27
|
before { fiscal_code.citizen.surname = 'oof' }
|
18
28
|
|
19
29
|
it 'puts the vowels after the consonants' do
|
20
|
-
fiscal_code.surname_part.
|
30
|
+
expect(fiscal_code.surname_part).to eq 'FOO'
|
21
31
|
end
|
22
32
|
end
|
23
33
|
|
@@ -25,21 +35,21 @@ describe CodiceFiscale::FiscalCode do
|
|
25
35
|
before { fiscal_code.citizen.surname = 'm' }
|
26
36
|
|
27
37
|
it 'pads with the "X" character' do
|
28
|
-
fiscal_code.surname_part.
|
38
|
+
expect(fiscal_code.surname_part).to eq 'MXX'
|
29
39
|
end
|
30
40
|
end
|
31
41
|
end
|
32
42
|
|
33
43
|
describe '#name_part' do
|
34
44
|
it 'is 3 chrs long' do
|
35
|
-
fiscal_code.name_part.size.
|
45
|
+
expect(fiscal_code.name_part.size).to eq 3
|
36
46
|
end
|
37
47
|
|
38
48
|
context 'when name has 4 or more consonants' do
|
39
49
|
before { fiscal_code.citizen.name = 'danielino' }
|
40
50
|
|
41
51
|
it 'takes the 1st the 3rd and the 4th' do
|
42
|
-
fiscal_code.name_part.
|
52
|
+
expect(fiscal_code.name_part).to eq 'DLN'
|
43
53
|
end
|
44
54
|
end
|
45
55
|
|
@@ -47,7 +57,7 @@ describe CodiceFiscale::FiscalCode do
|
|
47
57
|
before { fiscal_code.citizen.name = 'daniele' }
|
48
58
|
|
49
59
|
it 'takes the first 3 consonants' do
|
50
|
-
fiscal_code.name_part.
|
60
|
+
expect(fiscal_code.name_part).to eq 'DNL'
|
51
61
|
end
|
52
62
|
end
|
53
63
|
|
@@ -55,7 +65,7 @@ describe CodiceFiscale::FiscalCode do
|
|
55
65
|
before { fiscal_code.citizen.name = 'bar' }
|
56
66
|
|
57
67
|
it 'puts the vowels after the consonants' do
|
58
|
-
fiscal_code.name_part.
|
68
|
+
expect(fiscal_code.name_part).to eq 'BRA'
|
59
69
|
end
|
60
70
|
end
|
61
71
|
|
@@ -63,7 +73,7 @@ describe CodiceFiscale::FiscalCode do
|
|
63
73
|
before { fiscal_code.citizen.name = 'd' }
|
64
74
|
|
65
75
|
it 'pad with the "X" character' do
|
66
|
-
fiscal_code.name_part.
|
76
|
+
expect(fiscal_code.name_part).to eq 'DXX'
|
67
77
|
end
|
68
78
|
end
|
69
79
|
end
|
@@ -71,14 +81,16 @@ describe CodiceFiscale::FiscalCode do
|
|
71
81
|
|
72
82
|
describe '#birthdate_part' do
|
73
83
|
it 'start with the last 2 digit of the year' do
|
74
|
-
fiscal_code.birthdate_part.
|
84
|
+
expect(fiscal_code.birthdate_part).to start_with '83'
|
75
85
|
end
|
76
86
|
|
77
87
|
describe 'the 3rd character' do
|
78
|
-
before
|
88
|
+
before do
|
89
|
+
allow(CodiceFiscale::Codes).to receive(:month_letter).and_return('X')
|
90
|
+
end
|
79
91
|
|
80
92
|
it 'is the month code' do
|
81
|
-
fiscal_code.birthdate_part[2].
|
93
|
+
expect(fiscal_code.birthdate_part[2]).to eq 'X'
|
82
94
|
end
|
83
95
|
end
|
84
96
|
|
@@ -86,13 +98,13 @@ describe CodiceFiscale::FiscalCode do
|
|
86
98
|
context 'gender is male' do
|
87
99
|
before { fiscal_code.citizen.gender = :male }
|
88
100
|
|
89
|
-
it('is the birth day') { fiscal_code.birthdate_part[3..5].
|
101
|
+
it('is the birth day') { expect(fiscal_code.birthdate_part[3..5]).to eq '03' }
|
90
102
|
end
|
91
103
|
|
92
104
|
context 'gender is female' do
|
93
105
|
before { fiscal_code.citizen.gender = :female }
|
94
106
|
|
95
|
-
it('is the birth day + 40') { fiscal_code.birthdate_part[3..5].
|
107
|
+
it('is the birth day + 40') { expect(fiscal_code.birthdate_part[3..5]).to eq '43' }
|
96
108
|
end
|
97
109
|
end
|
98
110
|
end
|
@@ -105,15 +117,17 @@ describe CodiceFiscale::FiscalCode do
|
|
105
117
|
before { fiscal_code.config.city_code { 'Winterfell' } }
|
106
118
|
|
107
119
|
it 'returns the result of the city-block execution' do
|
108
|
-
fiscal_code.birthplace_part.
|
120
|
+
expect(fiscal_code.birthplace_part). to eq 'Winterfell'
|
109
121
|
end
|
110
122
|
end
|
111
123
|
|
112
124
|
context 'when codes are fetched using csv' do
|
113
|
-
before
|
125
|
+
before do
|
126
|
+
allow(CodiceFiscale::Codes).to receive(:city).and_return('hello')
|
127
|
+
end
|
114
128
|
|
115
129
|
it 'returns the city code' do
|
116
|
-
fiscal_code.birthplace_part.
|
130
|
+
expect(fiscal_code.birthplace_part).to eq 'hello'
|
117
131
|
end
|
118
132
|
end
|
119
133
|
end
|
@@ -125,15 +139,17 @@ describe CodiceFiscale::FiscalCode do
|
|
125
139
|
before { fiscal_code.config.country_code { 'The North' } }
|
126
140
|
|
127
141
|
it 'returns the result of the country-block execution' do
|
128
|
-
fiscal_code.birthplace_part.
|
142
|
+
expect(fiscal_code.birthplace_part).to eq 'The North'
|
129
143
|
end
|
130
144
|
end
|
131
145
|
|
132
146
|
context 'when codes are fetched using csv' do
|
133
|
-
before
|
147
|
+
before do
|
148
|
+
allow(CodiceFiscale::Codes).to receive(:country).and_return('Middle-Earth')
|
149
|
+
end
|
134
150
|
|
135
151
|
it 'returns the country code' do
|
136
|
-
fiscal_code.birthplace_part.
|
152
|
+
expect(fiscal_code.birthplace_part).to eq 'Middle-Earth'
|
137
153
|
end
|
138
154
|
end
|
139
155
|
end
|
@@ -141,7 +157,7 @@ describe CodiceFiscale::FiscalCode do
|
|
141
157
|
|
142
158
|
describe '#control_character' do
|
143
159
|
it 'returns the expected letter' do
|
144
|
-
fiscal_code.control_character('RSSMRA87A01A005').
|
160
|
+
expect(fiscal_code.control_character('RSSMRA87A01A005')).to eq 'V'
|
145
161
|
end
|
146
162
|
end
|
147
|
-
end
|
163
|
+
end
|
@@ -9,11 +9,11 @@ describe CodiceFiscale::ItalianCitizen do
|
|
9
9
|
|
10
10
|
describe '#initialize' do
|
11
11
|
it 'accepts 1 attribute' do
|
12
|
-
|
12
|
+
expect { marco }.not_to raise_error
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'do not accepts 0 attribute' do
|
16
|
-
|
16
|
+
expect { described_class.new }.to raise_error(ArgumentError)
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
@@ -21,8 +21,8 @@ describe CodiceFiscale::ItalianCitizen do
|
|
21
21
|
describe '#validations' do
|
22
22
|
describe 'when all attributes are valid' do
|
23
23
|
it 'is valid' do
|
24
|
-
marco.
|
25
|
-
john.
|
24
|
+
expect(marco).to be_valid
|
25
|
+
expect(john).to be_valid
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -31,7 +31,7 @@ describe CodiceFiscale::ItalianCitizen do
|
|
31
31
|
|
32
32
|
it 'is not valid' do
|
33
33
|
citizen_without_name.valid?
|
34
|
-
citizen_without_name.errors[:name].
|
34
|
+
expect(citizen_without_name.errors[:name]).not_to be_empty
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -40,7 +40,7 @@ describe CodiceFiscale::ItalianCitizen do
|
|
40
40
|
|
41
41
|
it 'is not valid' do
|
42
42
|
citizen_with_strange_gender.valid?
|
43
|
-
citizen_with_strange_gender.errors[:gender].
|
43
|
+
expect(citizen_with_strange_gender.errors[:gender]).not_to be_empty
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
@@ -49,7 +49,7 @@ describe CodiceFiscale::ItalianCitizen do
|
|
49
49
|
|
50
50
|
it 'is not valid' do
|
51
51
|
citizen_with_invalid_province.valid?
|
52
|
-
citizen_with_invalid_province.errors[:province_code].
|
52
|
+
expect(citizen_with_invalid_province.errors[:province_code]).not_to be_empty
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -58,7 +58,7 @@ describe CodiceFiscale::ItalianCitizen do
|
|
58
58
|
|
59
59
|
it 'is not valid' do
|
60
60
|
citizen_with_invalid_date.valid?
|
61
|
-
citizen_with_invalid_date.errors[:birthdate].
|
61
|
+
expect(citizen_with_invalid_date.errors[:birthdate]).not_to be_empty
|
62
62
|
end
|
63
63
|
end
|
64
64
|
end
|
@@ -1,25 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
1
2
|
require 'spec_helper'
|
2
3
|
|
3
4
|
describe CodiceFiscale do
|
4
5
|
describe '#calculate' do
|
5
|
-
let(:mario_attributes) { {:name => 'mario', :surname => 'rossi', :gender => :male, :birthdate => Date.new(1987, 1, 1),
|
6
|
-
:province_code => 'lc', :city_name => 'Abbadia Lariana'} }
|
7
|
-
|
8
6
|
before do
|
9
7
|
subject.config.city_code = nil
|
10
8
|
subject.config.country_code = nil
|
11
9
|
end
|
12
10
|
|
13
11
|
it 'returns the expected code' do
|
14
|
-
|
12
|
+
# Please note that the following are completely made-up data
|
13
|
+
[
|
14
|
+
[{:name => 'mario', :surname => 'rossi', :gender => :male, :birthdate => Date.new(1987, 1, 1), :province_code => 'lc', :city_name => 'Abbadia Lariana'}, "RSSMRA87A01A005V"],
|
15
|
+
[{:name => "Marco", :surname => "Rossi", :gender => :male, :birthdate => Date.new(1983, 5, 3), :city_name => "Oggiono", :province_code => "LC"}, "RSSMRC83E03G009W"],
|
16
|
+
[{:name => "John", :surname => "Smith", :gender => :male, :birthdate => Date.new(1988, 5, 3), :country_name => "Francia"}, "SMTJHN88E03Z110R"],
|
17
|
+
[{:name => "John", :surname => "Smith", :gender => :male, :birthdate => Date.new(1988, 5, 3), :country_name => "France"}, "SMTJHN88E03Z110R"],
|
18
|
+
[{:name => "John", :surname => "Smith", :gender => :male, :birthdate => Date.new(1988, 5, 3), :country_name => "Sao Tomé e Principe"}, "SMTJHN88E03Z341A"],
|
19
|
+
[{:name => "Marco", :surname => "Rossi", :gender => :male, :birthdate => Date.new(1983, 5, 3), :city_name => "Forlì", :province_code =>"FC"}, "RSSMRC83E03D704X"],
|
20
|
+
].each do |row|
|
21
|
+
params, expected_code = row[0], row[1]
|
22
|
+
expect(subject.calculate(params)).to eq expected_code
|
23
|
+
end
|
15
24
|
end
|
16
25
|
|
17
26
|
context 'when params are not valid' do
|
18
|
-
let(:invalid_attributes) {
|
27
|
+
let(:invalid_attributes) { {:name => ''} }
|
19
28
|
|
20
29
|
it 'raises an error' do
|
21
|
-
|
30
|
+
expect { subject.calculate(invalid_attributes) }.to raise_error(ArgumentError)
|
22
31
|
end
|
23
32
|
end
|
24
33
|
end
|
25
|
-
end
|
34
|
+
end
|