Module: OcflTools::Utils::Inventory

Defined in:
lib/ocfl_tools/utils_inventory.rb

Overview

A module of convenience methods for reading information from an OCFL inventory.json file. Inventory.get_value and its children are designed to account for reading info from the top few lines of a potentially many-MB size file, without having to load it all into memory by ingesting it with OcflInventory.

Class Method Summary collapse

Class Method Details

.get_contentDirectory(inventory_file) ⇒ String

Given an inventory file, return the value of contentDirectory IF FOUND, or ‘content’ if contentDirectory is not set. It explicitly does NOT use the config.content_directory setting for this check.



41
42
43
44
45
# File 'lib/ocfl_tools/utils_inventory.rb', line 41

def self.get_contentDirectory(inventory_file)
  contentDirectory = OcflTools::Utils::Inventory.get_value(inventory_file, 'contentDirectory')
  contentDirectory = 'content' if contentDirectory.nil?
  contentDirectory
end

.get_digestAlgorithm(inventory_file) ⇒ String

Given an inventory file, return the name of the digest algorithm used (e.g. ‘sha512’).



50
51
52
53
54
55
56
57
58
# File 'lib/ocfl_tools/utils_inventory.rb', line 50

def self.get_digestAlgorithm(inventory_file)
  digestAlgorithm = OcflTools::Utils::Inventory.get_value(inventory_file, 'digestAlgorithm')
  if digestAlgorithm.nil?
    # Actually against OCFL spec
    raise "Unable to find value for digestAlgorithm in #{inventory_file}"
  end

  digestAlgorithm
end

.get_fixity(inventory_file) ⇒ Hash or nil

Given an inventory file, return the fixity block (if it exists) or nil.



63
64
65
66
67
68
# File 'lib/ocfl_tools/utils_inventory.rb', line 63

def self.get_fixity(inventory_file)
  inventory = OcflTools::OcflInventory.new.from_file(inventory_file)
  return nil if inventory.fixity.empty?

  inventory.fixity
end

.get_fixity_digestAlgorithms(inventory_file) ⇒ Array or nil

Given an inventory file, return [Array] of the digest types found in the fixity block, or nil.



73
74
75
76
77
78
# File 'lib/ocfl_tools/utils_inventory.rb', line 73

def self.get_fixity_digestAlgorithms(inventory_file)
  inventory = OcflTools::OcflInventory.new.from_file(inventory_file)
  return nil if inventory.fixity.empty?

  inventory.fixity.keys
end

.get_fixity_digests(inventory_file, digestAlgorithm) ⇒ Hash or nil

Given an inventory file and a digestAlgorithm, return [Hash] of digests and [ filepaths ], or nil.



84
85
86
87
88
89
# File 'lib/ocfl_tools/utils_inventory.rb', line 84

def self.get_fixity_digests(inventory_file, digestAlgorithm)
  inventory = OcflTools::OcflInventory.new.from_file(inventory_file)
  return nil if inventory.fixity.empty?

  inventory.fixity[digestAlgorithm]
end

.get_value(inventory_file, key) ⇒ String or nil

Given an inventory file and a key to search for, return the value at that key.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ocfl_tools/utils_inventory.rb', line 13

def self.get_value(inventory_file, key)
  unless %w[contentDirectory digestAlgorithm head type id].include?(key)
    raise OcflTools::Errors::RequestedKeyNotFound, "#{key} is not a valid OCFL inventory header key"
  end

  inventory = OcflTools::OcflInventory.new.from_file(inventory_file)

  case key
    when 'contentDirectory'
      inventory.contentDirectory
    when 'digestAlgorithm'
      inventory.digestAlgorithm
    when 'head'
      inventory.head
    when 'type'
      inventory.type
    when 'id'
      inventory.id
    else
      raise "Unknown key #{key}"
  end

end