module Ddr::Utils

Public Class Methods

digest(content, algorithm) click to toggle source
# File lib/ddr/utils.rb, line 6
def self.digest content, algorithm
  raise TypeError, "Algorithm must be a string: #{algorithm.inspect}" unless algorithm.is_a?(String)
  digest_class = OpenSSL::Digest.const_get(algorithm.sub("-", "").to_sym)
  digest_class.new(content).to_s
rescue NameError => e
  raise ArgumentError, "Invalid algorithm: #{algorithm}"
end
ds_as_of_date_time(ds) click to toggle source
# File lib/ddr/utils.rb, line 79
def self.ds_as_of_date_time(ds)
  ds.create_date_string
end
file_name(file)
Alias for: file_name_for
file_name_for(file) click to toggle source
# File lib/ddr/utils.rb, line 35
def self.file_name_for(file)
  if file.respond_to?(:original_filename) && file.original_filename.present?
    file.original_filename
  else
    File.basename file_path(file)
  end
end
Also aliased as: file_name
file_or_path?(file) click to toggle source
# File lib/ddr/utils.rb, line 14
def self.file_or_path?(file)
  file_path(file)
rescue ArgumentError
  false
end
file_path(file) click to toggle source
# File lib/ddr/utils.rb, line 25
def self.file_path(file)
  if file.respond_to?(:path)
    ::File.absolute_path(file.path)
  elsif file_path?(file)
    file
  else
    raise ArgumentError, "Argument is neither a File nor a path to an existing file."
  end
end
file_path?(file) click to toggle source
# File lib/ddr/utils.rb, line 20
def self.file_path?(file)
  # length is a sanity check
  file.is_a?(String) && (file.length < 1024) && File.exist?(file)
end
file_uri?(uri) click to toggle source
# File lib/ddr/utils.rb, line 43
def self.file_uri?(uri)
  return false unless uri
  URI.parse(uri).scheme == "file"
end
path_from_uri(uri_string) click to toggle source

Return file path for URI string Should reverse .path_to_uri “file:/path/to/file” => “/path/to/file” @param uri [String] The URI string to pathify @return [String] the file path

# File lib/ddr/utils.rb, line 60
def self.path_from_uri(uri_string)
  uri = URI.parse(uri_string)
  unless uri.scheme == "file"
    raise ArgumentError, "URI does not have the file: scheme."
  end
  URI.unescape(uri.path)
end
path_to_uri(path) click to toggle source

Return URI string for file path Should reverse .path_from_uri “/path/to/file” => “file:/path/to/file” @param path [String] the file path @return [String] the file: URI string

# File lib/ddr/utils.rb, line 73
def self.path_to_uri(path)
  uri = URI.parse URI.escape(path)
  uri.scheme = "file"
  uri.to_s
end
sanitize_filename(file_name) click to toggle source
# File lib/ddr/utils.rb, line 48
def self.sanitize_filename(file_name)
  return unless file_name
  raise ArgumentError, "file_name argument must be a string" unless file_name.is_a?(String)
  raise ArgumentError, "file_name argument must not include path" if file_name.include?(File::SEPARATOR)
  file_name.gsub(/[^\w\.\-]/,"_")
end
solr_date(dt) click to toggle source

Returns a string suitable to index as a Solr date @param dt [Date, DateTime, Time] the date/time @return [String]

# File lib/ddr/utils.rb, line 175
def self.solr_date(dt)
  return if dt.nil?
  dt.to_time.utc.iso8601
end
solr_dates(dts) click to toggle source
# File lib/ddr/utils.rb, line 180
def self.solr_dates(dts)
  dts.map { |dt| solr_date(dt) }
end