Class: CVEList::YearDir

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

Constant Summary collapse

GLOB =

Dir.glob pattern for Nxxx range directories.

'*xxx'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Directory

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

Constructor Details

#initialize(path) ⇒ YearDir

Initializes the year dir.

Parameters:

  • path (String)

    The path to the year directory.



28
29
30
31
32
# File 'lib/cvelist/year_dir.rb', line 28

def initialize(path)
  super(path)

  @year = File.basename(@path).to_i
end

Instance Attribute Details

#pathString (readonly)

Path to the year directory.

Returns:

  • (String)


15
16
17
# File 'lib/cvelist/year_dir.rb', line 15

def path
  @path
end

#yearInteger (readonly)

The year of the directory.

Returns:

  • (Integer)


20
21
22
# File 'lib/cvelist/year_dir.rb', line 20

def year
  @year
end

Instance Method Details

#[](cve_id) ⇒ CVE?

Loads a CVE.

Parameters:

  • cve_id (String)

    The CVE ID.

Returns:

  • (CVE, nil)

    The loaded CVE or nil if the accompaning range directory for the CVE could not be found.



160
161
162
163
164
165
166
# File 'lib/cvelist/year_dir.rb', line 160

def [](cve_id)
  xxx_range = cve_to_xxx_range(cve_id)

  if has_range?(xxx_range)
    range(xxx_range)[cve_id]
  end
end

#directoriesArray<String>

The xxx number ranges within the directory.

Returns:

  • (Array<String>)


78
79
80
# File 'lib/cvelist/year_dir.rb', line 78

def directories
  glob(GLOB).sort
end

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

Enumerates over each CVE, in each range directory, within the year directory.

Yields:

  • (cve)

    The given block will be passed each CVE in the year dir.

Yield Parameters:

  • cve (CVE)

    A CVE within one of the range directories.

Returns:

  • (Enumerator)

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



105
106
107
108
109
110
111
# File 'lib/cvelist/year_dir.rb', line 105

def each(&block)
  return enum_for(__method__) unless block_given?

  ranges.each do |range_dir|
    range_dir.each(&block)
  end
end

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

Enumerates over every malformed CVE within the year directories.

Yields:

  • (malformed_cve)

    The given block will be passed each malformed CVE from within the year directory.

Yield Parameters:

  • malformed_cve (MalformedCVE)

    A malformed CVE from within the year directory.

Returns:

  • (Enumerator)

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



126
127
128
129
130
131
132
# File 'lib/cvelist/year_dir.rb', line 126

def each_malformed(&block)
  return enum_for(__method__) unless block_given?

  ranges.each do |range_dir|
    range_dir.each_malformed(&block)
  end
end

#has_cve?(cve_id) ⇒ Boolean

Determines whether a CVE exists with the given ID, within any of the range directories, within the year directory.

Parameters:

  • cve_id (String)

    The given CVE ID.

Returns:

  • (Boolean)

    Specifies whether the CVE exists.



144
145
146
147
148
# File 'lib/cvelist/year_dir.rb', line 144

def has_cve?(cve_id)
  xxx_range = cve_to_xxx_range(cve_id)

  has_range?(xxx_range) && range(xxx_range).has_cve?(cve_id)
end

#has_range?(xxx_range) ⇒ Boolean

Determines if the year directory contains the given range directory.

Parameters:

  • xxx_range (String)

    The given range directory ending in xxx.

Returns:

  • (Boolean)


42
43
44
# File 'lib/cvelist/year_dir.rb', line 42

def has_range?(xxx_range)
  directory?(xxx_range)
end

#range(xxx_range) ⇒ RangeDir Also known as: /

Access a range directory within the year directory.

Parameters:

  • xxx_range (String)

    The "xxx" range.

Returns:

Raises:

  • (RangeDirNotFound)

    Could not find the given range directory within the year directory.



58
59
60
61
62
63
64
65
66
# File 'lib/cvelist/year_dir.rb', line 58

def range(xxx_range)
  range_dir_path = join(xxx_range)

  unless File.directory?(range_dir_path)
    raise(RangeDirNotFound,"#{xxx_range.inspect} not found within #{@path.inspect}")
  end

  return RangeDir.new(range_dir_path)
end

#ranges(&block) ⇒ Enumerator

The range directories within the year directory.

Returns:

  • (Enumerator)

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



88
89
90
# File 'lib/cvelist/year_dir.rb', line 88

def ranges(&block)
  directories.map { |dir| RangeDir.new(dir) }
end