Class: FunSftp::SFTPClient
- Inherits:
-
Object
- Object
- FunSftp::SFTPClient
- Defined in:
- lib/fun_sftp.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
-
#source ⇒ Object
(also: #pwd)
Returns the value of attribute source.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#atime(path) ⇒ Object
returns the atime (access time) of a file.
- #chdir(path) ⇒ Object
-
#download!(target, src) ⇒ Object
fetch locally from remote.
-
#entries(dir, show_dot_files = false) ⇒ Object
array of directory entries not caring for ‘.’ files.
-
#glob(path, pattern = '**/*') ⇒ Object
(also: #items_in)
ex: (‘some_directory’, ‘*/.rb’).
- #has_directory?(dir) ⇒ Boolean
-
#initialize(server, user, password) ⇒ SFTPClient
constructor
A new instance of SFTPClient.
-
#mkdir!(path) ⇒ Object
make directory.
-
#mtime(path) ⇒ Object
returns the mtime (modified time) of a file.
-
#print_directory_items(dir = '.') ⇒ Object
(also: #ll)
printout of directory’s items.
-
#read(path) ⇒ Object
read a file.
-
#rename(name, new_name) ⇒ Object
rename a file.
- #reset_path! ⇒ Object
-
#rm(path) ⇒ Object
remove a file.
-
#rmdir!(path) ⇒ Object
remove directory.
- #setup_login ⇒ Object
-
#size(path) ⇒ Object
returns the size of a file.
-
#upload!(src, target) ⇒ Object
send to remote.
Constructor Details
#initialize(server, user, password) ⇒ SFTPClient
Returns a new instance of SFTPClient.
23 24 25 26 27 |
# File 'lib/fun_sftp.rb', line 23 def initialize(server, user, password) @server, @user, @password = server, user, password self.source = '.' @client = setup_login end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
19 20 21 |
# File 'lib/fun_sftp.rb', line 19 def client @client end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
19 20 21 |
# File 'lib/fun_sftp.rb', line 19 def password @password end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
19 20 21 |
# File 'lib/fun_sftp.rb', line 19 def server @server end |
#source ⇒ Object Also known as: pwd
Returns the value of attribute source.
17 18 19 |
# File 'lib/fun_sftp.rb', line 17 def source @source end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
19 20 21 |
# File 'lib/fun_sftp.rb', line 19 def user @user end |
Instance Method Details
#atime(path) ⇒ Object
returns the atime (access time) of a file. ex: => 2014-10-16 12:32:42 +0200
61 62 63 |
# File 'lib/fun_sftp.rb', line 61 def atime(path) #returns the atime (access time) of a file. ex: => 2014-10-16 12:32:42 +0200 Time.at(client.file.open(clean_path(path)).stat.atime) end |
#chdir(path) ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/fun_sftp.rb', line 110 def chdir(path) clean_path = clean_path(path) if has_directory? path self.source = clean_path "Current Path change to => #{source}" else "Sorry Path => #{path} not found" end end |
#download!(target, src) ⇒ Object
fetch locally from remote
42 43 44 45 46 47 48 |
# File 'lib/fun_sftp.rb', line 42 def download!(target, src) #fetch locally from remote opts = { progress: DownloadCallbacks.new, recursive: true} converted_target = clean_path(target) opts.delete(:progress) unless FunSftp.loggable? opts.delete(:recursive) unless has_directory?(target) client.download!(converted_target, src, opts) end |
#entries(dir, show_dot_files = false) ⇒ Object
array of directory entries not caring for ‘.’ files
74 75 76 77 78 |
# File 'lib/fun_sftp.rb', line 74 def entries(dir, show_dot_files = false) #array of directory entries not caring for '.' files entries_arr = client.dir.entries(clean_path(dir)).collect(&:name) entries_arr.reject!{|a| a.match(/^\..*$/)} unless show_dot_files entries_arr end |
#glob(path, pattern = '**/*') ⇒ Object Also known as: items_in
ex: (‘some_directory’, ‘*/.rb’)
69 70 71 |
# File 'lib/fun_sftp.rb', line 69 def glob(path, pattern='**/*') # ex: ('some_directory', '**/*.rb') client.dir.glob(clean_path(path), pattern).collect(&:name) end |
#has_directory?(dir) ⇒ Boolean
80 81 82 83 84 85 86 |
# File 'lib/fun_sftp.rb', line 80 def has_directory?(dir) begin true if client.dir.entries(clean_path(dir)).any? rescue Net::SFTP::StatusException => e false end end |
#mkdir!(path) ⇒ Object
make directory
93 94 95 |
# File 'lib/fun_sftp.rb', line 93 def mkdir!(path) #make directory client.mkdir!(clean_path(path)) end |
#mtime(path) ⇒ Object
returns the mtime (modified time) of a file. ex: => 2014-10-16 12:32:42 +0200
65 66 67 |
# File 'lib/fun_sftp.rb', line 65 def mtime(path) #returns the mtime (modified time) of a file. ex: => 2014-10-16 12:32:42 +0200 Time.at(client.file.open(clean_path(path)).stat.mtime) end |
#print_directory_items(dir = '.') ⇒ Object Also known as: ll
printout of directory’s items
88 89 90 |
# File 'lib/fun_sftp.rb', line 88 def print_directory_items(dir='.') #printout of directory's items client.dir.foreach(clean_path(dir)) { |file| puts "#{file.name}" } end |
#read(path) ⇒ Object
read a file
50 51 52 53 54 55 |
# File 'lib/fun_sftp.rb', line 50 def read(path) #read a file file = client.file.open(clean_path(path)) while !file.eof? puts file.gets end end |
#rename(name, new_name) ⇒ Object
rename a file
105 106 107 108 |
# File 'lib/fun_sftp.rb', line 105 def rename(name, new_name) #rename a file previous, renamed = clean_path(name), clean_path(new_name) client.rename!(previous, renamed) end |
#reset_path! ⇒ Object
120 121 122 123 |
# File 'lib/fun_sftp.rb', line 120 def reset_path! self.source = '.' "Path Reset!" end |
#rm(path) ⇒ Object
remove a file
97 98 99 |
# File 'lib/fun_sftp.rb', line 97 def rm(path) #remove a file client.remove!(clean_path(path)) end |
#rmdir!(path) ⇒ Object
remove directory
101 102 103 |
# File 'lib/fun_sftp.rb', line 101 def rmdir!(path) #remove directory client.rmdir!(clean_path(path)) end |
#setup_login ⇒ Object
29 30 31 |
# File 'lib/fun_sftp.rb', line 29 def setup_login Net::SFTP.start(server, user, password: password) end |
#size(path) ⇒ Object
returns the size of a file. ex: => 1413455562
57 58 59 |
# File 'lib/fun_sftp.rb', line 57 def size(path) #returns the size of a file. ex: => 1413455562 client.file.open(clean_path(path)).stat.size end |
#upload!(src, target) ⇒ Object
send to remote
33 34 35 36 37 38 39 40 |
# File 'lib/fun_sftp.rb', line 33 def upload!(src, target) #send to remote #target example: 'some_directory/some_name.txt' opts = { progress: UploadCallbacks.new, recursive: true } converted_target = clean_path(target) opts.delete(:progress) unless FunSftp.loggable? opts.delete(:recursive) unless has_directory?(target) client.upload!(src, converted_target, opts) end |