class Hybridge::Batch::Ingest

Public Class Methods

new(file, collection_id, current_user) click to toggle source
# File lib/hybridge/batch/ingest.rb, line 5
def initialize(file, collection_id, current_user)
  @file = file
  @collection_id = collection_id
  @current_user = current_user
  load!
end

Public Instance Methods

load!() click to toggle source
# File lib/hybridge/batch/ingest.rb, line 12
def load!

  return unless staged? || processing?

  processing!
  @works = []
  @files = {}
  good_work = false

  @csv = CSV.parse(File.read(@file), headers: true, encoding: 'utf-8').map(&:to_hash)
  @csv.each do |row|
    type = row.first.last
    if type.nil?
      good_work = false
      next
    elsif Hyrax.config.registered_curation_concern_types.include? type
      good_work = true
      row.delete("Filename")
      @works << row
      @files[@works.length] = []
    elsif type.include? "File"
      if good_work
        row.delete("Object Type")
        @files[@works.length] << row
      end
    else
      good_work = false
      message = "Unknown work type '#{type}'"
      Hyrax::MessengerService.deliver(User.batch_user, @current_user, message, "HyBridge Warning: Unknown work type")
    end
  end

  @works.each_with_index do |work, index|
    Entry.new(work, @files[index+1], @collection_id, @current_user, File.dirname(@file))
  end
  processed!
end
processed!() click to toggle source
# File lib/hybridge/batch/ingest.rb, line 56
def processed!
  new_file = Pathname(@file).sub_ext '' + ".processed"
  File.rename(@file, new_file)
  @file = new_file
end
processed?() click to toggle source
# File lib/hybridge/batch/ingest.rb, line 66
def processed?
  File.file?(Pathname(@file).sub_ext '' + ".processed")
end
processing!() click to toggle source
# File lib/hybridge/batch/ingest.rb, line 50
def processing!
  new_file = Pathname(@file).sub_ext '' + ".processing"
  File.rename(@file, new_file) unless File.file?(new_file)
  @file = new_file
end
processing?() click to toggle source
# File lib/hybridge/batch/ingest.rb, line 62
def processing?
  File.file?(Pathname(@file).sub_ext '' + ".processing")
end
staged?() click to toggle source
# File lib/hybridge/batch/ingest.rb, line 70
def staged?
  File.file?(Pathname(@file).sub_ext '' + ".staged")
end