misc_rbtools/classes/nvd_downloader.rb

41 lines
834 B
Ruby
Raw Normal View History

require 'rest-client'
require 'zlib'
require 'json'
class NvdDownloader
attr_accessor :base_url, :years
MIN_YEAR = '2002'
MAX_YEAR = '2019'
AVAILABLE_YEARS = (MIN_YEAR..MAX_YEAR).to_a
def initialize
@base_url = "https://nvd.nist.gov/feeds/json/cve/1.0/"
@years = self.years
end
def years
year = NvdDownloader::AVAILABLE_YEARS.map do |year|
[year.to_i, year]
end.to_h
end
def year(year)
years[year]
end
def get(url)
r = RestClient.get "#{base_url}#{url}"
r.body if r.code == 200
end
def read_gzip_stream(gzip_stream)
io_stream = StringIO.new(gzip_stream)
gz = Zlib::GzipReader.new(io_stream)
gz.read
end
def parse_json(json_string)
JSON.parse(json_string)
end
end