diff --git a/lib/tasks/suggested_resources.rake b/lib/tasks/suggested_resources.rake new file mode 100644 index 0000000..fbab52a --- /dev/null +++ b/lib/tasks/suggested_resources.rake @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +# These define tasks for managing our SuggestedResource records. +namespace :suggested_resources do + # While we intend to use Dataclips for exporting these records when needed, + # we do need a way to import records from a CSV file. + desc 'Replace all Suggested Resources from CSV' + task :reload, [:addr] => :environment do |_task, args| + raise ArgumentError.new, 'File is required' unless args.addr.present? + + Rails.logger.info('Reloading all Suggested Resource records from CSV') + Rails.logger.info("Record count before we reload: #{Detector::SuggestedResource.count}") + + if URI(args.addr).scheme + Rails.logger.info("Loading from remote address: #{args.addr}") + url = URI.parse(args.addr) + raise ArgumentError.new, 'HTTP/HTTPS scheme is required' unless url.scheme.in?(%w[http https]) + Rails.logger.info(url) + + file = url.read + Rails.logger.info(file) + # Need to connect to a CSV content type + # Invalid parsing should... do something? + else + Rails.logger.info("Loading from local file: #{args.addr}") + file = File.read(args.addr) + Rails.logger.info(file) + # Invalid / not found file should ... do something? + end + + Rails.logger.info('Now ready to parse a CSV') + data = CSV.parse(file) + Rails.logger.info(data) + + # Rails.logger.info("Record count after we reload: #{Detector::SuggestedResource.count}") + end +end