rftp 1.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +22 -0
  2. data/README +0 -0
  3. data/lib/rftp.rb +165 -0
  4. metadata +57 -0
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2008 Brian Collins
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ copies of the Software, and to permit persons to whom the
9
+ Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
File without changes
@@ -0,0 +1,165 @@
1
+ require 'net/ftp'
2
+ require 'date'
3
+
4
+ class Hash
5
+ def rftp_symbolize_keys
6
+ replace(inject({}) { |h,(k,v)| h[k.to_sym] = v; h })
7
+ end
8
+ end
9
+
10
+ class RftpFile
11
+ attr_accessor :parent
12
+ attr_accessor :name, :path, :permissions, :owner, :group, :size, :last_modified
13
+
14
+ def file?
15
+ true
16
+ end
17
+
18
+ def dir?
19
+ false
20
+ end
21
+
22
+ def initialize(parts, parent)
23
+ self.path = "#{parent.path}/#{parts.last}"
24
+ self.parent = parent
25
+ parse_parts(parts)
26
+ end
27
+
28
+ def parse_parts(parts)
29
+ self.permissions = parts[0]
30
+ self.owner = parts[2]
31
+ self.group = parts[3]
32
+ self.size = parts[4]
33
+ self.last_modified = DateTime.parse("#{parts[5]} #{parts[6]} #{parts[8]} #{parts[7]}")
34
+ self.name = parts.last
35
+ end
36
+
37
+ def connection
38
+ parent.connection
39
+ end
40
+
41
+ def contents
42
+ c = ""
43
+ connection.retrlines("RETR #{self.path}") do |line|
44
+ c += line + "\n"
45
+ end
46
+ c
47
+ end
48
+
49
+ end
50
+
51
+ class RftpDirectory < RftpFile
52
+ attr_accessor :files
53
+
54
+ def file?
55
+ false
56
+ end
57
+
58
+ def dir?
59
+ true
60
+ end
61
+
62
+ def contents
63
+ raise "Can't get contents of a directory, use ls"
64
+ end
65
+
66
+ def initialize(path, parent, lines = nil, parts = nil)
67
+ self.parent = parent
68
+ self.path = path
69
+ if lines
70
+ self.files = []
71
+ lines.each do |line|
72
+ parts = line.split(/\s+/)
73
+
74
+ if parts[0] =~ /^d/
75
+ self.files << RftpDirectory.new("#{self.path}#{parts.last}/", self, nil, parts)
76
+ else
77
+ self.files << RftpFile.new(parts, self)
78
+ end
79
+ end
80
+
81
+ else
82
+ parse_parts(parts)
83
+ end
84
+ end
85
+
86
+ def ls
87
+ if self.files
88
+ self.files
89
+ else
90
+ parent.ls(path)
91
+ end
92
+ end
93
+
94
+ def each(&block)
95
+ ls.each do |file|
96
+ yield file
97
+ end
98
+ end
99
+
100
+ def collect(&block)
101
+ ls.collect do |file|
102
+ yield file
103
+ end
104
+ end
105
+
106
+ def [](num)
107
+ ls[num]
108
+ end
109
+ end
110
+
111
+
112
+ class Rftp
113
+ attr_accessor :connection, :root
114
+
115
+ def initialize(options)
116
+ options.rftp_symbolize_keys
117
+
118
+ begin
119
+ self.connection = Net::FTP.new options[:host]
120
+ rescue SocketError => socket_error
121
+ raise "Unable to connect to #{options[:host]}. #{socket_error}"
122
+ end
123
+
124
+ begin
125
+ if options[:username] && options[:password]
126
+ self.connection.login options[:username], options[:password]
127
+ else
128
+ self.connection.login
129
+ end
130
+ rescue Net::FTPPermError => error
131
+ raise "Unable to login to #{options[:host]}. #{error}"
132
+ end
133
+
134
+ if options[:passive]
135
+ self.connection.passive = true
136
+ end
137
+
138
+ self.root = self.connection.pwd
139
+ end
140
+
141
+ def each(&block)
142
+ ls.each do |file|
143
+ yield file
144
+ end
145
+ end
146
+
147
+ def collect(&block)
148
+ ls.collect do |file|
149
+ yield file
150
+ end
151
+ end
152
+
153
+ def [](num)
154
+ ls[num]
155
+ end
156
+
157
+ def ls(path = ".")
158
+ self.connection.chdir path
159
+ RftpDirectory.new(self.root, self, self.connection.ls("-T"))
160
+ end
161
+ end
162
+
163
+
164
+
165
+
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rftp
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.14"
5
+ platform: ruby
6
+ authors:
7
+ - Brian Collins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-25 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A wrapper for net::ftp that is tolerable
17
+ email: bricollins@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README
26
+ - LICENSE
27
+ - lib/rftp.rb
28
+ has_rdoc: true
29
+ homepage: http://google.com
30
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options: []
34
+
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ version:
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ requirements: []
50
+
51
+ rubyforge_project:
52
+ rubygems_version: 1.3.5
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: A tolerable net::ftp wrapper
56
+ test_files: []
57
+