Class: SvgSentinel::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/svg_sentinel/config.rb

Overview

Loads scan settings from a .svg_sentinel.yml file (or a plain Hash) so a team can pin a profile, a rendering context, size limits, and - most usefully - per-finding severity, without forking the source.

# .svg_sentinel.yml
profile: strict
context: img
allow_external: false
severity:
external_ref: error     # treat as critical in this repo
raster_image: ignore    # we accept raster logos
disabled:
- css_import

to_scan_options returns a kwargs Hash ready to splat into SvgSentinel.scan. Only keys present in the config are returned, so caller (e.g. CLI) flags can be merged on top and win.

Constant Summary collapse

OPTION_KEYS =
%i[
  profile context allow_external allow_data_uri
  max_bytes hard_max_bytes max_depth max_nodes max_attributes
].freeze
SYMBOL_KEYS =
%i[profile context].freeze
FILENAME =
".svg_sentinel.yml"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Config

Returns a new instance of Config.



48
49
50
# File 'lib/svg_sentinel/config.rb', line 48

def initialize(data = {})
  @data = symbolize_keys(data)
end

Class Method Details

.discover(dir = Dir.pwd) ⇒ Object



43
44
45
46
# File 'lib/svg_sentinel/config.rb', line 43

def self.discover(dir = Dir.pwd)
  path = File.join(dir, FILENAME)
  File.file?(path) ? load_file(path) : new({})
end

.load_file(path) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/svg_sentinel/config.rb', line 34

def self.load_file(path)
  data = YAML.safe_load_file(path) || {}
  raise ConfigError, "#{path}: expected a mapping" unless data.is_a?(Hash)

  new(data)
rescue Psych::SyntaxError => e
  raise ConfigError, "#{path}: invalid YAML (#{e.message})"
end

Instance Method Details

#to_scan_optionsObject



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/svg_sentinel/config.rb', line 52

def to_scan_options
  opts = {}
  OPTION_KEYS.each do |key|
    next unless @data.key?(key)

    value = @data[key]
    opts[key] = (SYMBOL_KEYS.include?(key) && value) ? value.to_sym : value
  end
  opts[:severity_overrides] = symbolize_keys(@data[:severity]) if @data.key?(:severity)
  opts[:disabled] = Array(@data[:disabled]).map(&:to_sym) if @data.key?(:disabled)
  opts
end