Class: CVEList::RangeDir

Inherits:
Directory show all
Includes:
Enumerable
Defined in:
lib/cvelist/range_dir.rb

Constant Summary collapse

GLOB =

Dir.glob pattern for all CVE .json files.

'CVE-[0-9][0-9][0-9][0-9]-*.json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Directory

#directory?, #file?, #glob, #join, #to_s

Constructor Details

#initialize(path) ⇒ RangeDir

Initializes the range directory.

Parameters:

  • path (String)

    The path to the range directory.



25
26
27
28
29
# File 'lib/cvelist/range_dir.rb', line 25

def initialize(path)
  super(path)

  @range = File.basename(@path)
end

Instance Attribute Details

#pathString (readonly)

Returns:

  • (String)


14
15
16
# File 'lib/cvelist/range_dir.rb', line 14

def path
  @path
end

#rangeString (readonly)

Returns:

  • (String)


17
18
19
# File 'lib/cvelist/range_dir.rb', line 17

def range
  @range
end

Instance Method Details

#[](cve_id) ⇒ CVE?

Loads a CVE.

Parameters:

  • cve_id (String)

    The CVE ID.

Returns:

  • (CVE, nil)

    The loaded CVE of nil if the CVE could not be found within the range directory.



53
54
55
56
57
58
59
60
# File 'lib/cvelist/range_dir.rb', line 53

def [](cve_id)
  cve_file = "#{cve_id}.json"
  cve_path = join(cve_file)

  if File.file?(cve_path)
    CVE.load(cve_path)
  end
end

#each {|cve| ... } ⇒ Enumerator

Enumerates over the CVEs in the range directory.

Yields:

  • (cve)

    The given block will be passed each CVE in the range directory.

Yield Parameters:

  • cve (CVE)

    A CVE within the range directory.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.



86
87
88
89
90
91
92
93
94
95
# File 'lib/cvelist/range_dir.rb', line 86

def each
  return enum_for(__method__) unless block_given?

  files.each do |cve_path|
    begin
      yield CVE.load(cve_path)
    rescue InvalidJSON
    end
  end
end

#each_malformed {|malformed| ... } ⇒ Enumerator

Enumerates over the malformed CVEs within the range directory.

Yields:

  • (malformed)

    The given block will be passed each malformed

Yield Parameters:

Returns:

  • (Enumerator)

    If no block is given, an Enumerator object will be returned.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cvelist/range_dir.rb', line 108

def each_malformed
  return enum_for(__method__) unless block_given?

  files.each do |cve_path|
    begin
      CVE.load(cve_path)
    rescue InvalidJSON => error
      yield MalformedCVE.new(cve_path,error)
    end
  end
end

#filesArray<String>

The JSON files within the range directory.

Returns:

  • (Array<String>)


70
71
72
# File 'lib/cvelist/range_dir.rb', line 70

def files
  glob(GLOB).sort
end

#has_cve?(cve_id) ⇒ Boolean

Determines whether the range directory contains the given CVE ID.

Parameters:

  • cve_id (String)

    The given CVE ID.

Returns:

  • (Boolean)


39
40
41
# File 'lib/cvelist/range_dir.rb', line 39

def has_cve?(cve_id)
  file?("#{cve_id}.json")
end