class Dandelion::Adapter::FTP
Public Class Methods
new(config)
click to toggle source
# File lib/dandelion/adapter/ftp.rb, line 10 def initialize(config) require 'net/ftp' @config = config @config.defaults(port: Net::FTP::FTP_PORT) @config[:passive] = to_b(@config[:passive]) @ftp = ftp_client end
Public Instance Methods
delete(file)
click to toggle source
# File lib/dandelion/adapter/ftp.rb, line 40 def delete(file) begin @ftp.delete(path(file)) cleanup(File.dirname(path(file))) rescue Net::FTPPermError => e end end
read(file)
click to toggle source
# File lib/dandelion/adapter/ftp.rb, line 20 def read(file) begin @ftp.getbinaryfile(path(file), nil) rescue Net::FTPPermError => e nil end end
to_s()
click to toggle source
# File lib/dandelion/adapter/ftp.rb, line 48 def to_s "ftp://#{@config['username']}@#{@config['host']}/#{@config['path']}" end
write(file, data)
click to toggle source
# File lib/dandelion/adapter/ftp.rb, line 28 def write(file, data) temp(file, data) do |temp| begin @ftp.putbinaryfile(temp, path(file)) rescue Net::FTPPermError => e raise e unless e.to_s =~ /550|553/ mkdir_p(File.dirname(path(file))) @ftp.putbinaryfile(temp, path(file)) end end end
Private Instance Methods
cleanup(dir)
click to toggle source
# File lib/dandelion/adapter/ftp.rb, line 62 def cleanup(dir) unless dir == File.dirname(dir) if empty?(dir) @ftp.rmdir(dir) cleanup(File.dirname(dir)) end end end
empty?(dir)
click to toggle source
# File lib/dandelion/adapter/ftp.rb, line 71 def empty?(dir) return @ftp.nlst(dir).empty? end
ftp_client()
click to toggle source
# File lib/dandelion/adapter/ftp.rb, line 54 def ftp_client ftp = Net::FTP.new ftp.connect(@config['host'], @config['port']) ftp.login(@config['username'], @config['password']) ftp.passive = @config['passive'] ftp end
mkdir_p(dir)
click to toggle source
# File lib/dandelion/adapter/ftp.rb, line 75 def mkdir_p(dir) unless dir == File.dirname(dir) begin @ftp.mkdir(dir) rescue Net::FTPPermError => e mkdir_p(File.dirname(dir)) @ftp.mkdir(dir) end end end
path(file)
click to toggle source
# File lib/dandelion/adapter/ftp.rb, line 86 def path(file) if @config['path'] and !@config['path'].empty? File.join(@config['path'], file) else file end end
to_b(value)
click to toggle source
# File lib/dandelion/adapter/ftp.rb, line 94 def to_b(value) return [true, 'true', 1, '1', 'T', 't'].include?(value.class == String ? value.downcase : value) end