Sha256: 0a4f87c03e8fe2f67d79a6eaf54c9fe787834aaa596eb9aa617a2e4f32f314ce

Contents?: true

Size: 1.68 KB

Versions: 17

Compression:

Stored size: 1.68 KB

Contents

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

  require_relative 'cli/codes_to_tags_cli'

  def main(*_args)
    options[:end_get] = false
    codes_to_tags
  end

  def codes_to_tags
    msg = "You have not defined a tagtree for the organisation"
    raise msg unless session.tagtree

    csv_table = Eco::CSV.read(input_file)

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

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

      log(:info) {
        "Generated '#{output_file}' file"
      }
    end
  end

  private

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

  def mapper
    return @mapper if instance_variable_defined?(:@mapper)

    @mapper = session.config.locations_mapper if session.config.location_codes

    unless @mapper
      msg = "No filter_tags parser, nor location mappings are present"
      raise msg
    end

    @mapper
  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
    File.join(output_folder, "mapped_#{clean_file_name(input_file)}")
  end

  def output_folder
    File.dirname(input_file)
  end

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

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
eco-helpers-3.0.21 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.20 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.19 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.18 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.17 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.16 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.15 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.14 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.13 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.12 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.11 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.10 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.9 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.8 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.7 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.6 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.5 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb