class Siba::Destination::Ftp::Worker
Attributes
directory[RW]
host[RW]
passive[RW]
password[RW]
user[RW]
Public Class Methods
new(host, user, password, directory, passive)
click to toggle source
# File lib/siba-destination-ftp/worker.rb, line 14 def initialize(host, user, password, directory, passive) @host = host @host = ENV[Siba::Destination::Ftp::Init::DEFAULT_FTP_HOST_ENV_NAME] if host.nil? @user = user @password = password @directory = directory || "/" @passive = passive logger.info "Connecting to FTP server: #{host}" check_connection end
Public Instance Methods
cd(ftp)
click to toggle source
# File lib/siba-destination-ftp/worker.rb, line 92 def cd(ftp) directory.gsub! "\\","/" begin ftp.chdir(subdir) # try to change to full subdir first rescue directories = directory.split("/") directories.each do |subdir| ftp.mkdir(subdir) rescue nil ftp.chdir(subdir) end end end
check_connection()
click to toggle source
# File lib/siba-destination-ftp/worker.rb, line 27 def check_connection begin connect do |ftp| test_file ftp logger.debug("FTP connection verified") end rescue logger.error "Failed to connect to FTP server: #{user_host_and_dir}. Please ensure your username/password are correct and you have read/write access to your FTP directory." raise end end
connect(&block)
click to toggle source
# File lib/siba-destination-ftp/worker.rb, line 116 def connect(&block) siba_file.run_this do ftp = nil begin ftp = Net::FTP.open(host, user, password) ftp.passive = passive cd ftp block.call(ftp) ensure unless ftp.nil? ftp.close rescue nil end end end end
connect_and_delete_file(remote_file_name)
click to toggle source
# File lib/siba-destination-ftp/worker.rb, line 82 def connect_and_delete_file(remote_file_name) connect do |ftp| delete_file ftp, remote_file_name end end
connect_and_get_file(remote_file_name, path_to_destination_file)
click to toggle source
# File lib/siba-destination-ftp/worker.rb, line 72 def connect_and_get_file(remote_file_name, path_to_destination_file) connect do |ftp| get_file ftp, remote_file_name, path_to_destination_file end end
connect_and_put_file(path_to_file)
click to toggle source
# File lib/siba-destination-ftp/worker.rb, line 62 def connect_and_put_file(path_to_file) connect do |ftp| put_file ftp, path_to_file end end
delete_file(ftp, remote_file_name)
click to toggle source
# File lib/siba-destination-ftp/worker.rb, line 88 def delete_file(ftp, remote_file_name) ftp.delete(remote_file_name) end
get_file(ftp, remote_file_name, path_to_destination_file)
click to toggle source
# File lib/siba-destination-ftp/worker.rb, line 78 def get_file(ftp, remote_file_name, path_to_destination_file) ftp.getbinaryfile remote_file_name, path_to_destination_file end
get_files_list(prefix)
click to toggle source
# File lib/siba-destination-ftp/worker.rb, line 46 def get_files_list(prefix) connect do |ftp| list = [] ftp.list do |e| entry = Net::FTP::List.parse(e) # Ignore everything that's not a file (so symlinks, directories and devices etc.) next unless entry.file? next unless entry.basename =~ /^#{prefix}/ list << [entry.basename, entry.mtime] end list end end
put_file(ftp, path_to_file)
click to toggle source
# File lib/siba-destination-ftp/worker.rb, line 68 def put_file(ftp, path_to_file) ftp.putbinaryfile path_to_file end
test_file(ftp)
click to toggle source
# File lib/siba-destination-ftp/worker.rb, line 105 def test_file(ftp) src_file = Siba::TestFiles.prepare_test_file "test_ftp" put_file ftp, src_file src_to_check = Siba::TestFiles.generate_path "test_ftp_check" remote_file_name = File.basename(src_file) get_file ftp, remote_file_name, src_to_check raise Siba::Error, "Failed to get test file" unless File.file? src_to_check raise Siba::Error, "Error getting test files" unless FileUtils.compare_file(src_file, src_to_check) delete_file ftp, remote_file_name end
user_host_and_dir()
click to toggle source
# File lib/siba-destination-ftp/worker.rb, line 40 def user_host_and_dir str = "#{user.nil? ? "" : user + "@"}#{host}" str += ", dir: '#{directory}'" unless directory.nil? || directory == "/" str end