-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partial work toward import rake task
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
1 parent
9be8b36
commit 40bac79
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |