Browse files

Add a has_directory? and items_in method(s)

  • Loading branch information...
1 parent 00bda3e commit ad364dd8c1c8b1e5032e0f51976d1063f9f250fa @georgediaz88 committed Feb 24, 2012
Showing with 29 additions and 10 deletions.
  1. +14 −7 README.rdoc
  2. +14 −2 lib/fun_sftp.rb
  3. +1 −1 lib/fun_sftp/version.rb
View
21 README.rdoc
@@ -1,4 +1,4 @@
-= FunSFTP (Version 0.0.5)
+= FunSFTP (Version 0.0.6)
A Ruby Gem that provides a nice and easy to use wrapper for the Net::SFTP library.
@@ -46,17 +46,24 @@ Need to read a file on the remote host? Ah, it's no biggie...
When investigating items on the remote host, these commands can be handy:
- 1) conn.print_directory_items("directory_name")
+ 1) conn.has_directory?("directory_name")
+ # returns true or false if the directory exists
+
+ 2) conn.items_in("directory_name")
+ # returns an array of every directory and file within the provided directory
+ # => ["test1", "test1/h1.rb", "test2", "test2/h2.txt"]
+
+ 3) conn.print_directory_items("directory_name")
# outputs directories one line at a time
# => test1
test2
some_directory_here
-
- 2) conn.entries("directory_name")
+
+ 4) conn.entries("directory_name")
# outputs a nice array of entries in the specified directory
# => ["test1", "test2", "some_directory_here"]
-
- 3) conn.glob("directory_name", "**/*.rb")
+
+ 5) conn.glob("directory_name", "**/*.rb")
# Traverses the directory specified using the second argument/matcher
# So, you can get things like...
# => ["some_directory_here/hello_world.rb", "sftp_is_fun.rb"]
@@ -82,7 +89,7 @@ Files can be removed and renamed off the remote host:
2) conn.rename("old_file_name.txt", "new_file_name.txt")
# old_file_name.txt is now named new_file_name.txt on the remote host.
-Hopefully, this is basic enough to work with and transfer files!!
+Hopefully, this is easy enough to work with and transfer files!!
Look for new helpful method calls in future releases.
== License
View
16 lib/fun_sftp.rb
@@ -38,14 +38,26 @@ def read(path)
def glob(path, pattern) # ex: ('some_directory', '**/*.rb')
@client.dir.glob(path, pattern).collect(&:name)
end
-
+
def entries(dir) #array of directory items
- @client.dir.entries(dir).collect(&:name)
+ @client.dir.entries(dir).collect(&:name).reject!{|a| a.match(/^\.+$/)}
+ end
+
+ def has_directory?(dir) #returns true if directory exists
+ begin
+ true if entries(dir)
+ rescue Exception => e
+ false
+ end
end
def print_directory_items(dir) #printout of directory items
@client.dir.foreach(dir) { |file| print "#{file.name}\n" }
end
+
+ def items_in(root_dir) #array of *all* directories & files inside provided root directory
+ @client.glob(root_dir, '**/*').sort
+ end
#### Some Handy File Util Methods
def mkdir!(path) #make directory
View
2 lib/fun_sftp/version.rb
@@ -1,3 +1,3 @@
module FunSftp
- VERSION = "0.0.5"
+ VERSION = "0.0.6"
end

0 comments on commit ad364dd

Please sign in to comment.