Class: FTPUtils::FTPConnection

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

Class Method Summary collapse

Class Method Details

.clear_connection_cacheObject



35
36
37
# File 'lib/ftputils/ftpconnection.rb', line 35

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

.connect(uri) ⇒ Object



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

def self.connect(uri)
  timeout(FTPUtils.timeout_period) do
    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
rescue Timeout::Error
  raise "Connecting to #{uri} timed out after #{FTPUtils.timeout_period} seconds"
end