Sha256: 89477e7120741f67fd1add724c16f4ae13f8e393b76564a1dc5e02cdb2b43abd

Contents?: true

Size: 1.38 KB

Versions: 8

Compression:

Stored size: 1.38 KB

Contents

class Eco::API::UseCases::DefaultCases::CodesToTagsCase < Eco::API::Common::Loaders::UseCase
  name "codes-to-tags-from"
  type :other

  def codes_to_tags
    raise "You have not defined a tagtree for the organisation" unless session.tagtree
    csv_table = Eco::CSV.read(input_file)

    csv_table.add_column("ecoPortalTag").tap do |csv_table|
      csv_table.each do |row|
        row["ecoPortalTag"] = to_tag(row[codes_column])
      end

      File.open(output_file, "w") {|fd| fd << csv_table.to_csv}
    end
  end

  def main(session, options, usecase)
    options[:end_get] = false
    codes_to_tags
  end

  private

  def to_tag(code)
    if mapper.external?(code)
      mapper.to_internal(code)
    elsif session.tagtree.tag?(code)
      code.upcase
    else
      logger.warn("Unknown Location Code: '#{code}'")
      nil
    end
  end

  def mapper
    unless mapper ||= session.config.location_codes && session.config.locations_mapper
      raise "No filter_tags parser, nor location mappings are present"
    end
  end

  def codes_column
    @codes_column ||= options.dig(:other, :file, :codes_column)
  end

  def input_file
    @input_file ||= options.dig(:other, :file, :name)
  end

  def output_file
    "mapped_#{clean_file_name(input_file)}"
  end

  def clean_file_name(filename)
    File.basename(filename).gsub(/\s/, "")
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
eco-helpers-2.5.10 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.5.9 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.5.8 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.5.7 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.5.6 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.5.5 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.5.4 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.5.3 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb