data_importer/lib/importers/cna_importer.rb

56 lines
1.1 KiB
Ruby
Raw Normal View History

2022-04-07 18:15:21 -05:00
require 'json'
require '/data_importer/lib/json_helper.rb'
2022-04-07 18:15:21 -05:00
require 'rest-client'
class CnaImporter
EXPECTED_KEYS = [
:short_name,
:cna_id,
:organization_name,
:scope,
:contact,
:disclosure_policy,
:security_advisories,
:resources,
:cna,
:country
].freeze
EMPTY_HASH = EXPECTED_KEYS.map {|k| [k, nil] }.to_h.freeze
2022-04-07 18:15:21 -05:00
attr_accessor :url
def initialize
@url = 'https://raw.githubusercontent.com/CVEProject/cve-website/dev/src/assets/data/CNAsList.json'
end
def send_request_rest
RestClient::Request.execute(
method: :get,
url: url
)
end
def parse_res(response)
JSON.parse(response.body, symbolize_names: true)
2022-04-07 18:15:21 -05:00
end
def get_json
res = send_request_rest
if res.code == 200
json = parse_res(res)
JsonHelper.deep_transform_keys(json)
2022-04-07 18:15:21 -05:00
else
"HTTP Status: #{res.code}"
end
end
def import
jsons = get_json
merged_hashes = jsons.map {|h| h.slice(*EXPECTED_KEYS).reverse_merge(EMPTY_HASH) }
2022-04-07 18:15:21 -05:00
puts "Now importing CNAs."
Cna.upsert_all(merged_hashes, unique_by: :cna_id)
2022-04-07 18:15:21 -05:00
end
end