From 9fd17c606aa03f10f9bec2fe28c6ab48d06f6101 Mon Sep 17 00:00:00 2001 From: Brendan McDevitt Date: Mon, 18 Apr 2022 20:01:57 -0500 Subject: [PATCH] made a json helper to help me snake case / symbolize keys for prep to upsert_all into db --- lib/importers/cna_importer.rb | 35 ++++++----------------------------- lib/json_helper.rb | 13 +++++++++++++ 2 files changed, 19 insertions(+), 29 deletions(-) create mode 100644 lib/json_helper.rb diff --git a/lib/importers/cna_importer.rb b/lib/importers/cna_importer.rb index 3c63584..3e1bfad 100644 --- a/lib/importers/cna_importer.rb +++ b/lib/importers/cna_importer.rb @@ -1,6 +1,6 @@ require 'json' +require '/data_importer/lib/json_helper.rb' require 'rest-client' -require 'bulk_insert' class CnaImporter attr_accessor :url @@ -16,46 +16,23 @@ class CnaImporter end def parse_res(response) - JSON.parse(response.body) + JSON.parse(response.body, symbolize_names: true) end def get_json res = send_request_rest if res.code == 200 - parse_res(res) + json = parse_res(res) + JsonHelper.deep_transform_keys(json) else "HTTP Status: #{res.code}" end end - def json_to_hash(json) - data_hash = {} - data_hash[:short_name] = json['shortName'] - data_hash[:cna_id] = json['cnaID'] - data_hash[:organization_name] = json['organizationName'] - data_hash[:scope] = json['scope'] - data_hash[:contact] = json['contact'] - data_hash[:disclosure_policy] = json['disclosurePolicy'] - data_hash[:security_advisories] = json['securityAdvisories'] - data_hash[:resources] = json['resources'] - data_hash[:cna] = json['CNA'] - data_hash[:country] = json['country'] - data_hash - end - - def bulk_insert(cves) - Cna.bulk_insert do |worker| - cves.each do |attrs| - worker.add(attrs) - end - end - end - def import - json = get_json - attrs = json.map {|j| json_to_hash(j) } + jsons = get_json puts "Now importing CNAs." - bulk_insert(attrs) + Cna.upsert_all(jsons, unique_by: :cna_id) end end diff --git a/lib/json_helper.rb b/lib/json_helper.rb new file mode 100644 index 0000000..88fe8d6 --- /dev/null +++ b/lib/json_helper.rb @@ -0,0 +1,13 @@ +class JsonHelper + def self.deep_transform_keys(json_hash) + if json_hash.is_a? Array + json_hash.map {|jh| symbolize_names_snake_case(jh) } + else + symbolize_names_snake_case(json_hash) + end + end + + def self.symbolize_names_snake_case(json_hash) + json_hash.deep_transform_keys {|k| k.to_s.underscore.to_sym } + end +end