Class: NVD::JSONFeeds::Schema::CVEFeed

Inherits:
Object
  • Object
show all
Includes:
Enumerable, HasDataVersion
Defined in:
lib/nvd/json_feeds/schema/cve_feed.rb

Overview

Represents the parsed contents of a JSON CVE feed.

Constant Summary collapse

DATA_TYPES =
{
  'CVE' => :CVE
}
DATA_FORMATS =
{
  'MITRE' => :MITRE
}

Constants included from HasDataVersion

HasDataVersion::DATA_VERSIONS

Instance Attribute Summary collapse

Attributes included from HasDataVersion

#data_version

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasDataVersion

included

Constructor Details

#initialize(data_format:, data_type:, data_number_of_cves: nil, data_timestamp: nil, cve_items:, **kwargs) ⇒ CVEFeed

Initializes the JSON feed.

Parameters:

  • data_format (:MITRE)
  • data_type (:CVE)
  • data_number_of_cves (Integer, nil) (defaults to: nil)
  • data_timestamp (DateTime, nil) (defaults to: nil)
  • cve_items (Array<CVEItem>)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nvd/json_feeds/schema/cve_feed.rb', line 70

def initialize(data_format: , data_type: , data_number_of_cves: nil,
                                           data_timestamp: nil,
                                           cve_items: ,
                                           **kwargs)
  super(**kwargs)

  @data_format = data_format
  @data_type   = data_type

  @data_number_of_cves = data_number_of_cves
  @data_timestamp      = data_timestamp

  @cve_items = cve_items
end

Instance Attribute Details

#cve_itemsArray<CVEItem> (readonly)

The CVE items.

Returns:



55
56
57
# File 'lib/nvd/json_feeds/schema/cve_feed.rb', line 55

def cve_items
  @cve_items
end

#data_format:MITRE (readonly) Also known as: format

The feed format.

Returns:

  • (:MITRE)


27
28
29
# File 'lib/nvd/json_feeds/schema/cve_feed.rb', line 27

def data_format
  @data_format
end

#data_number_of_cvesInteger? (readonly) Also known as: number_of_cves

The number of CVEs.

Returns:

  • (Integer, nil)


41
42
43
# File 'lib/nvd/json_feeds/schema/cve_feed.rb', line 41

def data_number_of_cves
  @data_number_of_cves
end

#data_timestampDateTime? (readonly) Also known as: timestamp

The feed timestamp.

Returns:

  • (DateTime, nil)


48
49
50
# File 'lib/nvd/json_feeds/schema/cve_feed.rb', line 48

def data_timestamp
  @data_timestamp
end

#data_type:CVE (readonly) Also known as: type

The feed type.

Returns:

  • (:CVE)


34
35
36
# File 'lib/nvd/json_feeds/schema/cve_feed.rb', line 34

def data_type
  @data_type
end

Class Method Details

.from_json(json) ⇒ Hash{Symbol => Object}

Maps the parsed JSON to a Symbol Hash for #initialize.

Parameters:

  • json (Hash{String => Object})

    The parsed JSON.

Returns:

  • (Hash{Symbol => Object})

    The mapped Symbol Hash.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/nvd/json_feeds/schema/cve_feed.rb', line 94

def self.from_json(json)
  {
    data_type:   DATA_TYPES.fetch(json.fetch('CVE_data_type')),
    data_format: DATA_FORMATS.fetch(json.fetch('CVE_data_format')),
    **super(json),

    data_number_of_cves: json.fetch('CVE_data_numberOfCVEs').to_i,

    data_timestamp: if (timestamp = json['CVE_data_timestamp'])
                      Timestamp.parse(timestamp)
                    end,

    cve_items: json.fetch('CVE_Items').map(&CVEItem.method(:load))
  }
end

.load(json) ⇒ JSONFeed

Loads the JSON feed data from the parsed JSON.

Parameters:

  • json (Hash{String => Object})

    The parsed JSON.

Returns:

  • (JSONFeed)

    The loaded JSON data.



119
120
121
# File 'lib/nvd/json_feeds/schema/cve_feed.rb', line 119

def self.load(json)
  new(**from_json(json))
end

Instance Method Details

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

Enumerates over each CVE item in the feed.

Yields:

  • (cve_itme)

    The given block will be passed each CVE Item.

Yield Parameters:

  • cve_item (CVEItem)

    A CVE Item in the feed.

Returns:

  • (Enumerator)

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



135
136
137
# File 'lib/nvd/json_feeds/schema/cve_feed.rb', line 135

def each(&block)
  @cve_items.each(&block)
end