Load survey responses from sftp folder

This commit is contained in:
rebuilt 2023-01-03 15:21:13 -08:00
parent 1426e7cc63
commit 03c6bff7bb
38 changed files with 128 additions and 147272 deletions

View file

@ -0,0 +1,28 @@
require 'net/sftp'
require 'uri'
require 'csv'
module Sftp
class Directory
def self.open(path: '/data/survey_responses/', &block)
sftptogo_url = ENV['SFTPTOGO_URL']
puts sftptogo_url
uri = URI.parse(sftptogo_url)
Net::SFTP.start(uri.host, uri.user, password: uri.password) do |sftp|
sftp.dir.foreach(path) do |entry|
filename = entry.name
puts filename
sftp.file.open(filepath(path:, filename:), 'r', &block)
end
end
end
def self.filepath(path:, filename:)
path += '/' unless path.end_with?('/')
"#{path}#{filename}"
end
private_class_method :filepath
end
end

View file

@ -1,26 +0,0 @@
require 'net/sftp'
require 'uri'
require 'csv'
module Sftp
class Downloader
def initialize
sftptogo_url = ENV['SFTPTOGO_URL']
uri = URI.parse(sftptogo_url)
Net::SFTP.start(uri.host, uri.user, password: uri.password) do |sftp|
# download a file or directory from the remote host
# open and read from a pseudo-IO for a remote file
# sftp.file.open('/mciea/data/canary.csv', 'r') do |f|
# CSV.parse(f.read) do |row|
# puts row
# end
# end
# list the entries in a directory
sftp.dir.foreach('/mciea/data/') do |entry|
puts entry.longname
end
end
end
end
end

View file

@ -0,0 +1,33 @@
require 'net/sftp'
require 'uri'
require 'csv'
module Sftp
class RaceLoader
def self.load_data(path: '/data/survey_responses/')
SurveyItemResponse.update_all(student_id: nil)
StudentRace.delete_all
Student.delete_all
sftptogo_url = ENV['SFTPTOGO_URL']
uri = URI.parse(sftptogo_url)
Net::SFTP.start(uri.host, uri.user, password: uri.password) do |sftp|
sftp.dir.foreach(path) do |entry|
filename = entry.name
puts filename
sftp.file.open(filepath(path:, filename:), 'r') do |f|
StudentLoader.from_file(file: f, rules: [Rule::SkipNonLowellSchools])
end
end
end
end
def self.filepath(path:, filename:)
path += '/' unless path.end_with?('/')
"#{path}#{filename}"
end
private_class_method :filepath
end
end