ftp_liar 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46a94e03d4451cb163649ebbca4ea29b357c1b4b
4
- data.tar.gz: 0682537419402274ba2b73e1a7aa5af171afe1cf
3
+ metadata.gz: f4d0a590a5f11f6c92e906d855eb9f9701edd853
4
+ data.tar.gz: bad3d60b8d98ed04c2d027cc5772b2258e39ecc0
5
5
  SHA512:
6
- metadata.gz: eb7fc9663f7137a6764d6af8fc366807341489ecf75f6cfc932263b1b9a03a9002979179a8082b4770b8ba48743820020b3a93d312a5b05b71a93cd5938903ba
7
- data.tar.gz: d65874cef4501bd231d7ae3768f58c5eb857b71b3f99798b1fe0d69318e907cd201e1367001d182628a0c345bacb6470102a0596bae8cdedb051f9f90746b158
6
+ metadata.gz: 898fe0c3632611ec39ec626697e203c83c29f21eb9b9796a4611f0e12c9397879ec9853a38cbf7a098c88171378c1a564901bae45c437886c7820b233bd9f682
7
+ data.tar.gz: d18a986d07309963155b876f4b7bbff8e12a146a8ecfccb0b012ca6f36641fc4b114c9dd0029aba052c26e927bf695cf4e2178b2cb15c8d422e67459f4cb5672
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 2.3.0@ftp_liar
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in ftp_liar.gemspec
4
4
  gemspec
5
+
6
+ gem 'faker'
data/README.md CHANGED
@@ -24,7 +24,7 @@ Use FTPLiar class with proxy pattern, override interesting class.
24
24
  ```ruby
25
25
  class MyFTP
26
26
  def initialize(*args)
27
- @ftp_liar = FTPLiar::FTPLiar.new
27
+ @ftp_liar = FTPLiar::FTPLiar.new(*args)
28
28
  end
29
29
 
30
30
  def method_missing(name, *args)
@@ -32,12 +32,23 @@ class MyFTP
32
32
  end
33
33
  end
34
34
  ```
35
- ## Development
36
35
 
37
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
+ If some method raise NotImplementedError override create your own method like in the example
37
+ ```ruby
38
+ class MyFTP
39
+ def initialize(*args)
40
+ @ftp_liar = FTPLiar::FTPLiar.new(*args)
41
+ end
38
42
 
39
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+ def mdtm(filename)
44
+ # Do here what do you want.
45
+ end
40
46
 
47
+ def method_missing(name, *args)
48
+ @ftp_liar.send(name, *args)
49
+ end
50
+ end
51
+ ```
41
52
  ## Contributing
42
53
 
43
54
  Bug reports and pull requests are welcome on GitHub at https://github.com/Draqun/ftp_liar.
data/TODO ADDED
@@ -0,0 +1 @@
1
+ Some method can use blocks as parameters. Find this methods and implement it.
data/ftp_liar.gemspec CHANGED
@@ -23,4 +23,6 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 1.10"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
25
  spec.add_development_dependency "rspec", "~> 3.3"
26
+ spec.add_development_dependency "simplecov"
27
+ spec.add_development_dependency "byebug"
26
28
  end
data/lib/ftp_liar.rb CHANGED
@@ -1,57 +1,404 @@
1
1
  require "ftp_liar/version"
2
+ require_relative 'ftp_liar_net'
2
3
  require 'fileutils'
3
4
  require 'tmpdir'
5
+ require 'faker'
4
6
 
5
7
  module FTPLiar
6
8
  class FTPLiar
7
9
  # FTP Liar
8
10
  # Simple class for test aplication using Net::FTP
9
11
  attr_writer :open_timeout, :read_timeout, :debug_mode, :passive
12
+ attr_accessor :binary
10
13
 
11
- def initialize(*args)
12
- FileUtils.cd(Dir.tmpdir)
14
+ def initialize(host = nil, user = nil, passwd = nil, acct = nil)
15
+ @ftp_directory = File.join(Dir.tmpdir, '.ftp_liar')
16
+ FileUtils.mkdir_p(@ftp_directory)
17
+ @binary = true
18
+ @passive = false
19
+ ObjectSpace.define_finalizer(self, self.method(:finalize))
20
+ if !(user.nil? && passwd.nil?) && (user.nil? || passwd.nil?)
21
+ raise Net::FTPPermError.new("530 User cannot log in.")
22
+ else
23
+ @is_connect = true
24
+ end
25
+ chdir("/")
13
26
  end
14
27
 
15
- def login(*args)
28
+ def finalize(object_id)
29
+ # Finalizer to delete ftp_liar directory
30
+ FileUtils.rm_rf(@ftp_directory)
16
31
  end
17
32
 
18
- def getbinaryfile(remotefile, localfile = nil, blocksize = nil)
19
- # A simple method that manages to copy a remote file to local
20
- FileUtils.cp(remotefile, localfile ? localfile : File.basename(remotefile))
33
+ # :nocov:
34
+ def abort()
35
+ # Aborts the previous command (ABOR command).
36
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
21
37
  end
22
38
 
23
- def putbinaryfile(localfile, remotefile = nil, blocksize = nil)
24
- # A simple method that manages to copy a local file on the FTP.
25
- FileUtils.cp(localfile, remotefile ? remotefile : File.basename(localfile))
39
+ def acct(account)
40
+ # Sends the ACCT command.
41
+ #
42
+ # This is a less common FTP command, to send account information if the destination host requires it.
43
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
26
44
  end
45
+ # :nocov:
27
46
 
28
47
  def chdir(path)
29
- # Method to change directory
48
+ # Changes the (remote) directory.
49
+ raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
50
+ if path[0] == "/"
51
+ path = if path.length == 1
52
+ @ftp_directory
53
+ else
54
+ File.join(@ftp_directory, path[1..-1])
55
+ end
56
+ end
57
+
58
+ unless absolute_path_indicates_to_ftp_directory?(path) && Dir.exist?(path)
59
+ raise Net::FTPPermError.new("500")
60
+ end
61
+
30
62
  FileUtils.cd(path)
63
+ nil
64
+ end
65
+
66
+ def close()
67
+ # Closes the connection. Further operations are impossible until you open a new connection with connect.
68
+ chdir("/")
69
+ @is_connect = false
70
+ ""
71
+ end
72
+
73
+ def closed?()
74
+ # Returns true if the connection is closed.
75
+ !@is_connect
76
+ end
77
+
78
+ def connect(host, port = 21)
79
+ # Method imitate connect method in Net::FTP
80
+ @is_connect = true
81
+ nil
82
+ end
83
+
84
+ def delete(filename)
85
+ # Method remove file on FTP
86
+ raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
87
+ filename = if filename[0] == "/"
88
+ File.join(@ftp_directory, filename[1..-1])
89
+ else
90
+ File.join(@ftp_directory, pwd[1..-1], filename)
91
+ end
92
+ unless absolute_path_indicates_to_ftp_directory?(filename) && File.exist?(filename)
93
+ raise Net::FTPPermError.new("550")
94
+ end
95
+ File.delete(filename)
96
+ nil
31
97
  end
32
98
 
99
+ # :nocov:
100
+ def dir(*args)
101
+ # Alias for list
102
+ list(*args)
103
+ end
104
+ # :nocov:
105
+
106
+ def get(remotefile, localfile = File.basename(remotefile), blocksize = nil)
107
+ # A simple method that manages to copy a remote file to local
108
+ raise Net::FTPPermError("530 Please login with USER and PASS.") unless @is_connect
109
+ if remotefile[0] == "/"
110
+ if remotefile.length > 1
111
+ remotefile = File.join(@ftp_directory, pwd[1..-1], remotefile[1..-1])
112
+ elsif remotefile == "/"
113
+ raise Errno::EISDIR
114
+ end
115
+ end
116
+
117
+ unless absolute_path_indicates_to_ftp_directory?(remotefile) && File.exist?(remotefile)
118
+ raise Net::FTPPermError.new("550")
119
+ end
120
+
121
+ localdir = localfile.split("/")[0...-1].join("/")
122
+ if File.directory?(localfile)
123
+ raise Errno::EISDIR
124
+ end
125
+ unless Dir.exist?(localdir)
126
+ raise Errno::ENOENT
127
+ end
128
+ copy_file(remotefile, localfile)
129
+ nil
130
+ end
131
+
132
+ # :nocov:
133
+ def getbinaryfile(*args)
134
+ # A simple method that manages to copy a remote file to local
135
+ get(*args)
136
+ end
137
+
138
+ def getdir()
139
+ pwd
140
+ end
141
+
142
+ def gettextfile(*args)
143
+ get(*args)
144
+ end
145
+
146
+ def help(arg = nil)
147
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
148
+ end
149
+
150
+ def list(*args)
151
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
152
+ end
153
+ # :nocov:
154
+
155
+ def login(user = "anonymous", passwd = nil, acct = nil)
156
+ # Method imitate login to ftp. When login is "anonymous" it "connect" without password
157
+ if user != "anonymous" && (user.nil? || passwd.nil?)
158
+ raise Net::FTPPermError.new("530 User cannot log in.")
159
+ end
160
+ @is_connect = true
161
+ end
162
+
163
+ # :nocov:
164
+ def ls(*args)
165
+ # Alias for list
166
+ list(*args)
167
+ end
168
+
169
+ def mdtm(filename)
170
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
171
+ end
172
+ # :nocov:
173
+
174
+ def mkdir(dirname)
175
+ # Creates a remote directory.
176
+ raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
177
+ new_dirname = create_path(dirname)
178
+
179
+ if !absolute_path_indicates_to_ftp_directory?(new_dirname) || File.exist?(new_dirname)
180
+ raise Net::FTPPermError.new("550")
181
+ end
182
+
183
+ Dir.mkdir(new_dirname)
184
+ dirname
185
+ end
186
+
187
+ # :nocov:
188
+ def mtime(filename, local = false)
189
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
190
+ end
191
+ # :nocov:
192
+
33
193
  def nlst(path = '.')
34
194
  # A simple method to list data in directory, return list with filename if file
35
- if File.file?(path)
195
+ # Method does not work as method in Net::FTP. I started topic about it on https://bugs.ruby-lang.org/issues/11407 because I think original method has bugs
196
+ raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
197
+ new_path = create_path(path)
198
+
199
+ unless absolute_path_indicates_to_ftp_directory?(new_path)
200
+ raise Net::FTPPermError.new("550")
201
+ end
202
+
203
+ if File.file?(new_path)
36
204
  [path]
37
205
  else
38
- Dir.entries(path).sort[2..-1]
206
+ Dir.entries(new_path).sort[2..-1]
207
+ end
208
+ end
209
+
210
+ # :nocov:
211
+ def noop()
212
+ # Does nothing
213
+ nil
214
+ end
215
+ # :nocov:
216
+
217
+ def put(localfile, remotefile = File.basename(localfile), blocksize = nil)
218
+ # A simple method that manages to copy a local file on the FTP.
219
+ raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
220
+ if File.directory?(localfile)
221
+ raise Errno::EISDIR
39
222
  end
223
+ unless File.exist?(localfile)
224
+ raise Errno::ENOENT
225
+ end
226
+
227
+ if remotefile[0] == "/" && remotefile.length > 1
228
+ remotefile = File.join(@ftp_directory, remotefile[1..-1])
229
+ end
230
+
231
+ unless absolute_path_indicates_to_ftp_directory?(remotefile)
232
+ raise Net::FTPPermError.new("550")
233
+ end
234
+
235
+ if File.exist?(remotefile)
236
+ raise Net::FTPPermError.new("550")
237
+ end
238
+
239
+ copy_file(localfile, remotefile)
240
+ nil
40
241
  end
41
242
 
243
+ # :nocov:
244
+ def putbinaryfile(*args)
245
+ # A simple method that manages to copy a local file on the FTP.
246
+ put(*args)
247
+ end
248
+
249
+ def puttextfile(*args)
250
+ put(*args)
251
+ end
252
+ # :nocov:
253
+
42
254
  def pwd
43
255
  # Method return actual directory
44
- Dir.pwd
256
+ raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
257
+ ftp_directory_length = @ftp_directory.length
258
+ if Dir.pwd == @ftp_directory
259
+ "/"
260
+ else
261
+ Dir.pwd[ftp_directory_length..-1]
262
+ end
45
263
  end
46
264
 
47
- def delete(filename)
48
- # Method remove file on FTP
49
- File.delete(filename)
265
+ def quit
266
+ raise Errno::EPIPE unless @is_connect
267
+ chdir("/")
268
+ @is_connect = false
269
+ nil
50
270
  end
51
271
 
272
+ def rename(fromname, toname)
273
+ raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
274
+ [fromname, toname].each do |path|
275
+ eval(%Q{
276
+ if path == "/"
277
+ raise Net::FTPPermError.new("550")
278
+ end
279
+ })
280
+ end
281
+ fromname = create_path(fromname)
282
+ toname = create_path(toname)
283
+
284
+ [fromname, toname].each do |path|
285
+ eval(%Q{
286
+ unless absolute_path_indicates_to_ftp_directory?(path)
287
+ raise Net::FTPPermError.new("550")
288
+ end
289
+
290
+ if File.directory?(path) && !Dir.entries(path).sort[2..-1].empty?
291
+ raise Net::FTPPermError.new("550")
292
+ end
293
+ })
294
+ end
295
+
296
+ FileUtils.mv(fromname, toname)
297
+ nil
298
+ end
299
+
300
+ # :nocov:
301
+ def retrbinary(cmd, blocksize, rest_offset = nil)
302
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
303
+ end
304
+
305
+ def retrlines(cmd)
306
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
307
+ end
308
+ # :nocov:
309
+
52
310
  def rmdir(dirname)
53
311
  # Method remove directory on FTP
312
+ raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
313
+ dirname = if dirname[0] == "/"
314
+ File.join(@ftp_directory, dirname[1..-1])
315
+ else
316
+ File.join(@ftp_directory, pwd[1..-1], dirname)
317
+ end
318
+
319
+ if File.file?(dirname) || !File.exist?(dirname)
320
+ raise Net::FTPPermError.new("550")
321
+ end
54
322
  Dir.delete(dirname)
323
+ nil
324
+ end
325
+
326
+ # :nocov:
327
+ def sendcmd(cmd)
328
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
329
+ end
330
+
331
+ def set_socket(sock, get_greeting=true)
332
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
333
+ end
334
+
335
+ def site(arg)
336
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
337
+ end
338
+ # :nocov:
339
+
340
+ def size(filename)
341
+ raise Net::FTPPermError.new("530 Please login with USER and PASS.") unless @is_connect
342
+ filename = create_path(filename)
343
+ unless absolute_path_indicates_to_ftp_directory?(filename)
344
+ raise Net::FTPPermError.new("550")
345
+ end
346
+
347
+ if File.directory?(filename)
348
+ raise Net::FTPPermError.new("550")
349
+ end
350
+
351
+ File.size(filename)
352
+ end
353
+
354
+ # :nocov:
355
+ def status()
356
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
357
+ end
358
+
359
+ def storbinary(cmd, file, blocksize, rest_offset = nil)
360
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
361
+ end
362
+
363
+ def sotrlines(cmd, file)
364
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
365
+ end
366
+
367
+ def system()
368
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
369
+ end
370
+
371
+ def voidcmd(cmd)
372
+ raise NotImplementedError("Method not implemented. Override it if you want use this method.")
373
+ end
374
+ # :nocov:
375
+
376
+ private
377
+ attr_accessor :ftp_directory
378
+ attr_accessor :is_connection
379
+
380
+ def copy_file(from, to)
381
+ FileUtils.copy_file(from, to)
382
+ end
383
+
384
+ def absolute_path_indicates_to_ftp_directory?(path)
385
+ File.absolute_path(path).start_with?(@ftp_directory)
386
+ end
387
+
388
+ def create_path(path)
389
+ if path[0] == "/"
390
+ File.join(@ftp_directory, path[1..-1])
391
+ elsif pwd == "/"
392
+ File.join(@ftp_directory, path)
393
+ else
394
+ File.join(@ftp_directory, pwd[1..-1], path)
395
+ end
396
+ end
397
+
398
+ class << self
399
+ def open(host, *args)
400
+ FTPLiar.new(host, *args)
401
+ end
55
402
  end
56
403
  end
57
404
  end
@@ -1,3 +1,3 @@
1
1
  module FTPLiar
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,3 @@
1
+ module Net
2
+ FTPPermError = Class.new(Exception)
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ftp_liar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damian Giebas
@@ -52,6 +52,34 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  description: Simple class for test application using Net::FTP object. It is not complete
56
84
  FTP local server.
57
85
  email:
@@ -62,16 +90,19 @@ extra_rdoc_files: []
62
90
  files:
63
91
  - ".gitignore"
64
92
  - ".rspec"
93
+ - ".rvmrc"
65
94
  - ".travis.yml"
66
95
  - Gemfile
67
96
  - LICENSE
68
97
  - README.md
69
98
  - Rakefile
99
+ - TODO
70
100
  - bin/console
71
101
  - bin/setup
72
102
  - ftp_liar.gemspec
73
103
  - lib/ftp_liar.rb
74
104
  - lib/ftp_liar/version.rb
105
+ - lib/ftp_liar_net.rb
75
106
  homepage: https://github.com/Draqun/ftp_liar
76
107
  licenses:
77
108
  - MIT
@@ -92,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
123
  version: '0'
93
124
  requirements: []
94
125
  rubyforge_project:
95
- rubygems_version: 2.2.2
126
+ rubygems_version: 2.5.0
96
127
  signing_key:
97
128
  specification_version: 4
98
129
  summary: Simple class for test application using Net::FTP object