Sha256: cc72e6882697c15c498fdcd9fb848a89569b73bea0310074a8d8be7345afa319

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module RpiMarca
  class NationalClass
    attr_reader :number, :subclass1, :subclass2, :subclass3, :goods_services

    def initialize(number:, subclass1:, subclass2:, subclass3:,
                   goods_services:)
      fail ParseError, "National class #{classe} out of range (1-41)" unless
        (1..41).include?(number.to_i)

      @number = number
      @subclass1 = subclass1 if subclass1 > 0
      @subclass2 = subclass2 if subclass2 > 0
      @subclass3 = subclass3 if subclass3 > 0
      @goods_services = goods_services
    end

    def self.parse(el)
      return unless el

      subclass = parse_subclass(el)

      new(
        number: Helpers.get_attribute_value(el, 'codigo').to_i,
        **subclass,
        goods_services: Helpers.get_element_value(
          el.at_xpath('.//especificacao')
        )
      )
    end

    def self.parse_subclass(el)
      subclass = el.xpath('.//sub-classe-nacional').map { |s| s['codigo'] }
      fail ParseError, "National class can't have more than 3 subclasses" if
        subclass.length > 3

      {
        subclass1: subclass[0].to_i,
        subclass2: subclass[1].to_i,
        subclass3: subclass[2].to_i

      }
    end

    private_class_method :parse_subclass
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rpi_marca-0.0.1 lib/rpi_marca/national_class.rb