Class: FileTransfer::Ftp
Instance Attribute Summary collapse
-
#ftp ⇒ Object
readonly
Returns the value of attribute ftp.
Attributes inherited from Generic
#host, #keys, #password, #port, #timeout_seconds, #username
Instance Method Summary collapse
-
#close ⇒ Object
Closes FTP connection.
-
#download(from_path, to_path) ⇒ String
Downloads data from FTP server.
-
#exist?(file_path) ⇒ Boolean
Checks if file exists on FTP server.
-
#initialize(options = {}) ⇒ Ftp
constructor
A new instance of Ftp.
-
#list(dir, options = {}) ⇒ Array<String>
Gets directory listing from SFTP server.
-
#move(from_path, to_path) ⇒ Object
(also: #rename)
Moves file from source to destination on FTP server.
-
#open_connection(conn_timeout = timeout_seconds) { ... } ⇒ Object
Opens new connection, if not already open.
- #remove(file_path) ⇒ Object
-
#test_connection ⇒ Object
Connects to server.
-
#upload(from_path, to_path) ⇒ Object
Uploads file to FTP server.
Methods inherited from Generic
Constructor Details
#initialize(options = {}) ⇒ Ftp
Returns a new instance of Ftp.
7 8 9 |
# File 'lib/file_transfer/ftp.rb', line 7 def initialize( = {}) super() end |
Instance Attribute Details
#ftp ⇒ Object (readonly)
Returns the value of attribute ftp.
5 6 7 |
# File 'lib/file_transfer/ftp.rb', line 5 def ftp @ftp end |
Instance Method Details
#close ⇒ Object
Closes FTP connection
83 84 85 86 87 88 89 |
# File 'lib/file_transfer/ftp.rb', line 83 def close if ftp && !ftp.closed? timeout(30) do ftp.close end end end |
#download(from_path, to_path) ⇒ String
Downloads data from FTP server
46 47 48 49 50 51 52 53 |
# File 'lib/file_transfer/ftp.rb', line 46 def download(from_path, to_path) from_path = split_path(from_path) open_connection do ftp.chdir from_path[:file_path] ftp.getbinaryfile(to_path, from_path[:file_name]) "#{from_path[:file_path]}/#{from_path[:file_name]}" end end |
#exist?(file_path) ⇒ Boolean
Checks if file exists on FTP server
71 72 73 74 75 76 |
# File 'lib/file_transfer/ftp.rb', line 71 def exist?(file_path) open_connection(60) do result = ftp.list "#{file_path}" result && result.size > 0 end end |
#list(dir, options = {}) ⇒ Array<String>
Gets directory listing from SFTP server
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/file_transfer/ftp.rb', line 20 def list(dir, = {}) open_connection(60) do ftp.chdir dir result = ftp.nlst if .has_key? :file_type result = result.select { |file_name| file_name.end_with? [:file_type] } end result end end |
#move(from_path, to_path) ⇒ Object Also known as: rename
Moves file from source to destination on FTP server
58 59 60 61 62 63 64 65 |
# File 'lib/file_transfer/ftp.rb', line 58 def move(from_path, to_path) from_path = split_path(from_path) to_path = split_path(to_path) open_connection do ftp.chdir from_path[:file_path] ftp.rename from_path[:file_name], "#{to_path[:file_path]}/#{to_path[:file_name]}" if exist?("#{from_path[:file_name]}/#{from_path[:file_path]}") end end |
#open_connection(conn_timeout = timeout_seconds) { ... } ⇒ Object
Opens new connection, if not already open
94 95 96 97 98 99 |
# File 'lib/file_transfer/ftp.rb', line 94 def open_connection(conn_timeout = timeout_seconds) connect if closed? timeout(conn_timeout) do yield if block_given? end end |
#remove(file_path) ⇒ Object
78 79 80 |
# File 'lib/file_transfer/ftp.rb', line 78 def remove(file_path) raise 'not yet implemented' end |
#test_connection ⇒ Object
Connects to server
12 13 14 |
# File 'lib/file_transfer/ftp.rb', line 12 def test_connection open_connection end |
#upload(from_path, to_path) ⇒ Object
Uploads file to FTP server
34 35 36 37 38 39 40 |
# File 'lib/file_transfer/ftp.rb', line 34 def upload(from_path, to_path) to_path = split_path(to_path) open_connection do ftp.chdir to_path[:file_path] ftp.putbinaryfile(from_path) end end |