Class: Yarn::Audit::Wrap::AuditParser

Inherits:
Object
  • Object
show all
Defined in:
lib/yarn/audit/wrap/audit_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts:) ⇒ AuditParser

Returns a new instance of AuditParser.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/yarn/audit/wrap/audit_parser.rb', line 7

def initialize opts:
  @opts = opts
  @ignored = {
    info: [],
    low: [],
    moderate: [],
    high: [],
    critical: []
  }
  parse_json_audit
end

Instance Attribute Details

#outputObject

Returns the value of attribute output.



5
6
7
# File 'lib/yarn/audit/wrap/audit_parser.rb', line 5

def output
  @output
end

Instance Method Details

#add_ignored(type:, val:) ⇒ Object



30
31
32
# File 'lib/yarn/audit/wrap/audit_parser.rb', line 30

def add_ignored(type:, val:)
  @ignored[type.to_sym] << val
end

#get_summaryObject



44
45
46
# File 'lib/yarn/audit/wrap/audit_parser.rb', line 44

def get_summary
  @output.detect { |item| item["type"] == "auditSummary" } || {}
end

#parse_json_auditObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/yarn/audit/wrap/audit_parser.rb', line 19

def parse_json_audit
  audit_json = @opts[:audit_json]
  @output = if File.exist?(audit_json)
    File.readlines(audit_json).map do |line|
      JSON.parse(line)
    end
  else
    []
  end
end


48
49
50
51
52
53
54
55
56
57
# File 'lib/yarn/audit/wrap/audit_parser.rb', line 48

def print_summary
  puts "\nOriginal Yarn Audit:"
  s = get_summary["data"] || {}
  s.each { |k, v| puts "#{k.to_s.rjust(23)}: #{v.inspect}" }

  puts "\nSkipped vulnerabilities:"
  @ignored.each do |k, v|
    puts "#{k.to_s.rjust(23)}: #{v.size}"
  end
end

#select(&blk) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/yarn/audit/wrap/audit_parser.rb', line 34

def select(&blk)
  if @output
    @output.select do |item|
      yield item if item["type"] == "auditAdvisory"
    end
  else
    []
  end
end