Sha256: 8d129f0a52915d62c4131fc15ac891af12f277c2e6901cf7874e39279ffe0d0b

Contents?: true

Size: 1.51 KB

Versions: 66

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
    logger.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)
    begin
      spreadheet.parse(header_search: target, clean: true)
    rescue Roo::HeaderRowNotFoundError => e
      missing  = JSON.parse(e.message)
      logger.warn("The input file is missing these headers: #{missing}")
      present = target - missing
      rows(present)
    end
  end

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

end

Version data entries

66 entries across 66 versions & 1 rubygems

Version Path
eco-helpers-2.4.9 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.4.8 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.4.7 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.4.6 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.4.5 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.4.4 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.4.3 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.4.2 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.3.3 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.3.2 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.2.5 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.2.4 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.2.3 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.2.2 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.2.1 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.1.12 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.1.11 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.1.10 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.1.9 lib/eco/api/common/people/default_parsers/xls_parser.rb
eco-helpers-2.1.8 lib/eco/api/common/people/default_parsers/xls_parser.rb