Class: FtpSync::Simple
- Inherits:
-
Object
- Object
- FtpSync::Simple
- Defined in:
- lib/ftpsync.rb
Instance Attribute Summary collapse
-
#passive ⇒ Object
Returns the value of attribute passive.
-
#password ⇒ Object
Returns the value of attribute password.
-
#port ⇒ Object
Returns the value of attribute port.
-
#server ⇒ Object
Returns the value of attribute server.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #close! ⇒ Object
- #connect! ⇒ Object
-
#initialize(server, username, password, options = {}) ⇒ Simple
constructor
A new instance of Simple.
- #log(message) ⇒ Object
- #pull_dir(remotepath, localpath, options = {}, &block) ⇒ Object
Constructor Details
#initialize(server, username, password, options = {}) ⇒ Simple
Returns a new instance of Simple.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ftpsync.rb', line 10 def initialize(server, username, password, = {}) @server = server @port = [:port] || 21 @username = username @password = password @passive = [:passive] || false @verbose = [:verbose] || false @connection = nil @level = 0 end |
Instance Attribute Details
#passive ⇒ Object
Returns the value of attribute passive.
8 9 10 |
# File 'lib/ftpsync.rb', line 8 def passive @passive end |
#password ⇒ Object
Returns the value of attribute password.
8 9 10 |
# File 'lib/ftpsync.rb', line 8 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
8 9 10 |
# File 'lib/ftpsync.rb', line 8 def port @port end |
#server ⇒ Object
Returns the value of attribute server.
8 9 10 |
# File 'lib/ftpsync.rb', line 8 def server @server end |
#username ⇒ Object
Returns the value of attribute username.
8 9 10 |
# File 'lib/ftpsync.rb', line 8 def username @username end |
Instance Method Details
#close! ⇒ Object
86 87 88 89 |
# File 'lib/ftpsync.rb', line 86 def close! @connection.close log "Closed connection to #{@server}:#{@port}" end |
#connect! ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/ftpsync.rb', line 76 def connect! @connection = Net::FTP.new log "Connecting to #{@server}:#{@port} in #{@passive ? "passive" : "active"} mode" @connection.connect(@server, @port) @connection.passive = @passive log "Logging in as #{@username}:#{@password}" @connection.login(@username, @password) log "Successfully opened connection to #{@server}:#{@port}" end |
#log(message) ⇒ Object
91 92 93 |
# File 'lib/ftpsync.rb', line 91 def log() $stderr.puts if @verbose end |
#pull_dir(remotepath, localpath, options = {}, &block) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ftpsync.rb', line 22 def pull_dir(remotepath, localpath, = {}, &block) FileUtils.mkpath(localpath) unless File.exist?(localpath) and log "Creating #{localpath} directory" connect! unless @connection @level += 1 todelete = Dir.glob(File.join(localpath, '*')) directories = [] files = [] @connection.list(remotepath) do |e| entry = Net::FTP::List.parse(e) paths = [ "#{remotepath}/#{entry.basename}".gsub(/\/+/, '/'), File.join(localpath, entry.basename)] if entry.dir? if entry.name == "." or entry.name == ".." next else directories << paths end elsif entry.file? if [:since] files << paths unless File.exist?(paths[1]) and entry.mtime < File.mtime(paths[1]) and entry.filesize == File.size(paths[1]) else files << paths end end todelete.delete paths[1] end directories.each do |paths| remotedir, localdir = paths pull_dir(remotedir, localdir, , &block) end files.each do |paths| remotefile, localfile = paths begin log "#{remotefile} => #{localfile}" @connection.get(remotefile, localfile) if block_given? log "Executing block" yield(localfile) end rescue Net::FTPPermError log "Error when reading #{remotefile}" raise Net::FTPPermError unless [:skip_errors] end end @level -= 1 close! if @level == 0 end |