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.0.37 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.36 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.35 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.34 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.33 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.32 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.31 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.30 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.29 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.28 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.27 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.26 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.25 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.24 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.23 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.22 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.21 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.19 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.18 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.17 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb