Class: FtpSync::Simple
- Inherits:
-
Object
- Object
- FtpSync::Simple
- Defined in:
- lib/ftpsync.rb
Overview
Synchronizes directories against an FTP server.
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
Recursively pulls
remotepathintolocalpath.
Constructor Details
#initialize(server, username, password, options = {}) ⇒ Simple
Returns a new instance of Simple.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ftpsync.rb', line 13 def initialize(server, username, password, = {}) @server = server @port = .fetch(:port, 21) @username = username @password = password @passive = .fetch(:passive, false) @verbose = .fetch(:verbose, false) @connection = nil @level = 0 end |
Instance Attribute Details
#passive ⇒ Object
Returns the value of attribute passive.
11 12 13 |
# File 'lib/ftpsync.rb', line 11 def passive @passive end |
#password ⇒ Object
Returns the value of attribute password.
11 12 13 |
# File 'lib/ftpsync.rb', line 11 def password @password end |
#port ⇒ Object
Returns the value of attribute port.
11 12 13 |
# File 'lib/ftpsync.rb', line 11 def port @port end |
#server ⇒ Object
Returns the value of attribute server.
11 12 13 |
# File 'lib/ftpsync.rb', line 11 def server @server end |
#username ⇒ Object
Returns the value of attribute username.
11 12 13 |
# File 'lib/ftpsync.rb', line 11 def username @username end |
Instance Method Details
#close! ⇒ Object
62 63 64 65 66 |
# File 'lib/ftpsync.rb', line 62 def close! @connection.close @connection = nil log "Closed connection to #{@server}:#{@port}" end |
#connect! ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/ftpsync.rb', line 52 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}" @connection.login(@username, @password) log "Successfully opened connection to #{@server}:#{@port}" end |
#log(message) ⇒ Object
68 69 70 |
# File 'lib/ftpsync.rb', line 68 def log() warn if @verbose end |
#pull_dir(remotepath, localpath, options = {}, &block) ⇒ Object
Recursively pulls remotepath into localpath.
Options:
:since skip files that already exist locally and are up to date
:skip_errors rescue Net::FTPPermError and keep going
If a block is given, it is invoked with the local path of each downloaded file.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ftpsync.rb', line 33 def pull_dir(remotepath, localpath, = {}, &block) ensure_local_directory(localpath) connect! unless @connection @level += 1 directories, files = scan_directory(remotepath, localpath, ) directories.each do |remote_dir, local_dir| pull_dir(remote_dir, local_dir, , &block) end files.each do |remote_file, local_file| download(remote_file, local_file, , &block) end @level -= 1 close! if @level.zero? end |