ftpro 0.0.1
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 +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +87 -0
- data/Rakefile +2 -0
- data/ftpro.gemspec +23 -0
- data/lib/ftpro.rb +102 -0
- data/spec/ftpro_spec.rb +30 -0
- data/spec/spec_helper.rb +3 -0
- metadata +97 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 0dc808727ea1103f4995a294a3a9598c54bef16c
|
|
4
|
+
data.tar.gz: 190bbaa562c9d834cc691bb61f3baa256234448b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 5c4abfc0acc7982f2485a8d3096420cd377f913dae56c79ddbba515894deefd9fbc7eb30aa0bdcb5f4839efa74530f05b4ff2a968e41243f7eca77c4865d4570
|
|
7
|
+
data.tar.gz: 7e3755773dead0533e4b423c4215be138ddc2a6ed28b50a32b43c71ad9fc94f6dd850b76c4e2b38414fcd0cbec18bb79265de00d94591c0d709528c98c3e77ea
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2014 dingr
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# Ftpro overview
|
|
2
|
+
|
|
3
|
+
Ftpro is a extension class of Net::FTP that provide more function to handle Ftp
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'ftpro'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install ftpro
|
|
20
|
+
|
|
21
|
+
Or include it in your project's Gemfile
|
|
22
|
+
|
|
23
|
+
$ gem ‘ftpro’
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
This gem provide a ruby class `Ftpro`, you can use it like `Net::FTP`:
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
```ruby
|
|
31
|
+
Ftpro.open('ftp.netlab.co.jp') do |ftp|
|
|
32
|
+
ftp.login
|
|
33
|
+
files = ftp.chdir('pub/lang/ruby/contrib')
|
|
34
|
+
files = ftp.list('n*')
|
|
35
|
+
ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024)
|
|
36
|
+
end
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
Or
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
ftp = Net::FTP.new('ftp.netlab.co.jp')
|
|
43
|
+
ftp.login
|
|
44
|
+
files = ftp.chdir('pub/lang/ruby/contrib')
|
|
45
|
+
files = ftp.list('n*')
|
|
46
|
+
ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024)
|
|
47
|
+
ftp.close
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`Ftpro` add some function that draw lessons from `File` and `FileUtils` class, some example to show these:
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
ftp = Net::FTP.new('ftp.netlab.co.jp')
|
|
54
|
+
ftp.login
|
|
55
|
+
|
|
56
|
+
## Function like ftp.nlst and also show hidden file in it.
|
|
57
|
+
ftp.nlst_a(some_directory)
|
|
58
|
+
|
|
59
|
+
## Show if DIR on ftp server is a directory
|
|
60
|
+
ftp.directory?(DIR)
|
|
61
|
+
|
|
62
|
+
## Show if FILE on ftp server is a file
|
|
63
|
+
ftp.file?(FILE)
|
|
64
|
+
|
|
65
|
+
## Check if DIR is exist on ftp server, DIR can be a file or directory
|
|
66
|
+
ftp.exist?(DIR)
|
|
67
|
+
|
|
68
|
+
## Function like FileUtils#mkdir_p on ftp server
|
|
69
|
+
ftp.mkdir_p(REMOTE-DIR)
|
|
70
|
+
|
|
71
|
+
## Upload local directory and sub directory to remote dir on ftp server, directory will be create if remote dir not exist
|
|
72
|
+
ftp.put_r(local_dir, remote_dir)
|
|
73
|
+
|
|
74
|
+
## Function like FileUtils#rm_r on ftp server
|
|
75
|
+
ftp.rm_r(dir)
|
|
76
|
+
|
|
77
|
+
ftp.close
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Contributing
|
|
82
|
+
|
|
83
|
+
1. Fork it ( https://github.com/dddd1919/ftpro/fork )
|
|
84
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
85
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
86
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
87
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/ftpro.gemspec
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "ftpro"
|
|
7
|
+
spec.version = '0.0.1'
|
|
8
|
+
spec.authors = ["dddd1919"]
|
|
9
|
+
spec.email = ["dingrank@gmail.com"]
|
|
10
|
+
spec.summary = %q{"The enhanced version of net/ftp"}
|
|
11
|
+
spec.description = %q{"Provide all net/ftp method and some additional functionality"}
|
|
12
|
+
spec.homepage = "https://github.com/dddd1919/ftpro"
|
|
13
|
+
spec.license = "MIT"
|
|
14
|
+
|
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
18
|
+
spec.require_paths = ["lib"]
|
|
19
|
+
|
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
22
|
+
spec.add_development_dependency "rspec"
|
|
23
|
+
end
|
data/lib/ftpro.rb
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
require 'net/ftp'
|
|
2
|
+
|
|
3
|
+
class Ftpro < Net::FTP
|
|
4
|
+
|
|
5
|
+
## Net::FTP extension
|
|
6
|
+
|
|
7
|
+
def nlst_a(dir)
|
|
8
|
+
## Function like nlst and also show hide files
|
|
9
|
+
cmd = "NLST -a"
|
|
10
|
+
if dir
|
|
11
|
+
cmd = cmd + " " + dir
|
|
12
|
+
end
|
|
13
|
+
files = []
|
|
14
|
+
retrlines(cmd) do |line|
|
|
15
|
+
files.push(line) if !line.end_with?("/.", "/..")
|
|
16
|
+
end
|
|
17
|
+
return files
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
## File function mirror
|
|
21
|
+
def directory?(dir)
|
|
22
|
+
current_dir = pwd
|
|
23
|
+
begin
|
|
24
|
+
chdir(dir)
|
|
25
|
+
return true
|
|
26
|
+
rescue
|
|
27
|
+
return false
|
|
28
|
+
ensure
|
|
29
|
+
chdir(current_dir) ## Go back former dir
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def file?(file_path)
|
|
34
|
+
nlst(file_path)[0] == file_path
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def exist?(dir)
|
|
38
|
+
return true if !nlst(dir).empty? ## File or not empty directory
|
|
39
|
+
## Check if a empty directory
|
|
40
|
+
directory?(dir)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
## FileUtils function mirror
|
|
44
|
+
def mkdir_p(new_dir)
|
|
45
|
+
dir_parts = new_dir.split("/")
|
|
46
|
+
dir_parts.length.times do |i|
|
|
47
|
+
current_dir = dir_parts[0..i].join("/")
|
|
48
|
+
mkdir(current_dir) unless exist?(current_dir)
|
|
49
|
+
end
|
|
50
|
+
return new_dir if exist?(new_dir)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def put_r(local_dir, remote_dir)
|
|
54
|
+
## Upload local directory to ftp server with local directory name
|
|
55
|
+
return false if !File.directory?(local_dir)
|
|
56
|
+
upload_dir(local_dir, remote_dir)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def rm_r(dir)
|
|
60
|
+
dir_files = scan_ftp_dir(dir)
|
|
61
|
+
## empty fodler by the reverse of scan result
|
|
62
|
+
(dir_files.length - 1).downto(0) do |f|
|
|
63
|
+
if file?(dir_files[f])
|
|
64
|
+
delete(dir_files[f])
|
|
65
|
+
else
|
|
66
|
+
rmdir(dir_files[f])
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
def scan_ftp_dir(dir, scan_list = [])
|
|
74
|
+
return [] if !exist?(dir)
|
|
75
|
+
scan_list.concat([dir])
|
|
76
|
+
nlst_a(dir).each do |sub_file|
|
|
77
|
+
if file?(sub_file)
|
|
78
|
+
scan_list.concat([sub_file])
|
|
79
|
+
else
|
|
80
|
+
scan_ftp_dir(sub_file, scan_list)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
return scan_list
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def upload_dir(local_dir, remote_dir)
|
|
87
|
+
remote_new_dir = "#{remote_dir}/#{local_dir.split('/')[-1]}"
|
|
88
|
+
mkdir_p(remote_new_dir) ## init remote dir
|
|
89
|
+
Dir.foreach(local_dir) do |file|
|
|
90
|
+
if file != "." && file != ".."
|
|
91
|
+
file_path = local_dir + "/" + file
|
|
92
|
+
if File.directory?(file_path)
|
|
93
|
+
upload_dir(file_path, remote_new_dir)
|
|
94
|
+
else
|
|
95
|
+
putbinaryfile(file_path, remote_new_dir + '/' + file)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
return true
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
end
|
data/spec/ftpro_spec.rb
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
|
+
|
|
3
|
+
describe "Ftpro test" do
|
|
4
|
+
|
|
5
|
+
before(:each) do
|
|
6
|
+
@ftp_host = "example.com"
|
|
7
|
+
@ftp_port = "21"
|
|
8
|
+
@ftp_user = 'name'
|
|
9
|
+
@ftp_pwd = 'password'
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it 'With normal way' do
|
|
13
|
+
ftp = Ftpro.new
|
|
14
|
+
ftp.connect(@ftp_host, @ftp_port)
|
|
15
|
+
ftp.login(@ftp_user, @ftp_pwd)
|
|
16
|
+
expect(ftp.nlst.class).to eq(Array)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'With block call' do
|
|
20
|
+
Net::FTP::FTP_PORT = @ftp_port ## There will raise a warning cause FTP_PORT is a constant
|
|
21
|
+
Ftpro.open(@ftp_host) do |ftp|
|
|
22
|
+
ftp.login(@ftp_user, @ftp_pwd)
|
|
23
|
+
expect(ftp.nlst.class).to eq(Array)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it 'do some function test' do
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: ftpro
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- dddd1919
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2014-12-29 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.7'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.7'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
description: '"Provide all net/ftp method and some additional functionality"'
|
|
56
|
+
email:
|
|
57
|
+
- dingrank@gmail.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- ".gitignore"
|
|
63
|
+
- Gemfile
|
|
64
|
+
- LICENSE.txt
|
|
65
|
+
- README.md
|
|
66
|
+
- Rakefile
|
|
67
|
+
- ftpro.gemspec
|
|
68
|
+
- lib/ftpro.rb
|
|
69
|
+
- spec/ftpro_spec.rb
|
|
70
|
+
- spec/spec_helper.rb
|
|
71
|
+
homepage: https://github.com/dddd1919/ftpro
|
|
72
|
+
licenses:
|
|
73
|
+
- MIT
|
|
74
|
+
metadata: {}
|
|
75
|
+
post_install_message:
|
|
76
|
+
rdoc_options: []
|
|
77
|
+
require_paths:
|
|
78
|
+
- lib
|
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
80
|
+
requirements:
|
|
81
|
+
- - ">="
|
|
82
|
+
- !ruby/object:Gem::Version
|
|
83
|
+
version: '0'
|
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
requirements: []
|
|
90
|
+
rubyforge_project:
|
|
91
|
+
rubygems_version: 2.2.2
|
|
92
|
+
signing_key:
|
|
93
|
+
specification_version: 4
|
|
94
|
+
summary: '"The enhanced version of net/ftp"'
|
|
95
|
+
test_files:
|
|
96
|
+
- spec/ftpro_spec.rb
|
|
97
|
+
- spec/spec_helper.rb
|