Sha256: f7fd3322444edf39166d0bf11023254b13f0641263c1f9c1af4c24e68cbdcfa8

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

module RpiMarca
  class Receipt
    attr_reader :number, :date, :service_code, :agent,
                :applicant, :assignor, :assignee

    IPAS_RECEIPT_NOT_REQUIRED = %w(
      IPAS005 IPAS009 IPAS024 IPAS033 IPAS029 IPAS047 IPAS091 IPAS106 IPAS112
      IPAS135 IPAS136 IPAS139 IPAS142 IPAS157 IPAS158 IPAS161 IPAS289 IPAS291
      IPAS304 IPAS395 IPAS402 IPAS404 IPAS409 IPAS421 IPAS423
    )

    def initialize(number:, date:, service_code: nil, agent: nil,
                   applicant: nil, assignor: nil, assignee: nil)
      @number = number
      @date = date
      @service_code = format_service_code(service_code)
      @agent = agent
      @applicant = applicant
      @assignor = assignor
      @assignee = assignee
    end

    def self.parse(el)
      return if el.nil?

      number = Helpers.get_attribute_value(el, 'numero') or
        fail ParseError, 'Receipt number is required'
      date = Helpers.get_attribute_value(el, 'data') or
        fail ParseError, 'Receipt date is required'

      new(
        number: number,
        date: Helpers.parse_date(date),
        **optional_params(el)
      )
    end

    def self.optional_params(el)
      {
        service_code: Helpers.get_attribute_value(el, 'codigoServico'),
        agent: Helpers.get_element_value(el.at_xpath('procurador')),
        applicant: Owner.parse(el.at_xpath('requerente')),
        assignor: Owner.parse(el.at_xpath('cedente')),
        assignee: Owner.parse(el.at_xpath('cessionario'))
      }
    end

    private_class_method :optional_params

    private

    def format_service_code(code)
      "#{code[0...3]}.#{code[3..-1]}".chomp('.') if code
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rpi_marca-0.0.3 lib/rpi_marca/receipt.rb