Sha256: 89a42894ab1d6011e8fb061d7d6da911ba702d6a599ccc8b87276785f372ef1d

Contents?: true

Size: 971 Bytes

Versions: 7

Compression:

Stored size: 971 Bytes

Contents

# encoding: utf-8
module Spotlight
  ##
  # Process a CSV upload into new Spotlight::Resource::Upload objects
  class AddUploadsFromCSV < ActiveJob::Base
    queue_as :default

    after_perform do |job|
      csv_data, exhibit, user = job.arguments
      Spotlight::IndexingCompleteMailer.documents_indexed(csv_data, exhibit, user).deliver_now
    end

    def perform(csv_data, exhibit, _user)
      encoded_csv(csv_data).each do |row|
        url = row.delete('url')
        next unless url.present?

        resource = Spotlight::Resources::Upload.new(
          remote_url_url: url,
          data: row,
          exhibit: exhibit
        )

        resource.save_and_index
      end
    end

    private

    def encoded_csv(csv)
      csv.map do |row|
        row.map do |label, column|
          [label, column.encode('UTF-8', invalid: :replace, undef: :replace, replace: "\uFFFD")] if column.present?
        end.compact.to_h
      end.compact
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
blacklight-spotlight-0.33.3 app/jobs/spotlight/add_uploads_from_csv.rb
blacklight-spotlight-0.33.2 app/jobs/spotlight/add_uploads_from_csv.rb
blacklight-spotlight-0.33.1 app/jobs/spotlight/add_uploads_from_csv.rb
blacklight-spotlight-0.33.0 app/jobs/spotlight/add_uploads_from_csv.rb
blacklight-spotlight-0.32.0 app/jobs/spotlight/add_uploads_from_csv.rb
blacklight-spotlight-0.31.0 app/jobs/spotlight/add_uploads_from_csv.rb
blacklight-spotlight-0.30.0 app/jobs/spotlight/add_uploads_from_csv.rb