Class: Package::Audit::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/package/audit/models/package.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version, technology, **attr) ⇒ Package

Returns a new instance of Package.



13
14
15
16
17
18
19
20
21
# File 'lib/package/audit/models/package.rb', line 13

def initialize(name, version, technology, **attr)
  @name = name.to_s
  @version = version.to_s
  @technology = technology.to_s
  @groups = []
  @vulnerabilities = []
  @risks = []
  update(**attr)
end

Instance Attribute Details

#groupsObject

Returns the value of attribute groups.



11
12
13
# File 'lib/package/audit/models/package.rb', line 11

def groups
  @groups
end

#latest_versionObject

Returns the value of attribute latest_version.



11
12
13
# File 'lib/package/audit/models/package.rb', line 11

def latest_version
  @latest_version
end

#latest_version_dateObject

Returns the value of attribute latest_version_date.



11
12
13
# File 'lib/package/audit/models/package.rb', line 11

def latest_version_date
  @latest_version_date
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/package/audit/models/package.rb', line 10

def name
  @name
end

#technologyObject (readonly)

Returns the value of attribute technology.



10
11
12
# File 'lib/package/audit/models/package.rb', line 10

def technology
  @technology
end

#versionObject (readonly)

Returns the value of attribute version.



10
11
12
# File 'lib/package/audit/models/package.rb', line 10

def version
  @version
end

#version_dateObject

Returns the value of attribute version_date.



11
12
13
# File 'lib/package/audit/models/package.rb', line 11

def version_date
  @version_date
end

#vulnerabilitiesObject

Returns the value of attribute vulnerabilities.



11
12
13
# File 'lib/package/audit/models/package.rb', line 11

def vulnerabilities
  @vulnerabilities
end

Instance Method Details

#deprecated?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
# File 'lib/package/audit/models/package.rb', line 59

def deprecated?
  risks.each do |risk|
    return true if risk.explanation == Enum::RiskExplanation::POTENTIAL_DEPRECATION
  end
  false
end

#flagsObject



83
84
85
86
87
88
# File 'lib/package/audit/models/package.rb', line 83

def flags
  v = vulnerable? ? 'V' : '·'
  o = outdated? ? 'O' : '·'
  d = deprecated? ? 'D' : '·'
  "#{v}#{o}#{d}"
end

#full_nameObject



23
24
25
# File 'lib/package/audit/models/package.rb', line 23

def full_name
  "#{name}@#{version}"
end

#group_listObject



43
44
45
# File 'lib/package/audit/models/package.rb', line 43

def group_list
  @groups.join(' ')
end

#outdated?Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
# File 'lib/package/audit/models/package.rb', line 66

def outdated?
  risks.each do |risk|
    return true if [
      Enum::RiskExplanation::OUTDATED,
      Enum::RiskExplanation::OUTDATED_BY_MAJOR_VERSION
    ].include?(risk.explanation || '')
  end
  false
end

#riskObject



31
32
33
# File 'lib/package/audit/models/package.rb', line 31

def risk
  risks.max || Risk.new(Enum::RiskType::NONE)
end

#risk?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/package/audit/models/package.rb', line 39

def risk?
  risks.any?
end

#risk_explanationObject



55
56
57
# File 'lib/package/audit/models/package.rb', line 55

def risk_explanation
  risk.explanation
end

#risk_typeObject



51
52
53
# File 'lib/package/audit/models/package.rb', line 51

def risk_type
  risk.type
end

#risksObject



35
36
37
# File 'lib/package/audit/models/package.rb', line 35

def risks
  RiskCalculator.new(self).find
end

#to_csv(fields) ⇒ Object



90
91
92
# File 'lib/package/audit/models/package.rb', line 90

def to_csv(fields)
  fields.map { |field| send(field) }.join(',')
end

#to_sObject



94
95
96
# File 'lib/package/audit/models/package.rb', line 94

def to_s
  "#{@name} #{@version} - [#{@groups.sort.join(', ')}]"
end

#update(**attr) ⇒ Object



27
28
29
# File 'lib/package/audit/models/package.rb', line 27

def update(**attr)
  attr.each { |key, value| instance_variable_set(:"@#{key}", value) }
end

#vulnerabilities_groupedObject



47
48
49
# File 'lib/package/audit/models/package.rb', line 47

def vulnerabilities_grouped
  @vulnerabilities.group_by(&:itself).map { |k, v| "#{k}(#{v.length})" }.join('|')
end

#vulnerable?Boolean

Returns:

  • (Boolean)


76
77
78
79
80
81
# File 'lib/package/audit/models/package.rb', line 76

def vulnerable?
  risks.each do |risk|
    return true if risk.explanation == Enum::RiskExplanation::VULNERABILITY
  end
  false
end