Sha256: e3bff29fbbc9cdffe5117a531c97b6f2b0fd0459eb4894d4fcf074945622c07a

Contents?: true

Size: 1.55 KB

Versions: 1

Compression:

Stored size: 1.55 KB

Contents

class Eco::API::Common::People::DefaultParsers::XLSParser < Eco::API::Common::Loaders::Parser
  attribute :xls

  attr_accessor :already_required

  def parser(filename, _deps)
    rows(file: filename).tap do |rws|
      rws.each do |row|
        to_string!(row)
      end
    end
  end

  def serializer(_array_hash, _deps)
    raise "Not implemented. TODO: using axlsx or rubyXL gems. See: https://spin.atomicobject.com/2017/03/22/parsing-excel-files-ruby/"
  end

  private

  def to_string!(row)
    row.transform_values! do |val|
      next     unless val
      next val if val.is_a?(String)

      val.to_s
    end
  end

  def expected_headers
    log(:info) {
      "Headers detection is using your fields_map.json file (native behaviour)"
    }
    session.fields_mapper.list(:external).uniq
  end

  def sheet_name
    0
  end

  def workbook(file)
    require_reading_libs!
    Roo::Spreadsheet.open(file)
  end

  def spreadheet(name_or_index = sheet_name, file:)
    workbook(file).sheet(name_or_index)
  end

  def rows(target = expected_headers, file:)
    spreadheet(file: file).parse(header_search: target, clean: true)
  rescue Roo::HeaderRowNotFoundError => e
    missing = JSON.parse(e.message)

    log(:warn) {
      "The input file is missing these expected headers: #{missing}"
    }

    present = target - missing
    rows(present, file: file)
  end

  def require_reading_libs!
    return if already_required

    require 'roo'
    require 'roo-xls'
    self.already_required = true
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eco-helpers-3.0.21 lib/eco/api/common/people/default_parsers/xls_parser.rb