Module: CodiceFiscale
- Defined in:
- lib/codice_fiscale.rb,
lib/codice_fiscale/codes.rb,
lib/codice_fiscale/helpers.rb,
lib/codice_fiscale/version.rb,
lib/codice_fiscale/alphabet.rb,
lib/codice_fiscale/fiscal_code.rb,
lib/codice_fiscale/configuration.rb,
lib/codice_fiscale/italian_citizen.rb
Defined Under Namespace
Modules: Alphabet, Codes, Configurable, Helpers Classes: Configuration, FiscalCode, ItalianCitizen
Constant Summary collapse
- VERSION =
"1.0.0"
Class Method Summary collapse
- .calculate(params) ⇒ Object
- .config ⇒ Object
-
.update_csv_files(are_you_sure = false) ⇒ Object
This method is used by me to update the csv and repack the gem.
Class Method Details
.calculate(params) ⇒ Object
19 20 21 22 23 |
# File 'lib/codice_fiscale.rb', line 19 def self.calculate params citizen = ItalianCitizen.new params raise ArgumentError.new(citizen.errors..join(', ')) unless citizen.valid? citizen.fiscal_code end |
.config ⇒ Object
15 16 17 |
# File 'lib/codice_fiscale.rb', line 15 def self.config @config ||= Configuration.new end |
.update_csv_files(are_you_sure = false) ⇒ Object
This method is used by me to update the csv and repack the gem
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/codice_fiscale.rb', line 26 def self.update_csv_files(are_you_sure = false) unless are_you_sure warn("Nothing was done, call me with are_you_sure = true") return end require "net/http" require "tmpdir" require "tmpdir" require "fileutils" # download the CSV with all the city codes # @see https://www.istat.it/it/archivio/6789 for more info url = "https://www.istat.it/storage/codici-unita-amministrative/Elenco-comuni-italiani.csv" res = Net::HTTP.get_response(URI(url)) raise("Failed to GET #{url}") unless res.is_a?(Net::HTTPSuccess) data = res.body # Covert to utf-8 data.force_encoding("iso-8859-1") data.encode!("utf-8") raise("Failed to convert to utf-8") unless data.valid_encoding? # parse it and rebuild a new CSV selecting only the columns I need CSV.open(config.city_codes_csv_path, "wb") do |csv| CSV.parse(data, :col_sep => ";", :row_sep => "\r\n").each_with_index do |r, index| next if index == 0 c, p, x = *[r[5], r[13], r[18]].map(&:strip) raise("Check the number of columns: #{r}") if r.size != 23 raise("Invalid city name: #{c}") if c.include?(',') or c.blank? raise("Invalid province code: #{p}") if p !~ /^[A-Z]{2}$/ raise("Invalid city code: #{x}") if x !~ /^[A-Z]{1}[0-9]{3}$/ csv << [c, p, x] end end # checkout the CSV with all the country codes # @see https://www.istat.it/it/archivio/6747 url = "https://www.istat.it/it/files//2011/01/Elenco-codici-e-denominazioni-unita-territoriali-estere.zip" res = Net::HTTP.get_response(URI(url)) raise("Failed to GET #{url}") unless res.is_a?(Net::HTTPSuccess) # store the zip in temporary folder and extract it tmpdir = Dir.mktmpdir filename = "Elenco-codici-e-denominazioni-unita-territoriali-estere" system("cd #{tmpdir} && unzip -q -o #{tmpdir}/#{filename}.zip") data = File.open(Dir["#{tmpdir}/**/*.csv"][0], "rb", &:read) # Covert the csv to utf-8 data.force_encoding("iso-8859-1") data.encode!("utf-8") raise("Failed to convert to utf-8") unless data.valid_encoding? # parse it and rebuild a new CSV selecting only the columns I need CSV.open(config.country_codes_csv_path, "wb") do |csv| CSV.parse(data, :col_sep => ";", :row_sep => "\r\n").each_with_index do |r, index| next if index == 0 next if r.compact.empty? c1, c2, x = *[r[6], r[7], r[9]].map(&:strip) next if x.casecmp('n.d.').zero? raise("Check the number of columns: #{r}") if r.size != 15 raise("Invalid country name: #{c1}") if c1.include?(',') or c1.blank? raise("Invalid country name: #{c2}") if c2.include?(',') or c2.blank? raise("Invalid country code: #{x}") if x !~ /^[A-Z]{1}[0-9]{3}$/ csv << [c1, c2, x] end end ensure FileUtils.rm_rf(tmpdir) if tmpdir and Dir.exists?(tmpdir) end |