downspout 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) <2011> <Phi.Sanders>
2
+
3
+ Licensed under the MIT license
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,89 @@
1
+ = Downspout
2
+
3
+ Downspout is an easy-to-use ruby library for downloading files from URLs, supporting both HTTP & FTP protocols.
4
+
5
+ = Examples
6
+ == HTTP
7
+
8
+ === Fetch files with ease :
9
+
10
+ The 'fetch_url' method accepts an URL and on a successful download returns an Downloader object. that includes the path method to the resulting temporary file.
11
+
12
+ some_url = "http://www-host.domain.tld/folder/some_image_file.gif"
13
+
14
+ dl = Downspout.fetch_url( some_url )
15
+
16
+ dl.path
17
+ => "/tmp/downloads/downspout-20110203-59488-1run8k2-0/some_image_file.gif"
18
+
19
+ === Download to a Specified Path
20
+
21
+ By default, Downspout stores the download in an automatically generated temp file, but you can also specify the path you wish the file to go to :
22
+
23
+ some_url = "http://www-host.domain.tld/folder/hipster_hacker.png"
24
+
25
+ some_path = "/my/custom/downloads/folder/my_hero.png"
26
+
27
+ Downspout.download_url_to_path( some_url, some_path )
28
+
29
+ => "/my/custom/downloads/folder/my_hero.png"
30
+
31
+ Of course, you are responsible for ensuring the specified path is usable!
32
+
33
+ == FTP
34
+
35
+ Downspout also supports FTP, but that usually requires authorization, so you should do a small amount of configuration, to map URLs by host.
36
+
37
+ Downspout::Config.add_credential( :host => "ftp-host.domain.tld",
38
+ :user_name => "luser",
39
+ :pass_word => "pAzw0rd",
40
+ :scheme => 'ftp'
41
+ )
42
+
43
+ dl = Downspout.fetch_url("ftp://ftp-host.domain.tld/folder/path/archive.zip")
44
+
45
+ dl.path
46
+ => "/tmp/downloads/downspout-20110203-59843-109cmu0-7/archive.zip"
47
+
48
+ Downspout will now attempt to create a credential on the fly, in the case where user info is embedded in the FTP URL, as in :
49
+
50
+ dl = Downspout.fetch_url("ftp://account:s3cr3t@ftp-host.domain.tld/file.zip")
51
+
52
+ dl.path
53
+ => "/tmp/downloads/downspout-20110203-62579-109cmu0-7/file.zip"
54
+
55
+ The main advantage of setting up credentials is to avoid repeatedly communicating the secret information from system to system, especially permitting the download system to receive secure URLs via service calls or text files.
56
+
57
+ *Caveat* : Credentials are only stored in-memory at this time, so should be defined via application initializers. One strategy is to use an obfuscated yaml file for the sensitive info.
58
+
59
+ == Storage and Clean-up
60
+
61
+ By default, files are stored in a "downloads" directory under "/tmp" but this can (and should) be changed via Config :
62
+
63
+
64
+ Downspout::Config.tmp_dir = "/home/luser/downloads/"
65
+
66
+ You should clean up this folder periodically, by calling
67
+
68
+ Downspout.clean_download_dir( minutes )
69
+
70
+ which takes an integer for how many minutes old a file should be in order to be removed. The default is 30. The adequate delay for your application depends on the volume of downloads and your disk capacity.
71
+
72
+
73
+ == Contributing to downspout
74
+
75
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
76
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
77
+ * Fork the project
78
+ * Start a feature/bugfix branch
79
+ * Commit and push until you are happy with your contribution
80
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
81
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
82
+
83
+ == Copyright
84
+
85
+ Copyright (c) 2011 Phi.Sanders. See LICENSE.txt for further details.
86
+
87
+ == Thanks
88
+
89
+ Thanks to my employer, VitalSource Technologies (http://www.vitalsource.com), for permission to release as open source.
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ # Rakefile
2
+ require 'rubygems'
3
+
4
+ $LOAD_PATH.unshift('lib')
5
+
6
+ require 'rake'
7
+ require 'jeweler'
8
+
9
+ Jeweler::Tasks.new do |gem|
10
+ # gem is a Gem::Specification...
11
+ # see http://docs.rubygems.org/read/chapter/20 for more options
12
+ gem.name = "downspout"
13
+ gem.homepage = "http://github.com/sci-phi/downspout"
14
+ gem.license = "MIT"
15
+ gem.summary = %Q{Downspout is an easy-to-use library for downloading files from given URLs.}
16
+ gem.description = %Q{Downspout is an easy-to-use library for downloading files from given URLs. HTTP downloads can use either Net::HTTP, or libcurl (via the Curb gem)}
17
+ gem.email = "phi.sanders@sciphi.me"
18
+ gem.authors = ["Phi.Sanders"]
19
+ gem.files = FileList['lib/**/*.rb', '[A-Z]*', 'test/**/*'].to_a
20
+ gem.add_development_dependency "jeweler", "~> 1.5.2"
21
+ gem.add_development_dependency "shoulda", ">= 0"
22
+ gem.add_development_dependency "rcov", ">= 0"
23
+ end
24
+ Jeweler::RubygemsDotOrgTasks.new
25
+
26
+ require 'rdoc'
27
+ require 'rake/rdoctask'
28
+ Rake::RDocTask.new do |rdoc|
29
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
30
+
31
+ rdoc.rdoc_dir = 'rdoc'
32
+ rdoc.title = "downspout #{version}"
33
+ rdoc.rdoc_files.include('README*')
34
+ rdoc.rdoc_files.include('lib/**/*.rb')
35
+ end
36
+
37
+ require 'rcov/rcovtask'
38
+ Rcov::RcovTask.new do |test|
39
+ test.libs << 'test'
40
+ test.pattern = 'test/**/test_*.rb'
41
+ test.verbose = true
42
+ end
43
+
44
+ require 'rake/testtask'
45
+ Rake::TestTask.new(:test) do |test|
46
+ test.libs << 'lib' << 'test'
47
+ test.pattern = 'test/**/*_test.rb'
48
+ test.verbose = true
49
+ end
50
+
51
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.2
@@ -0,0 +1,131 @@
1
+ module Downspout
2
+
3
+ class Base
4
+ class << self
5
+ end
6
+ end
7
+
8
+ class UnsupportedScheme < StandardError
9
+ end
10
+
11
+ class ExcessiveRedirects < StandardError
12
+ end
13
+
14
+ class BadURL < StandardError
15
+ end
16
+
17
+ =begin rdoc
18
+ Download a file from a given URL to a given Path on the local system
19
+ The path is optional and will default to a generated temporary file
20
+ =end
21
+ def self.download_url_to_path( some_url, some_path = nil )
22
+ $logger.debug("downspout | download_url_to_path | URL : #{some_url}")
23
+ $logger.debug("downspout | download_url_to_path | Download Path : #{some_path}")
24
+
25
+ begin
26
+ d = Downspout::Downloader.new( :url => some_url, :path => some_path )
27
+ rescue Exception => e
28
+ $logger.error("downspout | download_url_to_path | Exception : '#{e}'")
29
+ return nil if e.class == Downspout::UnsupportedScheme
30
+ raise e
31
+ end
32
+
33
+ fetched = d.download!
34
+
35
+ if !(fetched) then
36
+ $logger.error("downspout | download_url_to_path | Fetch Failed : #{d.url} ")
37
+ return nil
38
+ end
39
+
40
+ $logger.debug("downspout | download_url_to_path | Local File : #{d.path} ")
41
+ return d
42
+ end
43
+
44
+ =begin rdoc
45
+ Convenience method for downloading a file from an URL without specifying a path for storage.
46
+ =end
47
+ def self.fetch_url( the_url )
48
+ return self.download_url_to_path( the_url )
49
+ end
50
+
51
+ =begin rdoc
52
+ Utility method for validating a URL without initiating a download
53
+ =end
54
+ def self.viable_url?( url_string )
55
+ $logger.info("downspout | supported_protocol? | URL : #{url_string} ")
56
+
57
+ # remove user/password prefix if provided
58
+ clean_url = self.extract_credentials_from_url!( url_string )
59
+
60
+ begin
61
+ uri = URI.parse( clean_url )
62
+ rescue URI::InvalidURIError
63
+ $logger.warn("downspout | supported_protocol? | The format of the url is not valid : #{url_string}")
64
+ return false
65
+ end
66
+
67
+ return false unless self.supported_protocol?( uri.scheme )
68
+
69
+ # TODO : do more in-depth checks on URL validity
70
+
71
+ return true
72
+ end
73
+
74
+ =begin rdoc
75
+ Utility method for checking the support for URLs of the given network protocol or 'scheme'
76
+ =end
77
+ def self.supported_protocol?( some_protocol )
78
+ $logger.debug("downspout | supported_protocol? | protocol : #{some_protocol} ")
79
+
80
+ protocol_string = some_protocol.to_s.upcase
81
+
82
+ return true if self.supported_protocols.include?( protocol_string )
83
+
84
+ case protocol_string
85
+ when "HTTP"
86
+ return true
87
+ when "URI::HTTP"
88
+ return true
89
+ when "HTTPS"
90
+ return true
91
+ when "URI::HTTPS"
92
+ return true
93
+ when "FTP"
94
+ return true
95
+ when "URI::FTP"
96
+ return true
97
+ else
98
+ $logger.warn("downspout | supported_protocol? | #{protocol_string} is not supported by Downspout.")
99
+ end
100
+
101
+ return false
102
+ end
103
+
104
+ private
105
+
106
+ def self.supported_protocols
107
+ return ["HTTP", "HTTPS", "FTP"]
108
+ end
109
+
110
+ def self.extract_credentials_from_url!( some_url )
111
+ the_uri = URI.parse( some_url )
112
+
113
+ if the_uri.userinfo.nil? then
114
+ return some_url
115
+ end
116
+
117
+ begin
118
+ Downspout::Config.add_credential( :scheme => the_uri.scheme,
119
+ :host => the_uri.host,
120
+ :user_name => the_uri.user,
121
+ :pass_word => the_uri.password
122
+ )
123
+ ensure
124
+ the_uri.user = nil
125
+ the_uri.password = nil
126
+ end
127
+
128
+ return the_uri.to_s
129
+ end
130
+
131
+ end
@@ -0,0 +1,77 @@
1
+ module Downspout
2
+
3
+ class Config
4
+ # Default Values
5
+ @@tmp_dir = "/tmp/downloads/"
6
+ @@network_enabled = true
7
+ @@credentials = []
8
+ @@curb_allowed = true
9
+ @@curb_enabled = true
10
+
11
+ def self.tmp_dir
12
+ return @@tmp_dir
13
+ end
14
+
15
+ def self.tmp_dir=( some_path )
16
+ @@tmp_dir = some_path
17
+ end
18
+
19
+ def self.credentials
20
+ return @@credentials
21
+ end
22
+
23
+ def self.network_enabled?
24
+ return @@network_enabled
25
+ end
26
+
27
+ def self.disable_networking!
28
+ @@network_enabled = false
29
+ return !(@@network_enabled)
30
+ end
31
+
32
+ def self.enable_networking!
33
+ @@network_enabled = true
34
+ end
35
+
36
+ def self.curb_available?
37
+ begin
38
+ require 'curb'
39
+ return true
40
+ rescue LoadError
41
+ return false
42
+ end
43
+ end
44
+
45
+ def self.use_curb?
46
+ @@curb_enabled
47
+ end
48
+
49
+ def self.enable_curb!
50
+ if self.curb_available? then
51
+ @@curb_enabled = true
52
+ else
53
+ @@curb_enabled = false
54
+ end
55
+ end
56
+
57
+ def self.disable_curb!
58
+ $logger.debug("downspout | config | disable_curb! | will fall back to Net/HTTP.")
59
+ @@curb_enabled = false
60
+ end
61
+
62
+ def self.add_credential( options = nil )
63
+ return nil unless options && options.respond_to?(:keys)
64
+ options = {:scheme => 'ftp'}.merge!( options )
65
+
66
+ c = Credential.new( options )
67
+
68
+ $logger.debug("downspout | config | add_credential | #{c.host}, #{c.user_name}, #{c.scheme} ")
69
+
70
+ @@credentials << c
71
+
72
+ return c
73
+ end
74
+
75
+ end
76
+
77
+ end
@@ -0,0 +1,20 @@
1
+ module Downspout
2
+
3
+ class Credential
4
+ attr_accessor :scheme
5
+ attr_accessor :host
6
+ attr_accessor :port
7
+ attr_accessor :user_name
8
+ attr_accessor :pass_word
9
+
10
+ def initialize( options = nil )
11
+ if options && options.respond_to?(:keys) then
12
+ options.each do |key, value|
13
+ self.send("#{key}=", value) if self.respond_to?("#{key}=")
14
+ end
15
+ end
16
+ end
17
+
18
+ end
19
+
20
+ end