Class: VRT::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/vrt/mapping.rb

Direct Known Subclasses

ThirdPartyLinks

Constant Summary collapse

PARENT_DIR =
'mappings'

Instance Method Summary collapse

Constructor Details

#initialize(scheme, subdirectory = nil) ⇒ Mapping

Returns a new instance of Mapping.



7
8
9
10
11
# File 'lib/vrt/mapping.rb', line 7

def initialize(scheme, subdirectory = nil)
  @scheme = scheme.to_s
  @parent_directory = File.join(self.class::PARENT_DIR, (subdirectory || @scheme))
  load_mappings
end

Instance Method Details

#get(id_list, version) ⇒ Object

returns the most specific value provided in the mapping file for the given vrt id

if no mapping file exists for the given version, the mapping file for the earliest version available will be used



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vrt/mapping.rb', line 16

def get(id_list, version)
  # update the vrt id to the first version we have a mapping file for
  unless @mappings.key?(version)
    id_list = VRT.find_node(vrt_id: id_list.join('.'), preferred_version: @min_version).id_list
    version = @min_version
  end
  mapping = @mappings.dig(version, 'content') || @mappings[version]
  default = @mappings.dig(version, 'metadata', 'default')
  keys = @mappings.dig(version, 'metadata', 'keys')
  if keys
    # Convert mappings with multiple keys to be nested under a single
    # top-level key. Remediation advice has keys 'remediation_advice'
    # and 'references' so we convert it to look like
    # { remediation_advice: { remediation_advice: '...', references: [...] } }
    keys.each_with_object({}) do |key, acc|
      acc[key.to_sym] = get_key(
        id_list: id_list,
        mapping: mapping,
        key: key
      ) || default&.dig(key)
    end
  else
    get_key(id_list: id_list, mapping: mapping, key: @scheme) || default
  end
end