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.

Parameters:

  • inventory_file (Pathname)

    fully-qualified path to a valid OCFL inventory.json.

Returns:

  • (String)

    the value of content_directory in the JSON, if found, or the OCFL required default value of ‘content’.



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’).

Parameters:

  • inventory_file (Pathname)

    fully-qualified path to a valid OCFL inventory.json.

Returns:

  • (String)

    the string value describing the digest algorithm used in this inventory.



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.

Parameters:

  • inventory_file (Pathname)

    fully-qualified path to a valid OCFL inventory.json.

Returns:

  • (Hash or nil)

    the fixity block from the provided inventory.json, or nil if the inventory does not contain a fixity block.



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.

Parameters:

  • inventory_file (Pathname)

    fully-qualified path to a valid OCFL inventory.json.

Returns:

  • (Array or nil)

    an array of [String] values, with each value being a digest type found in the fixity block, e.g. ‘sha1’, ‘md5’, etc, or nil if no fixity block is found.



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.

Parameters:

  • inventory_file (Pathname)

    fully-qualified path to a valid OCFL inventory.json.

  • digestAlgorithm (String)

    the algorithm used in the fixity block that you want digests for.

Returns:

  • (Hash or nil)

    a hash of digests and filepaths from the fixity block for the given digest type, or nil if the inventory.json does not contain a fixity block.



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.

Parameters:

  • inventory_file (Pathname)

    fully-qualified path to a valid OCFL inventory.json.

  • key (String)

    the JSON key in the inventory file that you want to return a value for.

Returns:

  • (String or nil)

    the value of the requested key, or nil if not found.



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