diff --git a/lib/itax_code.rb b/lib/itax_code.rb index 9a4fdea..7d49025 100644 --- a/lib/itax_code.rb +++ b/lib/itax_code.rb @@ -6,7 +6,6 @@ require "itax_code/utils" require "itax_code/encoder" require "itax_code/parser" -require "itax_code/validator" module ItaxCode Error = Class.new(StandardError) @@ -42,7 +41,10 @@ def decode(tax_code) # # @return [Boolean] def valid?(tax_code) - Validator.new(tax_code).valid? + decode(tax_code) + true + rescue Parser::Error + false end end end diff --git a/lib/itax_code/parser.rb b/lib/itax_code/parser.rb index 6df2cb9..eb17707 100644 --- a/lib/itax_code/parser.rb +++ b/lib/itax_code/parser.rb @@ -16,15 +16,17 @@ class Parser InvalidControlInternalNumberError = Class.new(Error) InvalidTaxCodeError = Class.new(Error) + LENGTH = 16 + # @param [String] tax_code # @param [Utils] utils def initialize(tax_code, utils = Utils.new) @tax_code = tax_code&.upcase - raise NoTaxCodeError if @tax_code.blank? - raise InvalidTaxCodeError unless Validator.standard_length?(@tax_code) + @utils = utils - @utils = utils - raise InvalidControlInternalNumberError if raw[:cin] != @utils.encode_cin(tax_code) + raise NoTaxCodeError if @tax_code.blank? + raise InvalidTaxCodeError if @tax_code.length != LENGTH + raise InvalidControlInternalNumberError if raw[:cin] != @utils.encode_cin(@tax_code) end # Decodes the tax code into its components. diff --git a/lib/itax_code/validator.rb b/lib/itax_code/validator.rb deleted file mode 100644 index 5900957..0000000 --- a/lib/itax_code/validator.rb +++ /dev/null @@ -1,47 +0,0 @@ -# frozen_string_literal: true - -module ItaxCode - # Handles the validation logic. - class Validator - LENGTH = 16 - - # @param [String] tax_code The pre-computed tax code - def initialize(tax_code) - @tax_code = tax_code - end - - class << self - # Checks the tax code standard length against user and business fiscal code standards. - # - # @param [String] tax_code The tax code - # - # @return [true, false] - def standard_length?(tax_code) - tax_code.length == LENGTH - end - end - - # Checks pre-computed tax code validity against newly encoded tax code. - # - # @return [true, false] - def valid? - !decoded_tax_code.nil? - end - - private - - attr_reader :tax_code - - # Decodes the given tax code to backfill possibly missing data in the 'data' hash. - # If the decode fails, it means that the provided tax code is not valid. - # - # @return [Hash, nil] The decoded hash or nil if the tax code is not valid - def decoded_tax_code - @decoded_tax_code ||= begin - Parser.new(tax_code).decode - rescue Parser::Error - nil - end - end - end -end diff --git a/test/itax_code/validator_test.rb b/test/itax_code/validator_test.rb deleted file mode 100644 index 373bf09..0000000 --- a/test/itax_code/validator_test.rb +++ /dev/null @@ -1,31 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -module ItaxCode - class ValidatorTest < ActiveSupport::TestCase - test "public interface" do - instance_methods = Validator.instance_methods - Object.instance_methods - class_methods = Validator.methods - Object.methods - - assert_equal %i[valid?], instance_methods - assert_equal %i[standard_length?], class_methods - end - - test "#valid? is truthy when the tax code can be decoded" do - assert_predicate Validator.new("RSSMRA80A01F205X"), :valid? - end - - test "#valid? is falsy when the parser cannot decode the given tax code" do - assert_not Validator.new("WRONG").valid? - end - - test "#standard_length? is truthy when the tax code length is 16" do - assert Validator.standard_length?("RSSMRA00A01F205F") - end - - test "#standard_length? is falsy when the tax code hasn't 16 chars" do - assert_not Validator.standard_length?("WRONG") - end - end -end diff --git a/test/itax_code_test.rb b/test/itax_code_test.rb index 264c837..5a8b240 100644 --- a/test/itax_code_test.rb +++ b/test/itax_code_test.rb @@ -19,10 +19,14 @@ class ItaxCodeTest < ActiveSupport::TestCase assert_instance_of Hash, klass.decode("RSSMRA80A41F205B") end - test "#valid?" do + test "#valid? is truthy when the tax code can be decoded" do assert klass.valid?("RSSMRA80A10F205Z") end + test "#valid? is falsy when the parser cannot decode the given tax code" do + assert_not klass.valid?("WRONG") + end + private def klass