Created
April 30, 2014 21:04
-
-
Save rjswenson/b93ff6b367f35074e407 to your computer and use it in GitHub Desktop.
A method to pull data conditionally from an external ftp. Checks modified time vs local file before uploading new version.
This file contains hidden or 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
def elastic_sftp | |
local_files = Dir.glob(File.join(Rails.root, 'data', 'data', "**/*")).map {|f| f.split("data/data/")[1] } | |
@sftp = Net::SFTP.start('sftp.vfc.com', ENV['VFC_USERNAME'], :password => ENV['VFC_PASSWORD']) do |sftp| | |
sftp.dir.glob(File.join("#{@sftp_directory}", 'Elastic' , 'Data'), "**/*.txt").each do |file| | |
if local_files.include?(file.name.downcase) | |
if Time.at(file.attributes.mtime) > File.mtime(File.join(Rails.root, 'data', 'data', file.name.downcase)) | |
sftp.download!(File.join("#{@sftp_directory}", 'Elastic' , 'Data', file.name), | |
File.join(Rails.root, 'data', 'data', file.name.downcase)) | |
p "#{file.name} is out of date and has been downloaded." | |
else | |
p "#{file.name} is already up to date." | |
end | |
else | |
sftp.download!(File.join("#{@sftp_directory}", 'Elastic' , 'Data', file.name), | |
File.join(Rails.root, 'data', 'data', file.name.downcase)) | |
p "A new file is being downloaded: #{file.name}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wrote this for Elastic Suite to pull data from an FTP. Ended up not needing it, saving for later.