Sha256: 63ed4ede471450af4515f8cbe6c7914c0d4d301281ecac93d5a4254602821f29

Contents?: true

Size: 1.51 KB

Versions: 34

Compression:

Stored size: 1.51 KB

Contents

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

  attr_accessor :already_required
  attr_reader   :file

  def parser(file, _deps)
    @file = file
    rows.tap do |rws|
      @file = nil
      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 nil unless val
      next val if val.is_a?(String)
      val.to_s
    end
  end

  def headers
    log(:warn) {
      "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
    require_reading_libs!
    Roo::Spreadsheet.open(file)
  end

  def spreadheet(name_or_index = sheet_name)
    workbook.sheet(name_or_index)
  end

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

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

    present = target - missing
    rows(present)
  end

  def require_reading_libs!
    return if already_required
    require 'roo'
    require 'roo-xls'
    self.already_required = true
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
eco-helpers-3.0.18 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.17 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.16 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.15 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.14 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.13 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.12 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.11 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.10 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.9 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.8 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.7 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.6 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.5 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.4 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.3 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.2 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.1 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-3.0.0 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.7.25 lib/eco/api/common/people/default_parsers/xls_parser.rb