Last active
August 29, 2015 14:00
-
-
Save charger/11204997 to your computer and use it in GitHub Desktop.
SftpUploader
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
class SftpUploader | |
def upload!(local_filename, remote_filename) | |
raise "#{local_filename} not readable" unless File.exist?(local_filename) | |
connect do |sftp| | |
dirs = File.dirname(remote_filename).split('/') | |
dir_to_create = credentials.path | |
dirs.each do |dir| | |
dir_to_create = File.join(dir_to_create, dir) | |
sftp.mkdir(dir_to_create) | |
end | |
remote_path = File.join(credentials.path, remote_filename) | |
sftp.upload!(local_filename, remote_path) | |
end | |
end | |
def connect(&block) | |
Net::SFTP.start('host', 'pass') do |sftp| | |
block.call(sftp) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment