Class: Siba::Destination::Ftp::Worker
- Inherits:
-
Object
- Object
- Siba::Destination::Ftp::Worker
- Includes:
- FilePlug, LoggerPlug
- Defined in:
- lib/siba-destination-ftp/worker.rb
Instance Attribute Summary collapse
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#host ⇒ Object
Returns the value of attribute host.
-
#passive ⇒ Object
Returns the value of attribute passive.
-
#password ⇒ Object
Returns the value of attribute password.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #cd(ftp) ⇒ Object
- #check_connection ⇒ Object
- #connect(&block) ⇒ Object
- #connect_and_delete_file(remote_file_name) ⇒ Object
- #connect_and_get_file(remote_file_name, path_to_destination_file) ⇒ Object
- #connect_and_put_file(path_to_file) ⇒ Object
- #delete_file(ftp, remote_file_name) ⇒ Object
- #get_file(ftp, remote_file_name, path_to_destination_file) ⇒ Object
- #get_files_list(prefix) ⇒ Object
-
#initialize(host, user, password, directory, passive) ⇒ Worker
constructor
A new instance of Worker.
- #put_file(ftp, path_to_file) ⇒ Object
- #test_file(ftp) ⇒ Object
- #user_host_and_dir ⇒ Object
Constructor Details
#initialize(host, user, password, directory, passive) ⇒ Worker
Returns a new instance of Worker.
14 15 16 17 18 19 20 21 22 23 24 25 |
# 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 |
Instance Attribute Details
#directory ⇒ Object
Returns the value of attribute directory.
12 13 14 |
# File 'lib/siba-destination-ftp/worker.rb', line 12 def directory @directory end |
#host ⇒ Object
Returns the value of attribute host.
12 13 14 |
# File 'lib/siba-destination-ftp/worker.rb', line 12 def host @host end |
#passive ⇒ Object
Returns the value of attribute passive.
12 13 14 |
# File 'lib/siba-destination-ftp/worker.rb', line 12 def passive @passive end |
#password ⇒ Object
Returns the value of attribute password.
12 13 14 |
# File 'lib/siba-destination-ftp/worker.rb', line 12 def password @password end |
#user ⇒ Object
Returns the value of attribute user.
12 13 14 |
# File 'lib/siba-destination-ftp/worker.rb', line 12 def user @user end |
Instance Method Details
#cd(ftp) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 |
# 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 ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# 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) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# 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) ⇒ Object
82 83 84 85 86 |
# 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) ⇒ Object
72 73 74 75 76 |
# 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) ⇒ Object
62 63 64 65 66 |
# 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) ⇒ Object
88 89 90 |
# 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) ⇒ Object
78 79 80 |
# 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) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# 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) ⇒ Object
68 69 70 |
# 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) ⇒ Object
105 106 107 108 109 110 111 112 113 114 |
# 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 ⇒ Object
40 41 42 43 44 |
# 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 |