Class: ChefLicensing::LicenseFile::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-licensing/license_key_fetcher/license_file/base.rb

Direct Known Subclasses

V3, V4

Constant Summary collapse

EXPECTED_STRUCTURE =
{
  file_format_version: "0.0.0",
  licenses: [
    {
      license_key: String,
      license_type: Symbol,
      update_time: String,
    },
  ],
}.freeze

Class Method Summary collapse

Class Method Details

.load_primary_structureHash

Returns The primary structure of the license file, without nested structures.

Returns:

  • (Hash)

    The primary structure of the license file, without nested structures



44
45
46
47
48
# File 'lib/chef-licensing/license_key_fetcher/license_file/base.rb', line 44

def self.load_primary_structure
  expected_structure_dup = self::EXPECTED_STRUCTURE.dup
  expected_structure_dup[:licenses] = []
  expected_structure_dup
end

.load_structureHash

Returns The complete structure of the license file, including nested structures.

Returns:

  • (Hash)

    The complete structure of the license file, including nested structures



51
52
53
# File 'lib/chef-licensing/license_key_fetcher/license_file/base.rb', line 51

def self.load_structure
  self::EXPECTED_STRUCTURE
end

.migrate_structure(contents, version) ⇒ Hash

Returns The contents of the license file after migration.

Parameters:

  • contents: (Hash)

    The contents of the license file

  • version: (Integer)

    The version of the license file

Returns:

  • (Hash)

    The contents of the license file after migration

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/chef-licensing/license_key_fetcher/license_file/base.rb', line 58

def self.migrate_structure(contents, version)
  raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
end

.verify_structure(data, expected_structure = self::EXPECTED_STRUCTURE) ⇒ Boolean

Note:

This method ignores extra keys in the data that are not in the expected structure

Returns true if the data matches the expected structure, false otherwise.

Parameters:

  • data: (Hash)

    The data to verify

  • expected_structure: (Hash)

    The structure to verify against

Returns:

  • (Boolean)

    true if the data matches the expected structure, false otherwise



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chef-licensing/license_key_fetcher/license_file/base.rb', line 19

def self.verify_structure(data, expected_structure = self::EXPECTED_STRUCTURE)
  return false unless data.is_a?(Hash)

  expected_structure.each do |key, value|
    return false unless data.key?(key)

    if value.is_a?(Hash)
      return false unless verify_structure(data[key], value)
    elsif value.is_a?(Array)
      return false unless data[key].is_a?(Array)

      data[key].each do |item|
        return false unless verify_structure(item, value[0])
      end
    elsif value.is_a?(Class)
      return false unless data[key].is_a?(value)
    else
      return false unless data[key] == value
    end
  end

  true
end