Sha256: fd75b866830a40152c2798958588904cbd8f5d3f537b0adaa2e21ac77c4ab357

Contents?: true

Size: 1.44 KB

Versions: 80

Compression:

Stored size: 1.44 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
    @session = session; @options = options
    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
      @session.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

80 entries across 80 versions & 1 rubygems

Version Path
eco-helpers-2.5.2 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.5.1 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.4.9 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.4.8 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.4.7 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.4.6 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.4.5 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.4.4 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.4.3 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.4.2 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.3.3 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.3.2 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.2.5 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.2.4 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.2.3 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.2.2 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.2.1 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.1.12 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.1.11 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.1.10 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb