briancollins-rftp 1.0

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 +138 -0
  4. metadata +55 -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
data/lib/rftp.rb ADDED
@@ -0,0 +1,138 @@
1
+ require 'net/ftp'
2
+ require 'date'
3
+
4
+ class RftpFile
5
+ attr_accessor :parent
6
+ attr_accessor :name, :path, :permissions, :owner, :group, :size, :last_modified
7
+
8
+
9
+ def initialize(parts, parent)
10
+ self.path = "#{parent.path}#{parts.last}"
11
+ self.parent = parent
12
+ parse_parts(parts)
13
+ end
14
+
15
+ def parse_parts(parts)
16
+ self.permissions = parts[0]
17
+ self.owner = parts[2]
18
+ self.group = parts[3]
19
+ self.size = parts[4]
20
+ self.last_modified = DateTime.parse("#{parts[5]} #{parts[6]} #{parts[8]} #{parts[7]}")
21
+ self.name = parts.last
22
+ end
23
+
24
+ def connection
25
+ parent.connection
26
+ end
27
+
28
+ def contents
29
+ c = ""
30
+ connection.retrlines("RETR #{self.path}") do |line|
31
+ c += line + "\n"
32
+ end
33
+ c
34
+ end
35
+
36
+ end
37
+
38
+ class RftpDirectory < RftpFile
39
+ attr_accessor :files
40
+
41
+ def contents
42
+ raise "Can't get contents of a directory, use ls"
43
+ end
44
+
45
+ def initialize(path, parent, lines = nil, parts = nil)
46
+ self.parent = parent
47
+ self.path = path
48
+ if lines
49
+ self.files = []
50
+ lines.each do |line|
51
+ parts = line.split(/\s+/)
52
+
53
+ if parts[0] =~ /^d/
54
+ self.files << RftpDirectory.new("#{self.path}#{parts.last}/", self, nil, parts)
55
+ else
56
+ self.files << RftpFile.new(parts, self)
57
+ end
58
+ end
59
+
60
+ else
61
+ parse_parts(parts)
62
+ end
63
+ end
64
+
65
+ def ls
66
+ if self.files
67
+ self.files
68
+ else
69
+ parent.ls(path)
70
+ end
71
+ end
72
+
73
+ def each(&block)
74
+ ls.each do |file|
75
+ yield file
76
+ end
77
+ end
78
+
79
+ def collect(&block)
80
+ ls.collect do |file|
81
+ yield file
82
+ end
83
+ end
84
+
85
+ def [](num)
86
+ ls[num]
87
+ end
88
+ end
89
+
90
+
91
+ class Rftp
92
+ attr_accessor :connection, :root
93
+
94
+ def initialize(options)
95
+ begin
96
+ self.connection = Net::FTP.new options[:host]
97
+ rescue SocketError => socket_error
98
+ raise "Unable to connect to #{options[:host]}. #{socket_error}"
99
+ end
100
+
101
+ begin
102
+ if options[:username] && options[:password]
103
+ self.connection.login options[:username], options[:password]
104
+ else
105
+ self.connection.login
106
+ end
107
+ rescue Net::FTPPermError => error
108
+ raise "Unable to login to #{options[:host]}. #{error}"
109
+ end
110
+
111
+ self.root = self.connection.pwd
112
+ end
113
+
114
+ def each(&block)
115
+ ls.each do |file|
116
+ yield file
117
+ end
118
+ end
119
+
120
+ def collect(&block)
121
+ ls.collect do |file|
122
+ yield file
123
+ end
124
+ end
125
+
126
+ def [](num)
127
+ ls[num]
128
+ end
129
+
130
+ def ls(path = ".")
131
+ self.connection.chdir path
132
+ RftpDirectory.new(self.root, self, self.connection.ls("-T"))
133
+ end
134
+ end
135
+
136
+
137
+
138
+
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: briancollins-rftp
3
+ version: !ruby/object:Gem::Version
4
+ version: "1.0"
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 not shit
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: false
29
+ homepage: http://google.com
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.2.0
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: A non-shit net::ftp wrapper
54
+ test_files: []
55
+