Module: Ddr::Utils

Extended by:
Deprecation
Defined in:
lib/ddr/utils.rb

Class Method Summary collapse

Class Method Details

.digest(content, algorithm) ⇒ Object



6
7
8
9
10
11
12
# 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) ⇒ Object



79
80
81
# File 'lib/ddr/utils.rb', line 79

def self.ds_as_of_date_time(ds)
  ds.create_date_string
end

.file_name_for(file) ⇒ Object Also known as: file_name



35
36
37
38
39
40
41
# 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

.file_or_path?(file) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
# File 'lib/ddr/utils.rb', line 14

def self.file_or_path?(file)
  file_path(file)
rescue ArgumentError
  false
end

.file_path(file) ⇒ Object



25
26
27
28
29
30
31
32
33
# 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) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
# 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) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# 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) ⇒ String

Return file path for URI string Should reverse .path_to_uri “file:/path/to/file” => “/path/to/file”

Parameters:

  • uri (String)

    The URI string to pathify

Returns:

  • (String)

    the file path



60
61
62
63
64
65
66
# 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) ⇒ String

Return URI string for file path Should reverse .path_from_uri “/path/to/file” => “file:/path/to/file”

Parameters:

  • path (String)

    the file path

Returns:

  • (String)

    the file: URI string



73
74
75
76
77
# 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) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
# 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) ⇒ String

Returns a string suitable to index as a Solr date

Parameters:

  • dt (Date, DateTime, Time)

    the date/time

Returns:

  • (String)


175
176
177
178
# 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) ⇒ Object



180
181
182
# File 'lib/ddr/utils.rb', line 180

def self.solr_dates(dts)
  dts.map { |dt| solr_date(dt) }
end