class Libis::Ingester::Exporter

Protected Instance Methods

email_report(item, *attachments) click to toggle source
# File lib/libis/ingester/tasks/exporter.rb, line 195
def email_report(item, *attachments)
  return if parameter(:mail_to).blank?
  send_email(get_export_file(item), *attachments) do |mail|
    mail.to = parameter(:mail_to)
    mail.cc = parameter(:mail_cc) unless parameter(:mail_cc).blank?
    mail.subject = 'Ingest complete.'
    mail.body = "The ingest '#{item.name}' finished successfully. Please find the ingest summary in attachment."
  end
  debug "Report sent to #{parameter(:mail_to)}#{parameter(:mail_cc).blank? ? '' : " and #{parameter(:mail_cc)}"}."
rescue Timeout::Error
  warn "Ingest report could not be sent by email. The report can be found here: #{get_export_file(item)}"
rescue Exception => e
  error "Problem encountered while trying to send report by email: #{e.message} @ #{e.backtrace[0]}. " +
            "The report can be found here: #{get_export_file(item)}"
end
export_collection(item) click to toggle source

@param [Libis::Ingester::Collection] item

# File lib/libis/ingester/tasks/exporter.rb, line 109
def export_collection(item)
  pid = item.properties['collection_id']
  unless pid
    warn "Collection #{item.name} was not found/created.", item
    return
  end

  export_file = get_export_file(item)

  key = get_key(export_file, item)

  pid = "col#{pid}"

  extra = {}
  parameter(:extra_keys).each do |k, v|
    extra[k] = eval(v) rescue ''
  end

  write_export(export_file, key, pid, extra)

  debug 'Collection %s with pid %s exported.', key, pid

end
export_item(item) click to toggle source

@param [Libis::Ingester::IntellectualEntity] item

# File lib/libis/ingester/tasks/exporter.rb, line 86
def export_item(item)
  pid = item.pid
  unless pid
    warn "Object #{item.name} was not ingested fully.", item
    return
  end

  export_file = get_export_file(item)

  key = get_key(export_file, item)

  extra = {}
  parameter(:extra_keys).each do |k, v|
    extra[k] = eval(v) rescue ''
  end

  write_export(export_file, key, pid, extra)

  debug 'Item %s with pid %s exported.', key, pid

end
for_csv(string) click to toggle source
# File lib/libis/ingester/tasks/exporter.rb, line 183
def for_csv(string)
  string =~ /,\n/ ? "\"#{string.gsub('"', '""')}\"" : string
end
for_tsv(string) click to toggle source
# File lib/libis/ingester/tasks/exporter.rb, line 179
def for_tsv(string)
  string =~ /\t\n/ ? "\"#{string.gsub('"', '""')}\"" : string
end
for_xml(string, type = :attr) click to toggle source
# File lib/libis/ingester/tasks/exporter.rb, line 187
def for_xml(string, type = :attr)
  string.encode(xml: type)
end
for_yml(string) click to toggle source
# File lib/libis/ingester/tasks/exporter.rb, line 191
def for_yml(string)
  string.inspect.to_yaml
end
get_export_file(item) click to toggle source
# File lib/libis/ingester/tasks/exporter.rb, line 142
def get_export_file(item)
  FileUtils.mkdir_p(parameter(:export_dir))
  file_name = parameter(:export_file_name)
  file_name ||= "#{item.get_run.name}.#{parameter(:export_format)}"
  File.join(parameter(:export_dir), file_name)
end
get_key(export_file, item) click to toggle source
# File lib/libis/ingester/tasks/exporter.rb, line 133
def get_key(export_file, item)
  run_item = item.get_run
  unless run_item.nil? || run_item.properties['export_file']
    run_item.properties['export_file'] = export_file
    run_item.save!
  end
  eval(parameter(:export_key))
end
post_process(item) click to toggle source
# File lib/libis/ingester/tasks/exporter.rb, line 76
def post_process(item)
  return unless item.is_a?(Libis::Ingester::Run)
  attachments = []
  attachments = item.options[:export_attachments].split(/\s*,\s*/) if item.options[:export_attachments]
  email_report item, *attachments
end
process(item) click to toggle source
# File lib/libis/ingester/tasks/exporter.rb, line 64
def process(item)
  case item
  when Libis::Ingester::Collection
    export_collection(item)
  when Libis::Ingester::IntellectualEntity
    export_item(item)
    stop_processing_subitems
  else
    # do nothing
  end
end
write_export(export_file, key_value, pid, extra = {}) click to toggle source
# File lib/libis/ingester/tasks/exporter.rb, line 149
def write_export(export_file, key_value, pid, extra = {})
  # noinspection RubyStringKeysInHashInspection
  data = {
      'KEY' => key_value,
      'PID' => pid,
      'URL' => "http://resolver.libis.be/#{pid}/representation"
  }.merge(extra)
  open(export_file, 'a') do |f|
    case parameter(:export_format).to_sym
    when :tsv
      f.puts data.keys.map {|k| for_tsv(k)}.join("\t") if f.size == 0 && parameter(:export_header)
      f.puts data.values.map {|v| for_tsv(v)}.join("\t")
    when :csv
      f.puts data.keys.map {|k| for_csv(k)}.join(',') if f.size == 0 && parameter(:export_header)
      f.puts data.values.map {|v| for_csv(v)}.join(',')
    when :xml
      f.puts '<?xml version="1.0" encoding="UTF-8"?>' if f.size == 0 && parameter(:export_header)
      f.puts '<item'
      data.each {|k, v| f.puts "  #{for_xml(k.to_s)}=\"#{for_xml(v)}\""}
      f.puts '/>'
    when :yml
      f.puts '# Ingester export file' if f.size == 0 && parameter(:export_header)
      f.puts '- ' + data.map {|k, v| "#{k}: #{for_yml(v)}"}.join("\n  ")
    else
      #nothing
    end

  end
end