Class: ItaxCode::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/itax_code/parser.rb,
lib/itax_code/error.rb

Overview

Handles the parsing logic.

Examples:


ItaxCode::Parser.new("RSSMRA70A01L726S").decode

Returns:

  • (Hash)

    The parsed tax code

Constant Summary collapse

Error =
Class.new(Error)
InvalidControlInternalNumberError =
Class.new(Error)
InvalidTaxCodeError =
Class.new(Error)
NoTaxCodeError =
Class.new(Error)
DateTaxCodeError =
Class.new(Error)
LENGTH =
16

Instance Method Summary collapse

Constructor Details

#initialize(tax_code, utils = Utils) ⇒ Parser

Returns a new instance of Parser.

Parameters:

  • tax_code (String)
  • utils (Utils) (defaults to: Utils)

Raises:



18
19
20
21
22
23
24
25
# File 'lib/itax_code/parser.rb', line 18

def initialize(tax_code, utils = Utils)
  @tax_code = tax_code&.upcase
  @utils    = utils

  raise NoTaxCodeError if @utils.blank?(@tax_code)
  raise InvalidTaxCodeError if @tax_code.length != LENGTH
  raise InvalidControlInternalNumberError if raw[:cin] != @utils.encode_cin(@tax_code)
end

Instance Method Details

#decodeHash

Decodes the tax code into its components.

Returns:

  • (Hash)


30
31
32
33
34
35
36
37
38
39
# File 'lib/itax_code/parser.rb', line 30

def decode
  {
    code: tax_code,
    gender: gender,
    birthdate: birthdate,
    birthplace: birthplace,
    omocodes: Omocode.new(tax_code).omocodes,
    raw: raw
  }
end