Class: OCFL::InventoryValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/ocfl/inventory_validator.rb

Overview

Checks to see that the inventory.json and it’s checksum in a direcotory are valid

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory:) ⇒ InventoryValidator

Returns a new instance of InventoryValidator.



6
7
8
# File 'lib/ocfl/inventory_validator.rb', line 6

def initialize(directory:)
  @directory = Pathname.new(directory)
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



10
11
12
# File 'lib/ocfl/inventory_validator.rb', line 10

def directory
  @directory
end

Instance Method Details

#inventory_checksum_fileObject



28
29
30
# File 'lib/ocfl/inventory_validator.rb', line 28

def inventory_checksum_file
  directory / "inventory.json.sha512"
end

#inventory_fileObject



36
37
38
# File 'lib/ocfl/inventory_validator.rb', line 36

def inventory_file
  directory / "inventory.json"
end

#inventory_file_checksumObject



32
33
34
# File 'lib/ocfl/inventory_validator.rb', line 32

def inventory_file_checksum
  Digest::SHA512.file inventory_file
end

#inventory_file_exists?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/ocfl/inventory_validator.rb', line 16

def inventory_file_exists?
  File.exist?(inventory_file)
end

#inventory_file_matches_checksum?Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
# File 'lib/ocfl/inventory_validator.rb', line 20

def inventory_file_matches_checksum?
  return false unless File.exist?(inventory_checksum_file)

  actual = inventory_file_checksum
  expected = File.read(inventory_checksum_file)
  expected.match?(/\A#{actual}\s+inventory\.json\z/)
end

#valid?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/ocfl/inventory_validator.rb', line 12

def valid?
  inventory_file_exists? && inventory_file_matches_checksum?
end