require 'json' require '/data_importer/lib/json_helper.rb' 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 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) end def get_json res = send_request_rest if res.code == 200 json = parse_res(res) JsonHelper.deep_transform_keys(json) 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) } puts "Now importing CNAs." Cna.upsert_all(merged_hashes, unique_by: :cna_id) end end