data_importer/lib/importers/cna_importer.rb

39 lines
757 B
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
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
2022-04-07 18:15:21 -05:00
puts "Now importing CNAs."
Cna.upsert_all(jsons, unique_by: :cna_id)
2022-04-07 18:15:21 -05:00
end
end