Class: Pione::Location::FTPLocation

Inherits:
DataLocation show all
Defined in:
lib/pione/location/ftp-location.rb

Overview

FTPLocation represents locations on FTP server.

Constant Summary

Constants inherited from DataLocation

DataLocation::KNOWN_ATTRS

Instance Attribute Summary

Attributes inherited from DataLocation

#path, #uri

Attributes inherited from BasicLocation

#address

Instance Method Summary collapse

Methods inherited from DataLocation

#+, #==, #as_directory, #basename, #cached?, #ctime, define, #directory_entries, #dirname, #extname, #file_entries, #hash, #local, #local?, need_caching?, real_appendable?, set_scheme, #sha1, writable?, #write

Methods inherited from BasicLocation

#==, #hash, location_type

Constructor Details

#initialize(uri) ⇒ FTPLocation

Returns a new instance of FTPLocation.



14
15
16
17
# File 'lib/pione/location/ftp-location.rb', line 14

def initialize(uri)
  uri = uri.to_ftp_scheme if uri.scheme == "myftp"
  super(uri)
end

Instance Method Details

#append(data) ⇒ Object



42
43
44
45
46
47
48
49
# 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) ⇒ Object



133
134
135
# File 'lib/pione/location/ftp-location.rb', line 133

def copy(dest)
  dest.create(read)
end

#create(data) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

#deleteObject



71
72
73
# File 'lib/pione/location/ftp-location.rb', line 71

def delete
  connect {|ftp| ftp.delete(@path.to_s)} if exist?
end

#directory?Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
131
# 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 = {}) ⇒ Object



91
92
93
# File 'lib/pione/location/ftp-location.rb', line 91

def entries(option={})
  rel_entries(option).map {|entry| rebuild(@path + entry)}
end

#exist?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/pione/location/ftp-location.rb', line 110

def exist?
  file? or directory?
end

#file?Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
# File 'lib/pione/location/ftp-location.rb', line 114

def file?
  begin
    connect {|ftp| ftp.size(@path.to_s) > -1}
  rescue
    false
  end
end

#inspectObject



154
155
156
157
158
159
160
161
# 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


137
138
139
# File 'lib/pione/location/ftp-location.rb', line 137

def link(orig)
  orig.copy(self)
end

#mkdirObject



75
76
77
# File 'lib/pione/location/ftp-location.rb', line 75

def mkdir
  connect {|ftp| makedirs(ftp, @path)} unless exist?
end

#move(dest) ⇒ Object



141
142
143
144
145
146
147
148
# 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

#mtimeObject



79
80
81
# 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) ⇒ Object



83
84
85
# File 'lib/pione/location/ftp-location.rb', line 83

def mtime=(time)

end

#readObject



51
52
53
54
55
56
57
# 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) ⇒ Object



19
20
21
22
23
24
25
26
# 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 = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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

#sizeObject



87
88
89
# 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) ⇒ Object



150
151
152
# File 'lib/pione/location/ftp-location.rb', line 150

def turn(dest)
  copy(dest)
end

#update(data) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# 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