class Pione::Location::FTPLocation

FTPLocation represents locations on FTP server.

Public Class Methods

new(uri) click to toggle source
Calls superclass method Pione::Location::DataLocation::new
# File lib/pione/location/ftp-location.rb, line 14
def initialize(uri)
  uri = uri.to_ftp_scheme if uri.scheme == "myftp"
  super(uri)
end

Public Instance Methods

append(data) click to toggle source
# File lib/pione/location/ftp-location.rb, line 42
def append(data)
  if exist?
    update(read + data)
  else
    create(data)
  end
  return self
end
copy(dest) click to toggle source
# File lib/pione/location/ftp-location.rb, line 133
def copy(dest)
  dest.create(read)
end
create(data) click to toggle source
# File lib/pione/location/ftp-location.rb, line 28
def create(data)
  if exist?
    raise ExistAlready.new(self)
  else
    connect do |ftp|
      makedirs(ftp, @path.dirname)
      path = Temppath.create
      Location[path].create(data)
      ftp.put(path, @path.to_s)
    end
  end
  return self
end
delete() click to toggle source
# File lib/pione/location/ftp-location.rb, line 71
def delete
  connect {|ftp| ftp.delete(@path.to_s)} if exist?
end
directory?() click to toggle source
# File lib/pione/location/ftp-location.rb, line 122
def directory?
  connect do |ftp|
    begin
      ftp.chdir(@path.to_s)
      return true
    rescue
      return false
    end
  end
end
entries(option={}) click to toggle source
# File lib/pione/location/ftp-location.rb, line 91
def entries(option={})
  rel_entries(option).map {|entry| rebuild(@path + entry)}
end
exist?() click to toggle source
# File lib/pione/location/ftp-location.rb, line 110
def exist?
  file? or directory?
end
file?() click to toggle source
# File lib/pione/location/ftp-location.rb, line 114
def file?
  begin
    connect {|ftp| ftp.size(@path.to_s) > -1}
  rescue
    false
  end
end
inspect() click to toggle source
# File lib/pione/location/ftp-location.rb, line 154
def inspect
  scheme = @uri.scheme
  auth = "%s:%s@" % [@uri.user, @uri.password] if @uri.user and @uri.password
  host = @uri.host
  port = ":%i" % @uri.port
  path = @path.expand_path("/").to_s
  "#<%s %s://%s%s%s%s>" % [self.class, scheme, auth, host, port, path]
end
mkdir() click to toggle source
# File lib/pione/location/ftp-location.rb, line 75
def mkdir
  connect {|ftp| makedirs(ftp, @path)} unless exist?
end
move(dest) click to toggle source
# File lib/pione/location/ftp-location.rb, line 141
def move(dest)
  if dest.scheme == scheme and dest.host == host
    connect{|ftp| ftp.rename(@path.to_s, dest.path.to_s)}
  else
    copy(dest)
    delete
  end
end
mtime() click to toggle source
# File lib/pione/location/ftp-location.rb, line 79
def mtime
  connect {|ftp| exist? ? ftp.mtime(@path.to_s) : (raise NotFound.new(self))}
end
mtime=(time) click to toggle source
# File lib/pione/location/ftp-location.rb, line 83
def mtime=(time)

end
read() click to toggle source
# File lib/pione/location/ftp-location.rb, line 51
def read
  file = Temppath.create
  connect {|ftp| ftp.get(@path, file.to_s)}
  return File.read(file.to_s)
rescue Net::FTPPermError
  raise NotFound.new(@uri)
end
rebuild(path) click to toggle source
# File lib/pione/location/ftp-location.rb, line 19
def rebuild(path)
  scheme = @uri.scheme
  auth = "%s:%s@" % [@uri.user, @uri.password] if @uri.user and @uri.password
  host = @uri.host
  port = ":%i" % @uri.port
  path = Pathname.new(path).expand_path("/").to_s
  Location["%s://%s%s%s%s" % [scheme, auth, host, port, path]]
end
rel_entries(option={}) click to toggle source
# File lib/pione/location/ftp-location.rb, line 95
def rel_entries(option={})
  list = []
  connect do |ftp|
    ftp.nlst(@path.to_s).each do |entry|
      list << entry
      entry_location = rebuild(@path + entry)
      if option[:rec] and entry_location.directory?
        _list = entry_location.rel_entries(option).map {|subentry| entry + subentry}
        list = list + _list
      end
    end
  end
  return list
end
size() click to toggle source
# File lib/pione/location/ftp-location.rb, line 87
def size
  connect {|ftp| exist? ? ftp.size(@path.to_s) : (raise NotFound.new(self))}
end
turn(dest) click to toggle source
# File lib/pione/location/ftp-location.rb, line 150
def turn(dest)
  copy(dest)
end
update(data) click to toggle source
# File lib/pione/location/ftp-location.rb, line 59
def update(data)
  connect do |ftp|
    begin
      ftp.dir(@path.dirname.to_s)
      src = Temppath.create.tap{|x| x.open("w") {|f| f.write(data)}}.to_s
      ftp.put(src, @path.to_s)
    rescue Net::FTPPermError
      raise NotFound.new(@uri)
    end
  end
end

Private Instance Methods

connect() { |ftp| ... } click to toggle source

Connect to FTP server with the block.

# File lib/pione/location/ftp-location.rb, line 166
def connect(&b)
  3.times do
    begin
      ftp = Net::FTP.new
      ftp.connect(@uri.host, @uri.port)
      ftp.passive = true
      ftp.login(@uri.user, @uri.password) if @uri.user
      return yield ftp
    rescue Errno::ECONNREFUSED
      sleep 1
    end
  end
  raise
end
makedirs(ftp, path, size=0) click to toggle source

@api private

# File lib/pione/location/ftp-location.rb, line 182
def makedirs(ftp, path, size=0)
  path.descend do |dpath|
    ftp.mkdir(dpath.to_s) unless rebuild(dpath).exist?
  end
end