Class: CVESchema::CVE::ID

Inherits:
Object
  • Object
show all
Defined in:
lib/cve_schema/cve/id.rb

Overview

Represents a CVE ID (ex: CVE-2021-1234).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(year, number) ⇒ ID

Initializes the CVE ID.

Parameters:

  • year (String)

    The year the CVE ID was assigned.

  • number (String)

    The CVE number.



27
28
29
30
# File 'lib/cve_schema/cve/id.rb', line 27

def initialize(year,number)
  @year   = year
  @number = number
end

Instance Attribute Details

#numberString (readonly)

The CVE number.

Returns:

  • (String)


16
17
18
# File 'lib/cve_schema/cve/id.rb', line 16

def number
  @number
end

#yearString (readonly)

The year the CVE ID was assigned.

Returns:

  • (String)


11
12
13
# File 'lib/cve_schema/cve/id.rb', line 11

def year
  @year
end

Class Method Details

.parse(id) ⇒ Object

Parses the CVE ID.

Parameters:

  • id (String)

    The CVE ID string.

Raises:

  • (ArgumentError)

    The given ID was not a valid CVE.



41
42
43
44
45
46
47
48
49
# File 'lib/cve_schema/cve/id.rb', line 41

def self.parse(id)
  cve, year, number = id.split('-',3)

  unless cve == 'CVE'
    raise(ArgumentError,"invalid CVE #{id.inspect}")
  end

  new(year,number)
end

Instance Method Details

#==(other) ⇒ Boolean

Compares the ID with another ID.

Parameters:

  • other (ID)

    The other ID.

Returns:

  • (Boolean)

    Identicates whether the IDs match.



60
61
62
63
64
65
# File 'lib/cve_schema/cve/id.rb', line 60

def ==(other)
  self.class == other.class && (
    @year   == other.year && 
    @number == other.number
  )
end

#to_sString

Converts the CVE ID back into a String.

Returns:

  • (String)

    The full CVE ID (ex: CVE-2021-1234).



73
74
75
# File 'lib/cve_schema/cve/id.rb', line 73

def to_s
  "CVE-#{@year}-#{@number}"
end