Class: InspecTools::Summary

Inherits:
Object show all
Defined in:
lib/inspec_tools/summary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Summary

Returns a new instance of Summary.



20
21
22
23
24
25
26
27
28
# File 'lib/inspec_tools/summary.rb', line 20

def initialize(**options)
  options = options[:options]
  @json = JSON.parse(File.read(options[:inspec_json]))
  @json_full = false || options[:json_full]
  @json_counts = false || options[:json_counts]
  @threshold = parse_threshold(options[:threshold_inline], options[:threshold_file])
  @threshold_provided = options[:threshold_inline] || options[:threshold_file]
  @summary = compute_summary
end

Instance Attribute Details

#jsonObject (readonly)

Returns the value of attribute json.



18
19
20
# File 'lib/inspec_tools/summary.rb', line 18

def json
  @json
end

#json_countsObject (readonly)

Returns the value of attribute json_counts.



18
19
20
# File 'lib/inspec_tools/summary.rb', line 18

def json_counts
  @json_counts
end

#json_fullObject (readonly)

Returns the value of attribute json_full.



18
19
20
# File 'lib/inspec_tools/summary.rb', line 18

def json_full
  @json_full
end

#summaryObject (readonly)

Returns the value of attribute summary.



18
19
20
# File 'lib/inspec_tools/summary.rb', line 18

def summary
  @summary
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



18
19
20
# File 'lib/inspec_tools/summary.rb', line 18

def threshold
  @threshold
end

#threshold_fileObject (readonly)

Returns the value of attribute threshold_file.



18
19
20
# File 'lib/inspec_tools/summary.rb', line 18

def threshold_file
  @threshold_file
end

#threshold_inlineObject (readonly)

Returns the value of attribute threshold_inline.



18
19
20
# File 'lib/inspec_tools/summary.rb', line 18

def threshold_inline
  @threshold_inline
end

Instance Method Details

#output_summaryObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/inspec_tools/summary.rb', line 30

def output_summary
  unless @json_full || @json_counts
    puts "\nThreshold compliance: #{@threshold['compliance.min']}%"
    puts "\nOverall compliance: #{@summary[:compliance]}%\n\n"
    @summary[:status].keys.each do |category|
      puts category
      @summary[:status][category].keys.each do |impact|
        puts "\t#{impact} : #{@summary[:status][category][impact]}"
      end
    end
  end

  puts @summary.to_json if @json_full
  puts @summary[:status].to_json if @json_counts
end

#results_meet_threshold?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/inspec_tools/summary.rb', line 46

def results_meet_threshold?
  raise 'Please provide threshold as a yaml file or inline yaml' unless @threshold_provided

  compliance = true
  failure = []
  failure << check_max_compliance(@threshold['compliance.max'], @summary[:compliance], '', 'compliance')
  failure << check_min_compliance(@threshold['compliance.min'], @summary[:compliance], '', 'compliance')

  BUCKETS.each do |bucket|
    TALLYS.each do |tally|
      failure << check_min_compliance(@threshold["#{bucket}.#{tally}.min"], @summary[:status][bucket][tally], bucket, tally)
      failure << check_max_compliance(@threshold["#{bucket}.#{tally}.max"], @summary[:status][bucket][tally], bucket, tally)
    end
  end

  failure.reject!(&:nil?)
  compliance = false if failure.length.positive?
  output(compliance, failure)
  compliance
end