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



78
79
80
81
82
83
84
# File 'lib/file_transfer/ftp.rb', line 78

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



41
42
43
44
45
46
47
48
# File 'lib/file_transfer/ftp.rb', line 41

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



66
67
68
69
70
71
# File 'lib/file_transfer/ftp.rb', line 66

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



15
16
17
18
19
20
21
22
23
24
# File 'lib/file_transfer/ftp.rb', line 15

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



53
54
55
56
57
58
59
60
# File 'lib/file_transfer/ftp.rb', line 53

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



89
90
91
92
93
94
# File 'lib/file_transfer/ftp.rb', line 89

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

#remove(file_path) ⇒ Object



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

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

#upload(from_path, to_path) ⇒ Object

Uploads file to FTP server

Parameters:

  • from_path (String)

    locale directory

  • to_path (String)

    remote directory



29
30
31
32
33
34
35
# File 'lib/file_transfer/ftp.rb', line 29

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