Class: FTPUtils::FTPConnection

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

Class Method Summary collapse

Class Method Details

.clear_connection_cacheObject



29
30
31
# File 'lib/ftputils/ftpconnection.rb', line 29

def self.clear_connection_cache
  self.connections = Hash.new
end

.connect(uri) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ftputils/ftpconnection.rb', line 5

def self.connect(uri)
  if uri.match(/^ftp:\/\/(.*?):(.*?)@(.*?)(\/.*)*$/)
    username = $1
    password = $2
    host = $3

    connection = self.establish_connection(host, username, password)

    # need to reset to the top directory since connections are cached and
    # could have been left elsewhere. this also provides a way to see if the 
    # connection has expired and needs to be reloaded
    begin
      connection.chdir "/"
    rescue Net::FTPTempError
      connection = self.establish_connection(host, username, password, true)
      retry
    end

    return connection
  else
    raise "Invalid FTP URL provided: #{uri}"
  end
end