class ROF::Utility

A few common utility methods

Constants

WORK_TYPES

Strictly speaking, a Collection is not a Work- it's included here to cull out and pass down the batch processing pipeline

WORK_TYPE_WITH_PREFIX_PATTERN

Attributes

workdir[R]

give base directory of given file for workdir

Public Class Methods

check_solr_for_previous(config, osf_project_identifier) click to toggle source

query SOLR for Previous version of OSF Project. Return its fedora pid if it is found, nil otherwise

# File lib/rof/utility.rb, line 92
def self.check_solr_for_previous(config, osf_project_identifier)
  solr_url = config.fetch('solr_url', nil)
  return nil if solr_url.nil?
  solr = RSolr.connect url: "#{solr_url}"
  query = solr.get 'select', params: {
    q: "desc_metadata__osf_project_identifier_ssi:#{osf_project_identifier}",
    rows: 1,
    sort_by: 'date_archived',
    fl: ['id'],
    wt: 'json'
  }
  return nil if (query['response']['numFound']).zero?
  # should only be 1 SOLR doc (the most recent) in docs[0]
  query['response']['docs'][0]['id']
end
file_from_targz(targzfile, file_name) click to toggle source

read file from gzipped tar archive

# File lib/rof/utility.rb, line 109
def self.file_from_targz(targzfile, file_name)
  File.open(targzfile, 'rb') do |file|
    Zlib::GzipReader.wrap(file) do |gz|
      Gem::Package::TarReader.new(gz) do |tar|
        tar.seek(file_name) do |file_entry|
          file_dest_dir = File.join(File.dirname(targzfile),
                                    File.dirname(file_entry.full_name))
          FileUtils.mkdir_p(file_dest_dir)
          File.open(File.join(file_dest_dir, File.basename(file_name)), 'wb') do |file_handle|
            file_handle.write(file_entry.read)
          end
        end
        tar.close
      end
    end
  end
end
has_embargo_date?(embargo_xml) click to toggle source

test for embargo xml cases

# File lib/rof/utility.rb, line 68
def self.has_embargo_date?(embargo_xml)
  return false if embargo_xml == '' || embargo_xml.nil?
  return false unless embargo_xml.elements['machine'].has_elements? && embargo_xml.elements['machine'].elements['date'].has_text?
  true
end
load_items_from_json_file(fname, outfile = STDERR) click to toggle source

@api public @param fname [String] Path to filename @param outfile [#puts] Where to write exceptions @return [Array] The items in the JSON document, coerced into an Array (if a single item was encountered)

# File lib/rof/utility.rb, line 78
def self.load_items_from_json_file(fname, outfile = STDERR)
  items = nil
  File.open(fname, 'r:UTF-8') do |f|
    items = JSON.parse(f.read)
  end
  items = [items] unless items.is_a? Array
  items
rescue JSON::ParserError => e
  outfile.puts("Error reading #{fname}:#{e}")
  exit!(1)
end
new() click to toggle source
# File lib/rof/utility.rb, line 9
def initialize
  @seq = 0
  @workdir = '.'
end
prop_ds(owner, representative = nil) click to toggle source

set 'properties'

# File lib/rof/utility.rb, line 58
def self.prop_ds(owner, representative = nil)
  s = "<fields><depositor>batch_ingest</depositor>\n<owner>#{owner}</owner>\n"
  if representative
    s += "<representative>#{representative}</representative>\n"
  end
  s += "</fields>\n"
  s
end

Public Instance Methods

decode_work_type(obj) click to toggle source

Given an object's type, detrmine and return its af-model

# File lib/rof/utility.rb, line 41
def decode_work_type(obj)
  if obj['type'] =~ WORK_TYPE_WITH_PREFIX_PATTERN
    return 'GenericWork' if Regexp.last_match(2).nil?
    Regexp.last_match(2)
  else
    # this will return nil if key t does not exist
    work_type = obj['type'].downcase
    WORK_TYPES[work_type]
  end
end
next_label() click to toggle source

Issue pid label

# File lib/rof/utility.rb, line 53
def next_label
  "$(pid--#{@seq})".tap { |_| @seq += 1 }
end
set_workdir(filename) click to toggle source

use base directory of given file for workdir

# File lib/rof/utility.rb, line 33
def set_workdir(filename)
  @workdir = File.dirname(filename)
end