Class: Bundler::Audit::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/audit/configuration.rb

Overview

Class for storing and validating configuration for running the auditor.

Since:

  • 0.8.0

Defined Under Namespace

Classes: FileNotFound, InvalidConfigurationError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Configuration

Initializes the configuration.

Parameters:

  • config (Hash) (defaults to: {})

    The configuration hash.

Options Hash (config):

  • :ignore (Array<String>)

    The list of advisory IDs to ignore.

Since:

  • 0.8.0



102
103
104
# File 'lib/bundler/audit/configuration.rb', line 102

def initialize(config={})
  @ignore = Set.new(config[:ignore])
end

Instance Attribute Details

#ignoreSet<String> (readonly)

The list of advisory IDs to ignore.

Returns:

  • (Set<String>)

Since:

  • 0.8.0



91
92
93
# File 'lib/bundler/audit/configuration.rb', line 91

def ignore
  @ignore
end

Class Method Details

.load(file_path) ⇒ Configuration

A constructor method for loading configuration from a YAML file.

Parameters:

  • file_path (String)

    Path to the YAML file holding the configuration.

Returns:

  • (Configuration)

    A Configuration object containing the config hash loaded from the file passed.

Raises:

  • (FileNotFound)

    Will raise a file not found error when the path to the configuration YAML file does not exist.

  • (InvalidConfigurationError)

    Will raise an invalid configuration error indicating what in the YAML file is invalid for the configuration.

Since:

  • 0.8.0



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/bundler/audit/configuration.rb', line 53

def self.load(file_path)
  raise(FileNotFound,"Configuration file '#{file_path}' does not exist") unless File.exist?(file_path)

  doc = YAML.parse(File.new(file_path))

  unless doc.kind_of?(YAML::Nodes::Document)
    raise(InvalidConfigurationError,"Configuration found in '#{file_path}' is not YAML")
  end

  unless doc.root.kind_of?(YAML::Nodes::Mapping)
    raise(InvalidConfigurationError,"Configuration found in '#{file_path}' is not a Hash")
  end

  config = {}

  doc.root.children.each_slice(2) do |key,value|
    case key.value
    when 'ignore'
      unless value.is_a?(YAML::Nodes::Sequence)
        raise(InvalidConfigurationError,"'ignore' key found in config file, but is not an Array")
      end

      unless value.children.all? { |node| node.is_a?(YAML::Nodes::Scalar) }
        raise(InvalidConfigurationError,"'ignore' array in config file contains a non-String")
      end

      config[:ignore] = value.children.map(&:value)
    end
  end

  new(config)
end