easy_downloader 0.0.1.alpha → 0.0.2.alpha

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,13 +1,16 @@
1
1
  source "http://rubygems.org"
2
2
  # Add dependencies required to use your gem here.
3
3
  # Example:
4
- # gem "activesupport", ">= 2.3.5"
4
+ gem "activesupport", "~> 3.0.3"
5
+ gem 'net-sftp'
5
6
 
6
7
  # Add dependencies to develop your gem here.
7
8
  # Include everything needed to run rake, tests, features, etc.
8
9
  group :development do
10
+ gem 'ruby-debug'
9
11
  gem "rspec", "~> 2.1.0"
10
12
  gem "bundler", "~> 1.0.0"
11
13
  gem "jeweler", "~> 1.5.1"
12
14
  gem "rcov", ">= 0"
15
+ gem 'yard'
13
16
  end
data/Gemfile.lock CHANGED
@@ -1,12 +1,18 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
+ activesupport (3.0.3)
5
+ columnize (0.3.2)
4
6
  diff-lcs (1.1.2)
5
7
  git (1.2.5)
6
8
  jeweler (1.5.2)
7
9
  bundler (~> 1.0.0)
8
10
  git (>= 1.2.5)
9
11
  rake
12
+ linecache (0.43)
13
+ net-sftp (2.0.5)
14
+ net-ssh (>= 2.0.9)
15
+ net-ssh (2.0.23)
10
16
  rake (0.8.7)
11
17
  rcov (0.9.9)
12
18
  rspec (2.1.0)
@@ -17,12 +23,22 @@ GEM
17
23
  rspec-expectations (2.1.0)
18
24
  diff-lcs (~> 1.1.2)
19
25
  rspec-mocks (2.1.0)
26
+ ruby-debug (0.10.4)
27
+ columnize (>= 0.1)
28
+ ruby-debug-base (~> 0.10.4.0)
29
+ ruby-debug-base (0.10.4)
30
+ linecache (>= 0.3)
31
+ yard (0.6.1)
20
32
 
21
33
  PLATFORMS
22
34
  ruby
23
35
 
24
36
  DEPENDENCIES
37
+ activesupport (~> 3.0.3)
25
38
  bundler (~> 1.0.0)
26
39
  jeweler (~> 1.5.1)
40
+ net-sftp
27
41
  rcov
28
42
  rspec (~> 2.1.0)
43
+ ruby-debug
44
+ yard
data/Rakefile CHANGED
@@ -15,7 +15,7 @@ Jeweler::Tasks.new do |gem|
15
15
  gem.name = "easy_downloader"
16
16
  gem.homepage = "http://github.com/btelles/easy_downloader"
17
17
  gem.license = "MIT"
18
- gem.summary = %Q{One-method downloading, with error handling and friendly messages}
18
+ gem.summary = %Q{A dead simple, one-method downloader, with friendly error messages}
19
19
  gem.description = %Q{ EasyDownloader reduces the amount of work required to setup and check for errors when downloading from another location. This
20
20
  is useful when, for example, a client wants to you to pick a file up from their FTP, SFTP, or regular website on a nightly basis.
21
21
  EasyDownloader gives you a one-method means of downloading those files, returns with a friendly error message if it fails
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1.alpha
1
+ 0.0.2.alpha
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{easy_downloader}
8
- s.version = "0.0.1.alpha"
8
+ s.version = "0.0.2.alpha"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Bernardo Telles"]
@@ -30,16 +30,21 @@ Gem::Specification.new do |s|
30
30
  "VERSION",
31
31
  "easy_downloader.gemspec",
32
32
  "lib/easy_downloader.rb",
33
- "spec/easy_downloader_spec.rb",
33
+ "lib/easy_downloader/ftp_downloader.rb",
34
+ "lib/easy_downloader/http_downloader.rb",
35
+ "lib/easy_downloader/options.rb",
36
+ "lib/easy_downloader/result.rb",
37
+ "lib/easy_downloader/sftp_downloader.rb",
38
+ "spec/easy_downloader/options_spec.rb",
34
39
  "spec/spec_helper.rb"
35
40
  ]
