Class: FtpSync::Simple

Inherits:
Object
  • Object
show all
Defined in:
lib/ftpsync.rb

Overview

Synchronizes directories against an FTP server.

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  @server = server
  @port = options.fetch(:port, 21)
  @username = username
  @password = password
  @passive = options.fetch(:passive, false)
  @verbose = options.fetch(:verbose, false)

  @connection = nil
  @level = 0
end

Instance Attribute Details

#passiveObject

Returns the value of attribute passive.



11
12
13
# File 'lib/ftpsync.rb', line 11

def passive
  @passive
end

#passwordObject

Returns the value of attribute password.



11
12
13
# File 'lib/ftpsync.rb', line 11

def password
  @password
end

#portObject

Returns the value of attribute port.



11
12
13
# File 'lib/ftpsync.rb', line 11

def port
  @port
end

#serverObject

Returns the value of attribute server.



11
12
13
# File 'lib/ftpsync.rb', line 11

def server
  @server
end

#usernameObject

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.(@username, @password)
  log "Successfully opened connection to #{@server}:#{@port}"
end

#log(message) ⇒ Object



68
69
70
# File 'lib/ftpsync.rb', line 68

def log(message)
  warn message 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, options = {}, &block)
  ensure_local_directory(localpath)
  connect! unless @connection
  @level += 1

  directories, files = scan_directory(remotepath, localpath, options)

  directories.each do |remote_dir, local_dir|
    pull_dir(remote_dir, local_dir, options, &block)
  end

  files.each do |remote_file, local_file|
    download(remote_file, local_file, options, &block)
  end

  @level -= 1
  close! if @level.zero?
end