Class: Friday::Dependency::Versioned

Inherits:
Object
  • Object
show all
Defined in:
app/models/friday/dependency.rb

Overview

A specific version of a Dependency

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dependency, version, vulnerability_status = nil) ⇒ Versioned

Returns a new instance of Versioned.



28
29
30
31
32
# File 'app/models/friday/dependency.rb', line 28

def initialize(dependency, version, vulnerability_status = nil)
  @dependency = dependency
  @version = version
  @vulnerability_status = vulnerability_status
end

Instance Attribute Details

#dependencyObject (readonly)

Returns the value of attribute dependency.



24
25
26
# File 'app/models/friday/dependency.rb', line 24

def dependency
  @dependency
end

#versionObject (readonly)

Returns the value of attribute version.



24
25
26
# File 'app/models/friday/dependency.rb', line 24

def version
  @version
end

Class Method Details

.from_redis(key, criticality_score) ⇒ Object



9
10
11
12
13
14
15
# File 'app/models/friday/dependency.rb', line 9

def from_redis(key, criticality_score)
  vulnerability_status = CRITICALITIES[criticality_score]

  language, name, version = parse_key(key)

  new(Dependency.new(language, name), version, vulnerability_status)
end

.parse_key(key) ⇒ Object



17
18
19
20
21
# File 'app/models/friday/dependency.rb', line 17

def parse_key(key)
  # I think the dependency name will have to be base64 encoded because java
  # dependencies appear to have : in the name
  key.split(':')
end

Instance Method Details

#==(other) ⇒ Object



83
84
85
# File 'app/models/friday/dependency.rb', line 83

def ==(other)
  self.class == other.class && to_s == other.to_s
end

#add_app(id) ⇒ Object



42
43
44
45
46
47
48
49
# File 'app/models/friday/dependency.rb', line 42

def add_app(id)
  criticality_score = CRITICALITIES.index(vulnerability_status)

  Friday.redis.zadd('dependencies', 0, dependency.to_s)
  Friday.redis.zadd('vulnerabilities', criticality_score, to_s)
  Friday.redis.zadd(dependency.versions_key, 0, version)
  Friday.redis.zadd(dependents_key, 0, id.to_s)
end

#dependentsObject



34
35
36
# File 'app/models/friday/dependency.rb', line 34

def dependents
  Friday.redis.zrange(dependents_key, 0, -1)
end

#remove_app(id) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/models/friday/dependency.rb', line 51

def remove_app(id)
  script = <<-LUA
    redis.call("ZREM", KEYS[1], ARGV[1])

    if redis.call("EXISTS", KEYS[1]) == 1 then return end

    redis.call("ZREM", KEYS[2], ARGV[2])
    redis.call("ZREM", "vulnerabilities", ARGV[3])

    if redis.call("EXISTS", KEYS[2]) == 1 then return end

    redis.call("ZREM", "dependencies", ARGV[4])
  LUA

  Friday.redis.eval(
    script,
    [dependents_key, dependency.versions_key],
    [id.to_s, version, to_s, dependency.to_s]
  )
end

#to_hashObject



72
73
74
75
76
77
# File 'app/models/friday/dependency.rb', line 72

def to_hash
  @dependency.as_json.merge!(
    version: @version,
    vulnerability_status: @vulnerability_status
  )
end

#to_sObject



79
80
81
# File 'app/models/friday/dependency.rb', line 79

def to_s
  "#{@dependency}:#{version}"
end

#vulnerability_statusObject



38
39
40
# File 'app/models/friday/dependency.rb', line 38

def vulnerability_status
  @vulnerability_status ||= CRITICALITIES[criticality_index]
end