Class: FtpD8a
Overview
D8a based on an ftp site.
Instance Attribute Summary collapse
-
#ls_parser ⇒ Object
readonly
Proc(line, state_array) -> {} to process ls -R lines from server.
Attributes inherited from D8a
Class Method Summary collapse
-
.open(host, dir = nil, user = nil, passwd = nil, &block) ⇒ Object
set up a new Net::FTP connection.
Instance Method Summary collapse
- #delete(d8m) ⇒ Object
- #each ⇒ Object
- #flush ⇒ Object
-
#ftpmtime(d8m, d8mattrs = nil) ⇒ Object
ftpmtime attr.
-
#initialize(name, ftp = nil, &block) ⇒ FtpD8a
constructor
initializer.
-
#mode(d8m, d8mattrs = nil) ⇒ Object
mode attr.
- #read(d8m) ⇒ Object
-
#size(d8m, d8mattrs = nil) ⇒ Object
size attr.
- #write(d8m) ⇒ Object
Methods inherited from D8a
#[], #[]=, #copy, #diff, #diff_d8m, #diffreport, #name, #sync
Constructor Details
#initialize(name, ftp = nil, &block) ⇒ FtpD8a
initializer
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/d8a/ftpd8a.rb', line 23 def initialize(name, ftp = nil, &block) super(name) @ftp_gen = block @ftp = ftp ensure_ftp @attrs.push(:size, :ftpmtime, :mode) @id_attrs.push(:size, :ftpmtime) @ls_parser = Proc.new { |line,state| case line when /^$/ nil when /^([-d])([-r][-w][-x][-r][-w][-x][-r][-w][-x])\s+\d+\s+\d+\s+\d+\s+(\d+)\s+(.{12})\s+(.*)$/ next nil if $1 == 'd' name = state[0] + $5 size = $3.to_i mode = eval("0b#{$2.tr('-rwx', '01')}") ftpmtime = case $4 when /^((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d{1,2}\s+\d{4})$/ Date.strptime($2, '%b %e %Y') when /^(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+(\d{1,2})\s+(\d{1,2}:\d{2})$/ # TODO - this seems kind of clunky... now = DateTime.now Date.strptime("#{$1} #{$2} #{now.year}", '%b %e %Y') > now ? Date.strptime("#{$1} #{$2} #{now.year-1} #{$3}", '%b %e %Y %R') : Date.strptime("#{$1} #{$2} #{now.year} #{$3}", '%b %e %Y %R') end name.sub!(%r{^./}, '') { :name => name, :size => size, :mode => mode, :ftpmtime => ftpmtime } when /^(.*):$/ state[0] = $1 + "/" end } end |
Instance Attribute Details
#ls_parser ⇒ Object (readonly)
Proc(line, state_array) -> {} to process ls -R lines from server
8 9 10 |
# File 'lib/d8a/ftpd8a.rb', line 8 def ls_parser @ls_parser end |
Class Method Details
.open(host, dir = nil, user = nil, passwd = nil, &block) ⇒ Object
set up a new Net::FTP connection
12 13 14 15 16 17 18 19 |
# File 'lib/d8a/ftpd8a.rb', line 12 def FtpD8a.open(host, dir = nil, user = nil, passwd = nil, &block) FtpD8a.new("ftp://#{user || ''}#{user ? '@' : ''}#{host}/#{dir || ''}") do |ftp| ftp.close unless !ftp || ftp.closed? ftp = Net::FTP.new(host, user, passwd) ftp.chdir(dir) if dir ftp end end |
Instance Method Details
#delete(d8m) ⇒ Object
124 125 |
# File 'lib/d8a/ftpd8a.rb', line 124 def delete(d8m) end |
#each ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/d8a/ftpd8a.rb', line 103 def each ensure_ftp dir = [""] @ftpcache = {} @ftp.list("-R") do |line| if (x = @ls_parser.call(line, dir)).is_a?(Hash) @ftpcache[x[:name]] = x yield x[:name] end end end |
#flush ⇒ Object
128 129 130 131 |
# File 'lib/d8a/ftpd8a.rb', line 128 def flush #@ftp.close unless !@ftp || @ftp.closed? #@ftp = nil end |
#ftpmtime(d8m, d8mattrs = nil) ⇒ Object
ftpmtime attr
92 93 94 |
# File 'lib/d8a/ftpd8a.rb', line 92 def ftpmtime(d8m, d8mattrs = nil) @ftpcache[d8m] && @ftpcache[d8m][:ftpmtime] end |
#mode(d8m, d8mattrs = nil) ⇒ Object
mode attr
98 99 100 |
# File 'lib/d8a/ftpd8a.rb', line 98 def mode(d8m, d8mattrs = nil) @ftpcache[d8m] && @ftpcache[d8m][:mode] end |
#read(d8m) ⇒ Object
116 117 |
# File 'lib/d8a/ftpd8a.rb', line 116 def read(d8m) end |
#size(d8m, d8mattrs = nil) ⇒ Object
size attr
86 87 88 |
# File 'lib/d8a/ftpd8a.rb', line 86 def size(d8m, d8mattrs = nil) @ftpcache[d8m] && @ftpcache[d8m][:size] end |
#write(d8m) ⇒ Object
120 121 |
# File 'lib/d8a/ftpd8a.rb', line 120 def write(d8m) end |