require 'json' require '/data_importer/lib/json_helper.rb' 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) 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 puts "Now importing CNAs." Cna.upsert_all(jsons, unique_by: :cna_id) end end