Skip to content

Commit

Permalink
Partial work toward import rake task
Browse files Browse the repository at this point in the history
This defines a rake task for importing SuggestedResource records from
a CSV file, which can be provided either as a local file or loaded from
a remote URL (via the HTTP or HTTPS protocols).

Committing this now to see what it does for test coverage, as I want to
investigate testing these tasks.
  • Loading branch information
matt-bernhardt committed Aug 2, 2024
1 parent eec3780 commit fde90f4
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions lib/tasks/suggested_resources.rake
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fde90f4

Please sign in to comment.