itcf 1.0.0

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.
Files changed (8) hide show
  1. data/History.txt +4 -0
  2. data/Manifest.txt +7 -0
  3. data/README.txt +31 -0
  4. data/Rakefile +18 -0
  5. data/lib/cf.data +11896 -0
  6. data/lib/cf.rb +163 -0
  7. data/test/test_cf.rb +44 -0
  8. metadata +65 -0
@@ -0,0 +1,163 @@
1
+ require 'rubygems'
2
+
3
+ class FiscalCode
4
+ VERSION = '1.0.0'
5
+
6
+ private
7
+
8
+ @@cities = {}
9
+
10
+ @@cities_path =
11
+ [Gem.default_dir + File::Separator + 'gems' +
12
+ File::Separator + 'cf-' + FiscalCode::VERSION + File::Separator +
13
+ 'lib' + File::Separator,
14
+ Dir.pwd + File::Separator,
15
+ Dir.pwd + File::Separator + 'lib' + File::Separator].find do |f|
16
+ FileTest.exists?(f + "cf.data")
17
+ end + "cf.data"
18
+
19
+ File.open(@@cities_path) do |f|
20
+ while (line = f.gets)
21
+ (city, province, citycode) = line.chomp.split(/:/)
22
+ @@cities[city] = citycode
23
+ end
24
+ end
25
+
26
+ def assert_range(n,min,max)
27
+ raise ArgumentError,"#{n} < #{min} || #{n} > #{max}" if (n < min || n > max)
28
+ end
29
+
30
+ def assert_sex(s)
31
+ raise ArgumentError,"Wrong sex (#{s})" unless (s == 'F' || s == 'M')
32
+ end
33
+
34
+ def initialize(name,surname,day,month,year,sex,city)
35
+ assert_range(day,1,31)
36
+ assert_range(month,1,12)
37
+ assert_range(year,0,99)
38
+ sex.upcase!
39
+ assert_sex(sex);
40
+ @name = name
41
+ @surname = surname
42
+ @day = day
43
+ @month = month
44
+ @year = year
45
+ @sex = sex
46
+ @city = city.upcase
47
+ end
48
+
49
+ MONTH_CODE = ['A','B','C','D','E','H','L','M','P','R','S','T']
50
+ VOWELS = Regexp.new('[AEIOU]')
51
+ CONSONANTS = Regexp.new('[BCDFGHJKLMNPQRSTVWXYZ]')
52
+
53
+ def common_name(n)
54
+ n.upcase!
55
+ n.gsub!(/\s/,"")
56
+ l = n.length
57
+ raise ArgumentError,"Null name" if (l == 0)
58
+ vowels = n.gsub(CONSONANTS,"")
59
+ consonants = n.gsub(VOWELS,"")
60
+ vl = vowels.size
61
+ cl = consonants.size
62
+
63
+ case cl
64
+ when 0
65
+ if (vl == 2)
66
+ return vowels + "X"
67
+ else
68
+ raise ArgumentError,"Cannot proceed with no " +
69
+ "consonants && without 2 vowels"
70
+ end
71
+
72
+ when 1
73
+ if (vl == 0)
74
+ raise ArgumentError,"Cannot proceed with 1 " +
75
+ "consonant && 0 vowels"
76
+ elsif (vl == 1)
77
+ return consonants[0,1] + vowels[0,1] + "X"
78
+ else
79
+ return consonants[0,1] + vowels[0..1]
80
+ end
81
+
82
+ when 2
83
+ if (vl >= 1)
84
+ return consonants[0..1] + vowels[0,1]
85
+ else
86
+ raise ArgumentError,"Cannot proceed with 2 " +
87
+ "consonants && without 1 vowel"
88
+ end
89
+
90
+ when 3
91
+ return consonants[0..2]
92
+ else
93
+ return consonants[0..3]
94
+ end
95
+ end
96
+
97
+ def name
98
+ namecode = common_name(@name)
99
+ if namecode.length > 3
100
+ return namecode[0,1] + namecode[2..3]
101
+ else
102
+ return namecode
103
+ end
104
+ end
105
+
106
+ def surname
107
+ return common_name(@surname)[0..2]
108
+ end
109
+
110
+ def city
111
+ c = @@cities[@city]
112
+ raise ArgumentError,"Unnknown city #{c}" if (c.nil? || c.empty?)
113
+ return c
114
+ end
115
+
116
+ def birth_date
117
+ birthcode = "%02d" % @year.to_s
118
+ birthcode += MONTH_CODE[@month-1].to_s
119
+ birthcode += ("%02d" % (@sex == 'M' ? @day : @day + 40).to_s)
120
+ end
121
+
122
+ public
123
+ def code
124
+ @code = surname +
125
+ name +
126
+ birth_date +
127
+ city
128
+ value = 0
129
+ for i in 0..14 do
130
+ c = @code[i,1]
131
+ if (i % 2 != 0)
132
+ if c =~ /[0-9]/
133
+ value += c.to_i
134
+ else
135
+ value += "ABCDEFGHIJKLMNOPQRSTUVWXYZ".index(c)
136
+ end
137
+ else
138
+ if (c =~ /[0-9]/)
139
+ value += 1 if (c == '0')
140
+ value += 5 if (c == '2')
141
+ value += 7 if (c == '3')
142
+ value += 9 if (c == '4')
143
+ value += 13 if (c == '5')
144
+ value += 15 if (c == '6')
145
+ value += 17 if (c == '7')
146
+ value += 19 if (c == '8')
147
+ value += 21 if (c == '9')
148
+ else
149
+ value += "BAKPLCQDREVOSFTGUHMINJWZYX".index(c);
150
+ end
151
+ end
152
+ end
153
+ value = value % 26;
154
+ @code += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[value,1]
155
+ end
156
+
157
+ def self.calc(name, surname, day, month, year, sex, city)
158
+ cf = FiscalCode.new(name, surname, day, month, year, sex, city)
159
+ return cf.code
160
+ end
161
+
162
+ end
163
+
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'test/unit'
4
+ require 'cf'
5
+
6
+ class TestFiscalCode < Test::Unit::TestCase
7
+ def test_wrong_values
8
+ assert_raise(ArgumentError) { FiscalCode.calc "","",0,0,0,"","" }
9
+ assert_raise(ArgumentError) { FiscalCode.calc "Mario","",0,0,0,"","" }
10
+ assert_raise(ArgumentError) { FiscalCode.calc "Mario","Rossi",0,0,0,"","" }
11
+ assert_raise(ArgumentError) { FiscalCode.calc "Mario","Rossi",0,0,0,"","" }
12
+ assert_raise(ArgumentError) { FiscalCode.calc "Mario","Rossi",32,1,0,"","" }
13
+ assert_raise(ArgumentError) { FiscalCode.calc "Mario","Rossi",31,1,0,"D","" }
14
+ assert_raise(ArgumentError) { FiscalCode.calc "Mario","Rossi",31,1,0,"M","XXX" }
15
+ assert_raise(ArgumentError) { FiscalCode.calc "M","Rossi",31,1,0,"M","XXX" }
16
+ assert_raise(ArgumentError) { FiscalCode.calc "Ma","Rossi",31,1,0,"M","XXX" }
17
+ end
18
+
19
+ def test_limit_case
20
+ assert_equal(FiscalCode.calc("Maa","Rossi",1,1,40,"M","Milano"),
21
+ "RSSMAA40A01F205G")
22
+ end
23
+
24
+ def test_standard_case
25
+ assert_equal("RSSMRA40A01F205N",
26
+ FiscalCode.calc("Mario","Rossi",1,1,40,"M","Milano"))
27
+ assert_equal("BNCLCU27A41H501N",
28
+ FiscalCode.calc("Lucia","Bianchi",1,1,27,"F","Roma"))
29
+ assert_equal("JNHDNY27A01Z404Y",
30
+ FiscalCode.calc("Danny","Jonhson",1,1,27,"M",
31
+ "Stati Uniti d'America"))
32
+ assert_equal("SNDMRI27A01Z200A",
33
+ FiscalCode.calc("Mir","Sandeep",1,1,27,"M",
34
+ "Afghanistan"))
35
+ assert_equal("KMIDAO27A01Z213Y",
36
+ FiscalCode.calc("Dao","Kim",1,1,27,"M",
37
+ "Corea del Sud"))
38
+ assert_equal("TZULAO27A01Z210W",
39
+ FiscalCode.calc("Lao","Tzu",1,1,27,"M",
40
+ "Cina"))
41
+
42
+
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.2
3
+ specification_version: 1
4
+ name: itcf
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2007-09-12 00:00:00 +02:00
8
+ summary: FIX
9
+ require_paths:
10
+ - lib
11
+ email: gerardo.lamastra <at> telecomitalia.it
12
+ homepage: |
13
+ == DESCRIPTION:
14
+
15
+ rubyforge_project: fiscalcode
16
+ description: "== FEATURES/PROBLEMS: FiscalCode.calc will throw an ArgumentError Exception if it cannot do the calculation, for example because day,month and years are not in the proper range (1-31,1-12,0-99), or because the city cannot be located in the city database. == SYNOPSIS: Use: FiscalCode.calc(name, surname, day, month, year, sex, city) Es. FiscalCode.calc(\"Mario\", \"Rossi\", 31,12,80, \"M\", \"Milano\") == INSTALL:"
17
+ autorequire:
18
+ default_executable:
19
+ bindir: bin
20
+ has_rdoc: true
21
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
22
+ requirements:
23
+ - - ">"
24
+ - !ruby/object:Gem::Version
25
+ version: 0.0.0
26
+ version:
27
+ platform: ruby
28
+ signing_key:
29
+ cert_chain:
30
+ post_install_message:
31
+ authors:
32
+ - Gerardo Lamastra
33
+ files:
34
+ - History.txt
35
+ - Manifest.txt
36
+ - README.txt
37
+ - Rakefile
38
+ - lib/cf.rb
39
+ - lib/cf.data
40
+ - test/test_cf.rb
41
+ test_files:
42
+ - test/test_cf.rb
43
+ rdoc_options:
44
+ - --main
45
+ - README.txt
46
+ extra_rdoc_files:
47
+ - History.txt
48
+ - Manifest.txt
49
+ - README.txt
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ requirements: []
55
+
56
+ dependencies:
57
+ - !ruby/object:Gem::Dependency
58
+ name: hoe
59
+ version_requirement:
60
+ version_requirements: !ruby/object:Gem::Version::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 1.3.0
65
+ version: