Class: Jekyll::AgentAudit::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/agent_audit/configuration.rb

Constant Summary collapse

DEFAULTS =
{
  "fail_on" => "error", "format" => "console", "include" => ["**/*"], "exclude" => [],
  "content" => {"selector" => nil, "exclude_selectors" => %w[nav footer aside script style template]},
  "metadata" => {"selectors" => {"author" => nil, "publisher" => nil, "published" => nil, "modified" => nil}},
  "graph" => {"roots" => ["/"]}, "routes" => {"directory_index" => "index.html", "external_paths" => [], "aliases" => {}},
  "rules" => {}, "suppressions" => [],
  "limits" => {"max_html_bytes" => 5_242_880, "max_jsonld_bytes" => 1_048_576, "max_documents" => 50_000, "max_edges" => 1_000_000}
}.freeze
TOP_KEYS =
DEFAULTS.keys.freeze
LIMITS =
%w[max_html_bytes max_jsonld_bytes max_documents max_edges].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_namespace = {}, overrides = {}) ⇒ Configuration

Returns a new instance of Configuration.



23
24
25
26
27
28
# File 'lib/jekyll/agent_audit/configuration.rb', line 23

def initialize(raw_namespace = {}, overrides = {})
  @raw = deep_stringify(raw_namespace || {})
  @overrides = deep_stringify(overrides || {})
  @effective = nil
  validate!
end

Instance Attribute Details

#raw ⇒ Object (readonly)

Returns the value of attribute raw.



21
22
23
# File 'lib/jekyll/agent_audit/configuration.rb', line 21

def raw
  @raw
end

Instance Method Details

#[](key) ⇒ Object



35
36
37
# File 'lib/jekyll/agent_audit/configuration.rb', line 35

def [](key)
  to_h[key.to_s]
end

#enabled_rules ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/jekyll/agent_audit/configuration.rb', line 39

def enabled_rules
  registry = Registry.all
  registry.each_with_object({}) do |(id, ), result|
    next unless [:release] == "M"
    configured = self["rules"][id]
    result[id] =  unless configured.is_a?(Hash) && configured["enabled"] == false
  end
end

#expired_suppressions(now: Date.today) ⇒ Object



69
70
71
# File 'lib/jekyll/agent_audit/configuration.rb', line 69

def expired_suppressions(now: Date.today)
  Array(self["suppressions"]).select { |entry| entry["until"] && Date.iso8601(entry["until"].to_s) < now }
end

#selected?(source_path) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/jekyll/agent_audit/configuration.rb', line 48

def selected?(source_path)
  path = source_path.to_s.sub(%r{\A/+}, "")
  includes = Array(self["include"])
  excludes = Array(self["exclude"])
  includes.any? { |pattern| File.fnmatch?(pattern.to_s, path, File::FNM_PATHNAME | File::FNM_EXTGLOB) } &&
    !excludes.any? { |pattern| File.fnmatch?(pattern.to_s, path, File::FNM_PATHNAME | File::FNM_EXTGLOB) }
end

#suppression_for(rule_id, source_path: nil, fingerprint: nil, now: Date.today) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jekyll/agent_audit/configuration.rb', line 56

def suppression_for(rule_id, source_path: nil, fingerprint: nil, now: Date.today)
  Array(self["suppressions"]).filter_map do |suppression|
    next unless suppression["rule"] == rule_id.to_s
    if suppression["paths"]
      next unless source_path
      next unless Array(suppression["paths"]).any? { |p| File.fnmatch?(p, source_path.to_s, File::FNM_PATHNAME | File::FNM_EXTGLOB) }
    end
    next if suppression["fingerprint"] && suppression["fingerprint"] != fingerprint
    next if suppression["until"] && Date.iso8601(suppression["until"].to_s) < now
    suppression
  end
end

#to_h ⇒ Object



30
31
32
33
# File 'lib/jekyll/agent_audit/configuration.rb', line 30

def to_h
  @effective ||= deep_freeze(deep_merge(DEFAULTS, @raw).merge(cli_values))
  @effective
end