Sha256: 8cf35b04b99cded8abb14396a839d22db91d8184556d970ed193b9c8f6eaed35

Contents?: true

Size: 1.42 KB

Versions: 29

Compression:

Stored size: 1.42 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)
    @session = session
    @options = options
    codes_to_tags
    exit
  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

29 entries across 29 versions & 1 rubygems

Version Path
eco-helpers-2.0.16 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.15 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.14 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.13 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.12 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.11 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.10 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.9 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.8 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.7 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.6 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.5 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.4 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.3 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-2.0.2 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-1.5.15 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-1.5.14 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-1.5.13 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-1.5.12 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb
eco-helpers-1.5.11 lib/eco/api/usecases/default_cases/codes_to_tags_case.rb