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.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#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!(remote, local) ⇒ Object
fetch from remote to local.
-
#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, options = {}) ⇒ 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, options = {}) ⇒ 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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
19 20 21 |
# File 'lib/fun_sftp.rb', line 19 def 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
65 66 67 |
# File 'lib/fun_sftp.rb', line 65 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
114 115 116 117 118 119 120 121 122 |
# File 'lib/fun_sftp.rb', line 114 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!(remote, local) ⇒ Object
fetch from remote to local
46 47 48 49 50 51 52 |
# File 'lib/fun_sftp.rb', line 46 def download!(remote, local) #fetch from remote to local opts = { progress: DownloadCallbacks.new, recursive: true} converted_remote_path = clean_path(remote) opts.delete(:progress) unless FunSftp.loggable? opts.delete(:recursive) unless has_directory?(remote) client.download!(converted_remote_path, local, opts) end |
#entries(dir, show_dot_files = false) ⇒ Object
array of directory entries not caring for ‘.’ files
78 79 80 81 82 |
# File 'lib/fun_sftp.rb', line 78 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’)
73 74 75 |
# File 'lib/fun_sftp.rb', line 73 def glob(path, pattern='**/*') # ex: ('some_directory', '**/*.rb') client.dir.glob(clean_path(path), pattern).collect(&:name) end |
#has_directory?(dir) ⇒ Boolean
84 85 86 87 88 89 90 |
# File 'lib/fun_sftp.rb', line 84 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
97 98 99 |
# File 'lib/fun_sftp.rb', line 97 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
69 70 71 |
# File 'lib/fun_sftp.rb', line 69 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
92 93 94 |
# File 'lib/fun_sftp.rb', line 92 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
54 55 56 57 58 59 |
# File 'lib/fun_sftp.rb', line 54 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
109 110 111 112 |
# File 'lib/fun_sftp.rb', line 109 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
124 125 126 127 |
# File 'lib/fun_sftp.rb', line 124 def reset_path! self.source = '.' "Path Reset!" end |
#rm(path) ⇒ Object
remove a file
101 102 103 |
# File 'lib/fun_sftp.rb', line 101 def rm(path) #remove a file client.remove!(clean_path(path)) end |
#rmdir!(path) ⇒ Object
remove directory
105 106 107 |
# File 'lib/fun_sftp.rb', line 105 def rmdir!(path) #remove directory client.rmdir!(clean_path(path)) end |
#setup_login ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/fun_sftp.rb', line 29 def setup_login unless .none? Net::SFTP.start(server, user, ) else Net::SFTP.start(server, user, password: password) end end |
#size(path) ⇒ Object
returns the size of a file. ex: => 1413455562
61 62 63 |
# File 'lib/fun_sftp.rb', line 61 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
37 38 39 40 41 42 43 44 |
# File 'lib/fun_sftp.rb', line 37 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 |