Created
July 9, 2018 04:07
-
-
Save kiku1705/7c7dff4d9e3710ce3ed5802283e8dedb to your computer and use it in GitHub Desktop.
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
sftp = Net::SFTP.start(@sftp_details['server_ip'], @sftp_details['server_username'], :password => decoded_pswd) | |
if sftp | |
begin | |
sftp.dir.foreach(@sftp_details['server_folder_path']) do |entry| | |
print_memory_usage do | |
print_time_spent do | |
if entry.file? && entry.name.end_with?("csv") | |
batch_size_cnt = 0 | |
sftp.file.open("#{@sftp_details['server_folder_path']}/#{entry.name}") do |file| | |
header = file.gets | |
header = header.force_encoding(header.encoding).encode('UTF-8', invalid: :replace, undef: :replace, replace: '') | |
csv_data = '' | |
while line = file.gets | |
batch_size_cnt += 1 | |
csv_data.concat(line.force_encoding(line.encoding).encode('UTF-8', invalid: :replace, undef: :replace, replace: '')) | |
if batch_size_cnt == 1000 || file.eof? | |
CSV.parse(csv_data, {headers: header, write_headers: true}) do |row| | |
row.delete(nil) | |
entities << row.to_hash | |
end | |
csv_data, batch_size_cnt = '', 0 | |
courses.delete_if(&:blank?) | |
# DO PROCESSING PART | |
entities = [] | |
end | |
end if header | |
end | |
sftp.rename("#{@sftp_details['server_folder_path']}/#{entry.name}", "#{@sftp_details['processed_file_path']}/#{entry.name}") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment