Class: Dandelion::Backend::FTP

Inherits:
Base
  • Object
show all
Defined in:
lib/dandelion/backend/ftp.rb

Instance Method Summary collapse

Methods inherited from Base

create, gem_list, gems, scheme

Constructor Details

#initialize(config) ⇒ FTP

Returns a new instance of FTP.



8
9
10
11
12
13
14
15
16
# File 'lib/dandelion/backend/ftp.rb', line 8

def initialize(config)
  require 'net/ftp'
  @config = config
  @ftp = Net::FTP.new
  @ftp.connect(@config['host'], @config['port'] || Net::FTP::FTP_PORT)
  @ftp.(@config['username'], @config['password'])
  @ftp.passive = @config['passive'].nil? ? true : to_b(@config['passive'])
  @ftp.chdir(@config['path']) if @config['path']
end

Instance Method Details

#delete(file) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/dandelion/backend/ftp.rb', line 43

def delete(file)
  begin
    @ftp.delete(file)
    cleanup(File.dirname(file))
  rescue Net::FTPPermError => e
  end
end

#read(file) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/dandelion/backend/ftp.rb', line 18

def read(file)
  begin
    # Implementation of FTP#getbinaryfile differs between 1.8
    # and 1.9 so we call FTP#retrbinary directly
    content = ''
    @ftp.retrbinary("RETR #{file}", 4096) do |data|
      content += data
    end
    content
  rescue Net::FTPPermError => e
    raise MissingFileError
  end
end

#to_sObject



51
52
53
# File 'lib/dandelion/backend/ftp.rb', line 51

def to_s
  "ftp://#{@config['username']}@#{@config['host']}/#{@config['path']}"
end

#write(file, data) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/dandelion/backend/ftp.rb', line 32

def write(file, data)
  temp(file, data) do |temp|
    begin
      @ftp.putbinaryfile(temp, file)
    rescue Net::FTPPermError => e
      mkdir_p(File.dirname(file))
      @ftp.putbinaryfile(temp, file)
    end
  end
end