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
-
#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
78 79 80 81 82 83 84 |
# File 'lib/file_transfer/ftp.rb', line 78 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
41 42 43 44 45 46 47 48 |
# File 'lib/file_transfer/ftp.rb', line 41 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
66 67 68 69 70 71 |
# File 'lib/file_transfer/ftp.rb', line 66 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
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/file_transfer/ftp.rb', line 15 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
53 54 55 56 57 58 59 60 |
# File 'lib/file_transfer/ftp.rb', line 53 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
89 90 91 92 93 94 |
# File 'lib/file_transfer/ftp.rb', line 89 def open_connection(conn_timeout = timeout_seconds) connect if closed? timeout(conn_timeout) do yield if block_given? end end |
#remove(file_path) ⇒ Object
73 74 75 |
# File 'lib/file_transfer/ftp.rb', line 73 def remove(file_path) raise 'not yet implemented' end |
#upload(from_path, to_path) ⇒ Object
Uploads file to FTP server
29 30 31 32 33 34 35 |
# File 'lib/file_transfer/ftp.rb', line 29 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 |