1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
require 'net/ftp'

class SimpleFtp

  def self.pull(config, from_remote, to_local)
    open(config) {|ftp| ftp.gettextfile(from_remote, to_local)}
  end

  def self.push(config, from_local, to_remote)
    open(config) {|ftp| ftp.puttextfile(from_local, to_remote)}
  end

  private

  # This will open up an FTP connection, yield to the block, and then close the connection
  def self.open(config)
    Net::FTP.open(config[:url],config[:username], config[:password]) do |ftp|
      ftp.chdir(config[:folder]) unless config[:folder].blank?
      yield(ftp)
    end
  end
end