36
41
  s.homepage = %q{http://github.com/btelles/easy_downloader}
37
42
  s.licenses = ["MIT"]
38
43
  s.require_paths = ["lib"]
39
44
  s.rubygems_version = %q{1.3.7}
40
- s.summary = %q{One-method downloading, with error handling and friendly messages}
45
+ s.summary = %q{A dead simple, one-method downloader, with friendly error messages}
41
46
  s.test_files = [
42
- "spec/easy_downloader_spec.rb",
47
+ "spec/easy_downloader/options_spec.rb",
43
48
  "spec/spec_helper.rb"
44
49
  ]
45
50
 
@@ -48,21 +53,33 @@ Gem::Specification.new do |s|
48
53
  s.specification_version = 3
49
54
 
50
55
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
56
+ s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.3"])
57
+ s.add_runtime_dependency(%q<net-sftp>, [">= 0"])
58
+ s.add_development_dependency(%q<ruby-debug>, [">= 0"])
51
59
  s.add_development_dependency(%q<rspec>, ["~> 2.1.0"])
52
60
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
53
61
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
54
62
  s.add_development_dependency(%q<rcov>, [">= 0"])
63
+ s.add_development_dependency(%q<yard>, [">= 0"])
55
64
  else
65
+ s.add_dependency(%q<activesupport>, ["~> 3.0.3"])
66
+ s.add_dependency(%q<net-sftp>, [">= 0"])
67
+ s.add_dependency(%q<ruby-debug>, [">= 0"])
56
68
  s.add_dependency(%q<rspec>, ["~> 2.1.0"])
57
69
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
58
70
  s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
59
71
  s.add_dependency(%q<rcov>, [">= 0"])
72
+ s.add_dependency(%q<yard>, [">= 0"])
60
73
  end
61
74
  else
75
+ s.add_dependency(%q<activesupport>, ["~> 3.0.3"])
76
+ s.add_dependency(%q<net-sftp>, [">= 0"])
77
+ s.add_dependency(%q<ruby-debug>, [">= 0"])
62
78
  s.add_dependency(%q<rspec>, ["~> 2.1.0"])
63
79
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
64
80
  s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
65
81
  s.add_dependency(%q<rcov>, [">= 0"])
82
+ s.add_dependency(%q<yard>, [">= 0"])
66
83
  end
67
84
  end
68
85
 
@@ -0,0 +1,73 @@
1
+ require 'easy_downloader/result'
2
+ require 'easy_downloader/options'
3
+ require 'easy_downloader/sftp_downloader'
4
+ require 'easy_downloader/ftp_downloader'
5
+ require 'easy_downloader/http_downloader'
6
+ require 'net/sftp'
7
+ module EasyDownloader
8
+
9
+ class InvalidRemoteDirectory < Exception; end
10
+ def self.download(*options)
11
+ Downloader.new(*options)
12
+ end
13
+
14
+ class Downloader
15
+ attr_reader :files, :result
16
+
17
+ include SftpDownloader
18
+ include FtpDownloader
19
+ include HttpDownloader
20
+
21
+
22
+ # Set up and immediately download files. The object has only two attributes:
23
+ # 1. result
24
+ # 2. files
25
+ #
26
+ # @param [Hash, #options] options to use including host, password, source, and destination directories.%
27
+ # The available keys are:
28
+ #
29
+ # :type => (required) The protocal used to download the file. One of :sftp, :ftp or :http.
30
+ # :host => (required) the domain of the remote server (source)
31
+ # :remote_path => a relative path to "cd" into when we first access the remote server
32
+ # :remote_pattern => a glob pattern that will select only the files we want to download.%
33
+ # see Ruby's core API if you are not familiar with this concept.
34
+ # :user => a username or login used for authenticating on the server
35
+ # :password => a password that, in combination with the user, will grant access to the remote server/host
36
+ def initialize(*options)
37
+ @options= Options.new(*options)
38
+ download
39
+ @result = @options.result
40
+ @files = @result.files_downloaded
41
+ end
42
+
43
+ private
44
+
45
+ def download
46
+ begin
47
+ case @options.type
48
+ when :ftp
49
+ ftp_download(@options)
50
+ when :http
51
+ http_download(@options)
52
+ when :sftp
53
+ sftp_download(@options)
54
+ end
55
+
56
+ rescue Exception => e
57
+ @options.result.errors= <<-ERROR_MESSAGE
58
+ There was a problem downloading from #{@options.host}.
59
+ The error we got was:
60
+ #{e.message}
61
+ #{e.backtrace.inspect}
62
+ We tried connecting with:
63
+ host: #{@options.host}
64
+ user: #{@options.user}
65
+ pass: [filtered]
66
+ directory_path: #{@options.remote_path}
67
+ file_pattern: #{@options.remote_pattern}
68
+ destination: #{@options.local_path}
69
+ ERROR_MESSAGE
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,39 @@
1
+ require 'net/ftp'
2
+ module EasyDownloader
3
+ module FtpDownloader
4
+ def ftp_download(options)
5
+ download_count = options.download_count
6
+ result = options.result
7
+ result.started
8
+ Net::FTP.open(options.host,
9
+ options.user,
10
+ ftp_password_option(options.password)) do |ftp|
11
+
12
+ begin
13
+ ftp.chdir(options.remote_path)
14
+ rescue InvalidRemoteDirectory
15
+ end
16
+
17
+ files = ftp.nlst.select {|file_name| options.remote_pattern == '*' || file_name =~ Regexp.new(options.remote_pattern) }
18
+ puts files.join("\n")
19
+ total = files.size
20
+ result.found(files.size, files)
21
+
22
+ files.each do |path|
23
+ result.starting_path(path)
24
+ ftp.get(path, "#{options.local_path}#{path}")
25
+ download_count += 1
26
+ result.finished_path(path)
27
+ result.files_downloaded << "#{options.local_path}#{path}"
28
+ end
29
+ end
30
+
31
+ result.downloaded(download_count)
32
+ result.finished
33
+ end
34
+
35
+ def ftp_password_option(password)
36
+ password ? password : nil
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,30 @@
1
+ module HttpDownloader
2
+ def http_download(options)
3
+ #result = options.result
4
+ #result.started
5
+ #Net::SFTP.start(options.host,
6
+ # options.user,
7
+ # password_option(options.password)) do |sftp|
8
+
9
+ # files = sftp.dir.glob(options.remote_path, options.remote_pattern)
10
+ # total = files.size
11
+ # result.found(files.size, files.map(&:name))
12
+
13
+ # files.map(&:name).each do |path|
14
+ # result.starting_path(path)
15
+ # download_count += 1 if sftp.download!("#{options.remote_path}#{path}", "#{options.destination_dir}#{path}")
16
+ # result.finished_path(path)
17
+ # files_downloaded << "#{options.destination_dir}#{path}"
18
+ # end
19
+ #end
20
+
21
+ #result.downloaded(download_count)
22
+ #result.finished
23
+ end
24
+
25
+ def password_option(password)
26
+ password ?
27
+ {:password => password} :
28
+ Hash.new
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ require 'active_support/core_ext/array/extract_options'
2
+ module EasyDownloader
3
+ class Options
4
+ attr_accessor :files, :successful, :result, :download_count,
5
+ :type, :host, :user, :password, :local_path,
6
+ :destination_dir, :remote_path, :remote_pattern
7
+
8
+ def initialize(*options)
9
+ @files = []
10
+ @successful = false
11
+ @result = Result.new
12
+ @download_count = 0
13
+ @options = options
14
+
15
+ options.extract_options!.each do |key, value|
16
+ send("#{key}=".to_sym, value) if respond_to?("#{key}=".to_sym)
17
+ end
18
+ end
19
+
20
+ def type
21
+ @type.to_sym
22
+ end
23
+
24
+ def local_path
25
+ @local_path =~ /\/$/ ?
26
+ @local_path :
27
+ @local_path + '/'
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,71 @@
1
+ module EasyDownloader
2
+ class Result
3
+ attr_accessor :errors, :files_downloaded
4
+
5
+ def initialize
6
+ @header, @started, @finished, @errors= ''
7
+ @files_downloaded = []
8
+ @progress = ["Progress:"]
9
+ end
10
+
11
+ def to_s
12
+ [@header,
13
+ found_list,
14
+ started_string,
15
+ progress,
16
+ @errors,
17
+ finished_string,
18
+ @footer].join("\n")
19
+ end
20
+
21
+ def found(total_found, file_names)
22
+ @found = total_found
23
+ @header= "We found #{total_found} file(s) to download with the following names: \n"
24
+ @found_list = file_names
25
+ end
26
+
27
+ def started
28
+ @started= Time.now
29
+ end
30
+
31
+ def finished
32
+ @finished= Time.now
33
+ end
34
+
35
+ def downloaded(total)
36
+ @downloaded = total
37
+ @footer= "Downloaded #{total} file(s)"
38
+ end
39
+
40
+ def starting_path(path)
41
+ @progress << "Starting to download #{path}"
42
+ end
43
+
44
+ def finished_path(path)
45
+ @progress << "Finished downloading #{path}"
46
+ end
47
+
48
+ private
49
+
50
+ def progress
51
+ @progress.join("\n")
52
+ end
53
+
54
+ def found_list
55
+ full_list = ''
56
+ @found_list.each_with_index do |file_name, index|
57
+ full_list += "##{(index+1).to_s}. #{file_name} "
58
+ end
59
+ full_list
60
+ end
61
+
62
+ def started_string
63
+ "Started downloading at #{@started}"
64
+ end
65
+
66
+ def finished_string
67
+ "Finished downloading at #{@finished}"
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,33 @@
1
+ module EasyDownloader
2
+ module SftpDownloader
3
+ def sftp_download(options)
4
+ result = options.result
5
+ result.started
6
+
7
+ Net::SFTP.start(options.host,
8
+ options.user,
9
+ sftp_password_option(options.password)) do |sftp|
10
+
11
+ files = sftp.dir.glob(options.remote_path, options.remote_pattern)
12
+ total = files.size
13
+ result.found(files.size, files.map(&:name))
14
+
15
+ files.map(&:name).each do |path|
16
+ result.starting_path(path)
17
+ options.download_count += 1 if sftp.download!("#{options.remote_path}#{path}", "#{options.local_path}#{path}")
18
+ result.finished_path(path)
19
+ result.files_downloaded << "#{options.local_path}#{path}"
20
+ end
21
+ end
22
+
23
+ result.downloaded(options.download_count)
24
+ result.finished
25
+ end
26
+
27
+ def sftp_password_option(password)
28
+ password ?
29
+ {:password => password} :
30
+ Hash.new
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ require 'easy_downloader/options'
4
+
5
+ describe EasyDownloader::Options do
6
+ context 'Accessors' do
7
+ [:files, :successful, :result, :download_count,
8
+ :host, :user, :password, :local_path,
9
+ :destination_dir, :remote_path, :remote_pattern].each do |accessor|
10
+ specify {subject.should respond_to(accessor)}
11
+ end
12
+ end
13
+
14
+ describe "exposes options" do
15
+ [:host, :user, :password, :remote_path, :remote_pattern, :local_path].each_with_index do |exposable_option, index|
16
+ it "exposes #{exposable_option}" do
17
+ option = EasyDownloader::Options.new(exposable_option => "hi there #{index}")
18
+ option.send(exposable_option).should == "hi there #{index}"
19
+ end
20
+ end
21
+ it "exposes type as a symbol" do
22
+ option = EasyDownloader::Options.new(:type => "hithere")
23
+ option.type.should == :hithere
24
+ end
25
+ end
26
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
  require 'rspec'
4
+ require 'ruby-debug'
4
5
  require 'easy_downloader'
5
6
 
6
7
  # Requires supporting files with custom matchers and macros, etc,
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_downloader
3
3
  version: !ruby/object:Gem::Version
4
- hash: 296151482
4
+ hash: 296151478
5
5
  prerelease: true
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
9
+ - 2
10
10
  - alpha
11
- version: 0.0.1.alpha
11
+ version: 0.0.2.alpha
12
12
  platform: ruby
13
13
  authors:
14
14
  - Bernardo Telles
@@ -21,6 +21,50 @@ default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
23
23
  version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 1
29
+ segments:
30
+ - 3
31
+ - 0
32
+ - 3
33
+ version: 3.0.3
34
+ requirement: *id001
35
+ prerelease: false
36
+ type: :runtime
37
+ name: activesupport
38
+ - !ruby/object:Gem::Dependency
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ requirement: *id002
49
+ prerelease: false
50
+ type: :runtime
51
+ name: net-sftp
52
+ - !ruby/object:Gem::Dependency
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirement: *id003
63
+ prerelease: false
64
+ type: :development
65
+ name: ruby-debug
66
+ - !ruby/object:Gem::Dependency
67
+ version_requirements: &id004 !ruby/object:Gem::Requirement
24
68
  none: false
25
69
  requirements:
26
70
  - - ~>
@@ -31,12 +75,12 @@ dependencies:
31
75
  - 1
32
76
  - 0
33
77
  version: 2.1.0
34
- requirement: *id001
78
+ requirement: *id004
35
79
  prerelease: false
36
80
  type: :development
37
81
  name: rspec
38
82
  - !ruby/object:Gem::Dependency
39
- version_requirements: &id002 !ruby/object:Gem::Requirement
83
+ version_requirements: &id005 !ruby/object:Gem::Requirement
40
84
  none: false
41
85
  requirements:
42
86
  - - ~>
@@ -47,12 +91,12 @@ dependencies:
47
91
  - 0
48
92
  - 0
49
93
  version: 1.0.0
50
- requirement: *id002
94
+ requirement: *id005
51
95
  prerelease: false
52
96
  type: :development
53
97
  name: bundler
54
98
  - !ruby/object:Gem::Dependency
55
- version_requirements: &id003 !ruby/object:Gem::Requirement
99
+ version_requirements: &id006 !ruby/object:Gem::Requirement
56
100
  none: false
57
101
  requirements:
58
102
  - - ~>
@@ -63,12 +107,12 @@ dependencies:
63
107
  - 5
64
108
  - 1
65
109
  version: 1.5.1
66
- requirement: *id003
110
+ requirement: *id006
67
111
  prerelease: false
68
112
  type: :development
69
113
  name: jeweler
70
114
  - !ruby/object:Gem::Dependency
71
- version_requirements: &id004 !ruby/object:Gem::Requirement
115
+ version_requirements: &id007 !ruby/object:Gem::Requirement
72
116
  none: false
73
117
  requirements:
74
118
  - - ">="
@@ -77,10 +121,24 @@ dependencies:
77
121
  segments:
78
122
  - 0
79
123
  version: "0"
80
- requirement: *id004
124
+ requirement: *id007
81
125
  prerelease: false
82
126
  type: :development
83
127
  name: rcov
128
+ - !ruby/object:Gem::Dependency
129
+ version_requirements: &id008 !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ hash: 3
135
+ segments:
136
+ - 0
137
+ version: "0"
138
+ requirement: *id008
139
+ prerelease: false
140
+ type: :development
141
+ name: yard
84
142
  description: " EasyDownloader reduces the amount of work required to setup and check for errors when downloading from another location. This \n is useful when, for example, a client wants to you to pick a file up from their FTP, SFTP, or regular website on a nightly basis.\n EasyDownloader gives you a one-method means of downloading those files, returns with a friendly error message if it fails\n (geared at cron job notifications), or returns an array of file names it downloaded."
85
143
  email: btelles@gmail.com
86
144
  executables: []
@@ -101,7 +159,12 @@ files:
101
159
  - VERSION
102
160
  - easy_downloader.gemspec
103
161
  - lib/easy_downloader.rb
104
- - spec/easy_downloader_spec.rb
162
+ - lib/easy_downloader/ftp_downloader.rb
163
+ - lib/easy_downloader/http_downloader.rb
164
+ - lib/easy_downloader/options.rb
165
+ - lib/easy_downloader/result.rb
166
+ - lib/easy_downloader/sftp_downloader.rb
167
+ - spec/easy_downloader/options_spec.rb
105
168
  - spec/spec_helper.rb
106
169
  has_rdoc: true
107
170
  homepage: http://github.com/btelles/easy_downloader
@@ -138,7 +201,7 @@ rubyforge_project:
138
201
  rubygems_version: 1.3.7
139
202
  signing_key:
140
203
  specification_version: 3
141
- summary: One-method downloading, with error handling and friendly messages
204
+ summary: A dead simple, one-method downloader, with friendly error messages
142
205
  test_files:
143
- - spec/easy_downloader_spec.rb
206
+ - spec/easy_downloader/options_spec.rb
144
207
  - spec/spec_helper.rb
@@ -1,7 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "EasyDownloader" do
4
- it "fails" do
5
- fail "hey buddy, you should probably rename this file and start specing for real"
6
- end
7
- end