Sha256: b3f29a37ae021f2b2868875064054846e00a1502fd486de3adddebe9575a6e29

Contents?: true

Size: 1.39 KB

Versions: 28

Compression:

Stored size: 1.39 KB

Contents

class Eco::API::UseCases::Default::Locations::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

28 entries across 28 versions & 1 rubygems

Version Path
eco-helpers-3.0.4 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.3 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.2 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.1 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-3.0.0 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.25 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.24 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.23 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.22 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.21 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.20 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.19 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.18 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.17 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.16 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.15 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.14 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.13 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.12 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb
eco-helpers-2.7.4 lib/eco/api/usecases/default/locations/codes_to_tags_case.rb