class ItaxCode::Encoder
This class handles the tax code generation logic.
@param [String] surname The user first name @param [String] name The user last name @param [String] gender The user gender @param [String, Date] birthdate The user birthdate @param [String] birthplace The user birthplace
@example
ItaxCode::Encoder.new( surname: "Rossi", name: "Matteo", gender: "M", birthdate: "1990-08-23", birthplace: "Milano" ).encode
@return [String] The encoded tax code
Attributes
birthdate[RW]
birthplace[RW]
gender[RW]
name[RW]
surname[RW]
utils[RW]
Public Class Methods
new(data = {}, utils = Utils.new)
click to toggle source
# File lib/itax_code/encoder.rb, line 26 def initialize(data = {}, utils = Utils.new) @surname = data[:surname] @name = data[:name] @gender = data[:gender].try :upcase @birthdate = parsed_date data[:birthdate] @birthplace = data[:birthplace] instance_variables.each do |ivar| next if instance_variable_get(ivar).present? raise MissingDataError, "missing #{ivar} value" end @utils = utils end
Public Instance Methods
encode()
click to toggle source
This method calculates the tax code.
@return [String] The calculated tax code
# File lib/itax_code/encoder.rb, line 47 def encode code = encode_surname code += encode_name code += encode_birthdate code += encode_birthplace code += utils.encode_cin code code end
Private Instance Methods
encode_birthdate()
click to toggle source
# File lib/itax_code/encoder.rb, line 86 def encode_birthdate year = birthdate.year.to_s[2..-1] month = utils.months[birthdate.month - 1] day = format "%02d", (birthdate.day + (gender == "F" ? 40 : 0)) "#{year}#{month}#{day}" end
encode_birthplace(src = utils.municipalities, exit: false)
click to toggle source
# File lib/itax_code/encoder.rb, line 93 def encode_birthplace(src = utils.municipalities, exit: false) place = src.find do |m| utils.slugged(m["name"]) == utils.slugged(birthplace) end code = place.try(:[], "code") return code if code.present? return if exit encode_birthplace utils.countries, exit: true end
encode_name()
click to toggle source
# File lib/itax_code/encoder.rb, line 72 def encode_name str = utils.slugged(name).chars consonants = utils.extract_consonants str vowels = utils.extract_vowels str if consonants.length > 3 arr = consonants.dup.chars arr.delete_at 1 consonants = arr.join end "#{consonants[0..2]}#{vowels[0..2]}XXX"[0..2].upcase end
encode_surname()
click to toggle source
# File lib/itax_code/encoder.rb, line 65 def encode_surname str = utils.slugged(surname).chars consonants = utils.extract_consonants str vowels = utils.extract_vowels str "#{consonants[0..2]}#{vowels[0..2]}XXX"[0..2].upcase end
parsed_date(date)
click to toggle source
# File lib/itax_code/encoder.rb, line 105 def parsed_date(date) case date.class.name when "Date", "Time", "DateTime" then date else Date.parse(date) end end