Class: InspecTools::Summary

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

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Summary

Returns a new instance of Summary.



27
28
29
30
31
32
33
34
35
# File 'lib/inspec_tools/summary.rb', line 27

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.



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

def json
  @json
end

#json_countsObject (readonly)

Returns the value of attribute json_counts.



21
22
23
# File 'lib/inspec_tools/summary.rb', line 21

def json_counts
  @json_counts
end

#json_fullObject (readonly)

Returns the value of attribute json_full.



20
21
22
# File 'lib/inspec_tools/summary.rb', line 20

def json_full
  @json_full
end

#summaryObject (readonly)

Returns the value of attribute summary.



24
25
26
# File 'lib/inspec_tools/summary.rb', line 24

def summary
  @summary
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



25
26
27
# File 'lib/inspec_tools/summary.rb', line 25

def threshold
  @threshold
end

#threshold_fileObject (readonly)

Returns the value of attribute threshold_file.



22
23
24
# File 'lib/inspec_tools/summary.rb', line 22

def threshold_file
  @threshold_file
end

#threshold_inlineObject (readonly)

Returns the value of attribute threshold_inline.



23
24
25
# File 'lib/inspec_tools/summary.rb', line 23

def threshold_inline
  @threshold_inline
end

Instance Method Details

#output_summaryObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/inspec_tools/summary.rb', line 37

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)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/inspec_tools/summary.rb', line 53

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