lib/itax_code/parser.rb in itax_code-0.2.0 vs lib/itax_code/parser.rb in itax_code-0.3.0

- old
+ new

@@ -23,19 +23,14 @@ # Decodes the tax code into its components. # # @return [Hash] def decode - year = decode_year - month = utils.months.find_index(raw[:birthdate_month]) + 1 - day, gender = decode_day_and_gender - birthplace = decode_birthplace - { code: tax_code, gender: gender, - birthdate: Date.parse([year, month, day].join("-")).to_s, + birthdate: birthdate, birthplace: birthplace, omocodes: Omocode.new(tax_code).list, raw: raw } end @@ -43,55 +38,68 @@ private attr_accessor :tax_code, :utils def raw - matches = tax_code.scan(utils.regex).flatten - - { - surname: matches[0], - name: matches[1], - birthdate: matches[2], - birthdate_year: matches[3], - birthdate_month: matches[4], - birthdate_day: matches[5], - birthplace: matches[6], - cin: matches[7] + @raw ||= { + surname: raw_matches[0], + name: raw_matches[1], + birthdate: raw_matches[2], + birthdate_year: raw_matches[3], + birthdate_month: raw_matches[4], + birthdate_day: raw_matches[5], + birthplace: raw_matches[6], + cin: raw_matches[7] } end - def decode_year - year = utils.omocodia_decode raw[:birthdate_year] - year = (Date.today.year.to_s[0..1] + year).to_i - year -= 100 if year > Date.today.year - year + def raw_matches + @raw_matches ||= tax_code.scan(utils.regex).flatten end - def decode_day_and_gender - day = utils.omocodia_decode(raw[:birthdate_day]).to_i + def gender + val = utils.omocodia_decode(raw[:birthdate_day]).to_i + val > 40 ? "F" : "M" + end - if day > 40 - day -= 40 - [day, "F"] - else - [day, "M"] - end + # This method tries to calculate the year based on a raw data. This means that the returned + # value could be wrong. E.g. a person born on 1920 would have birthdate_year = 20 meaning + # that both 1920 and 2020 could be valid born years. + def year + val = (Date.today.year.to_s[0..1] + utils.omocodia_decode(raw[:birthdate_year])).to_i + val > Date.today.year ? val - 100 : val end - def decode_birthplace(src = utils.cities, exit: false) - place = src.find do |item| - item["code"] == city_code && !item["name"].include?("soppresso") - end + def month + utils.months.find_index(raw[:birthdate_month]) + 1 + end + def day + val = utils.omocodia_decode(raw[:birthdate_day]).to_i + val > 40 ? val - 40 : val + end + + def birthdate + @birthdate ||= Date.parse("#{year}-#{month}-#{day}").to_s + end + + def birthplace(src = utils.cities, stop: false) + place = src.find { |item| item["code"] == birthplace_code && in_dates?(item) } + if place.nil? - exit ? return : decode_birthplace(utils.countries, exit: true) + birthplace(utils.countries, stop: true) unless stop else - place["name"] = place["name"].gsub(" (soppresso)", "") place.to_h.deep_symbolize_keys end end - def city_code + def birthplace_code raw[:birthplace][0] + utils.omocodia_decode(raw[:birthplace][1..-1]) + end + + def in_dates?(item) + created_on = Date.parse item.fetch("created_on", "0000-01-01") + deleted_on = Date.parse item.fetch("deleted_on", "9999-12-31") + Date.parse(birthdate).between? created_on, deleted_on end end end