Class: FileTransfer::Ftp

Inherits:
Generic show all
Defined in:
lib/file_transfer/ftp.rb

Instance Attribute Summary collapse

Attributes inherited from Generic

#host, #keys, #password, #port, #timeout_seconds, #username

Instance Method Summary collapse

Methods inherited from Generic

#to_s

Constructor Details

#initialize(options = {}) ⇒ Ftp

Returns a new instance of Ftp.



7
8
9
# File 'lib/file_transfer/ftp.rb', line 7

def initialize(options = {})
  super(options)
end

Instance Attribute Details

#ftpObject (readonly)

Returns the value of attribute ftp.



5
6
7
# File 'lib/file_transfer/ftp.rb', line 5

def ftp
  @ftp
end

Instance Method Details

#closeObject

Closes FTP connection



83
84
85
86
87
88
89
# File 'lib/file_transfer/ftp.rb', line 83

def close
  if ftp && !ftp.closed?
    timeout(30) do
      ftp.close
    end
  end
end

#download(from_path, to_path) ⇒ String

Downloads data from FTP server

Parameters:

  • from_path (String)

    remote path

  • to_path (String)

    locale path

Returns:

  • (String)

    remote path of downloaded file



46
47
48
49
50
51
52
53
# File 'lib/file_transfer/ftp.rb', line 46

def download(from_path, to_path)
  from_path = split_path(from_path)
  open_connection do
    ftp.chdir from_path[:file_path]
    ftp.getbinaryfile(to_path, from_path[:file_name])
    "#{from_path[:file_path]}/#{from_path[:file_name]}"
  end
end

#exist?(file_path) ⇒ Boolean

Checks if file exists on FTP server

Parameters:

  • file_path (String)

    path to check

Returns:

  • (Boolean)

    true if file exists, otherwise false



71
72
73
74
75
76
# File 'lib/file_transfer/ftp.rb', line 71

def exist?(file_path)
  open_connection(60) do
    result = ftp.list "#{file_path}"
    result && result.size > 0
  end
end

#list(dir, options = {}) ⇒ Array<String>

Gets directory listing from SFTP server

Parameters:

  • dir (String)

    remote directory

  • options (Hash<Symbol,String>) (defaults to: {})

    additional options

Returns:

  • (Array<String>)

    listing with all file paths



20
21
22
23
24
25
26
27
28
29
# File 'lib/file_transfer/ftp.rb', line 20

def list(dir, options = {})
  open_connection(60) do
    ftp.chdir dir
    result = ftp.nlst
    if options.has_key? :file_type
      result = result.select { |file_name| file_name.end_with? options[:file_type] }
    end
    result
  end
end

#move(from_path, to_path) ⇒ Object Also known as: rename

Moves file from source to destination on FTP server

Parameters:

  • from_path (String)

    remote source path

  • to_path (String)

    remote destination path



58
59
60
61
62
63
64
65
# File 'lib/file_transfer/ftp.rb', line 58

def move(from_path, to_path)
  from_path = split_path(from_path)
  to_path = split_path(to_path)
  open_connection do
    ftp.chdir from_path[:file_path]
    ftp.rename from_path[:file_name], "#{to_path[:file_path]}/#{to_path[:file_name]}" if exist?("#{from_path[:file_name]}/#{from_path[:file_path]}")
  end
end

#open_connection(conn_timeout = timeout_seconds) { ... } ⇒ Object

Opens new connection, if not already open

Parameters:

  • conn_timeout (Integer) (defaults to: timeout_seconds)

    IDLE timeout in seconds

Yields:

  • executes operation within open connection on server



94
95
96
97
98
99
# File 'lib/file_transfer/ftp.rb', line 94

def open_connection(conn_timeout = timeout_seconds)
  connect if closed?
  timeout(conn_timeout) do
    yield if block_given?
  end
end

#remove(file_path) ⇒ Object



78
79
80
# File 'lib/file_transfer/ftp.rb', line 78

def remove(file_path)
  raise 'not yet implemented'
end

#test_connectionObject

Connects to server



12
13
14
# File 'lib/file_transfer/ftp.rb', line 12

def test_connection
  open_connection
end

#upload(from_path, to_path) ⇒ Object

Uploads file to FTP server

Parameters:

  • from_path (String)

    locale directory

  • to_path (String)

    remote directory



34
35
36
37
38
39
40
# File 'lib/file_transfer/ftp.rb', line 34

def upload(from_path, to_path)
  to_path = split_path(to_path)
  open_connection do
    ftp.chdir to_path[:file_path]
    ftp.putbinaryfile(from_path)
  end
end