lib/itax_code/parser.rb in itax_code-0.1.2 vs lib/itax_code/parser.rb in itax_code-0.1.3
- old
+ new
@@ -1,21 +1,26 @@
module ItaxCode
##
- # This class handles the parsing logic for TaxCode module.
+ # This class handles the parsing logic.
#
# @param [String] tax_code
#
# @example
#
# ItaxCode::Parser.new("RSSMRA70A01L726S").decode
#
# @return [Hash]
class Parser
+ class NoTaxCodeError < StandardError; end
+ class InvalidTaxCodeError < StandardError; end
+
def initialize(tax_code, utils = Utils.new)
- @tax_code = (tax_code || "").upcase
@utils = utils
+ @tax_code = (tax_code || "").upcase
+ raise NoTaxCodeError if @tax_code.blank?
+ raise InvalidTaxCodeError unless Validator.standard_length?(@tax_code)
end
##
# This method decodes the tax code.
#
@@ -72,16 +77,25 @@
else
[day, "M"]
end
end
- def decode_birthplace
- places = utils.municipalities
- .select { |m| m["code"] == municipality_code }
+ def decode_birthplace(src = utils.municipalities, exit: false)
+ places = src.select do |m|
+ m["code"] == municipality_code
+ end
+
place = places.find { |m| !m["name"].include? "soppresso" }
- place = (place.presence || places.last).deep_symbolize_keys
- place[:name] = place[:name].gsub(" (soppresso)", "")
- place
+ place = place.presence || places.last
+
+ if place.nil?
+ return if exit
+
+ decode_birthplace utils.countries, exit: true
+ else
+ place["name"] = place["name"].gsub(" (soppresso)", "")
+ place.deep_symbolize_keys
+ end
end
def municipality_code
raw[:birthplace][0] + utils.omocodia_decode(raw[:birthplace][1..-1])
end