mechanize-downloader 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem 'mechanize' ,">= 1.0.0"
6
+ gem 'progressbar' ,">= 0.9.0"
7
+ gem 'httpclient', '>= 2.1.6.1'
8
+
9
+ # Add dependencies to develop your gem here.
10
+ # Include everything needed to run rake, tests, features, etc.
11
+ group :development do
12
+ gem "shoulda", ">= 0"
13
+ gem "bundler", "~> 1.0.0"
14
+ gem "jeweler", "~> 1.5.2"
15
+ gem "rcov", ">= 0"
16
+ end
@@ -0,0 +1,28 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ httpclient (2.1.6.1)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ mechanize (1.0.0)
11
+ nokogiri (>= 1.2.1)
12
+ nokogiri (1.4.4)
13
+ progressbar (0.9.0)
14
+ rake (0.8.7)
15
+ rcov (0.9.9)
16
+ shoulda (2.11.3)
17
+
18
+ PLATFORMS
19
+ ruby
20
+
21
+ DEPENDENCIES
22
+ bundler (~> 1.0.0)
23
+ httpclient (>= 2.1.6.1)
24
+ jeweler (~> 1.5.2)
25
+ mechanize (>= 1.0.0)
26
+ progressbar (>= 0.9.0)
27
+ rcov
28
+ shoulda
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Yoshihiro TAKAHARA
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,26 @@
1
+ = mechanize-downloader
2
+
3
+ This is a mechanize plugin of donwload files wtih progress bar.
4
+
5
+ == Usage
6
+
7
+ Mechanize.new {|agent|
8
+ agent.download(URL,PATH or IO)
9
+ }
10
+
11
+
12
+ == Contributing to mechanize-downloader
13
+
14
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
15
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
16
+ * Fork the project
17
+ * Start a feature/bugfix branch
18
+ * Commit and push until you are happy with your contribution
19
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
20
+ * 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.
21
+
22
+ == Copyright
23
+
24
+ Copyright (c) 2011 Yoshihiro TAKAHARA. See LICENSE.txt for
25
+ further details.
26
+
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "mechanize-downloader"
16
+ gem.homepage = "http://github.com/tumf/mechanize-downloader"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Mechanize helper: Download files with progress bar}
19
+ gem.description = %Q{This is a mechanize plugin of donwload files wtih progress bar.}
20
+ gem.email = "y.takahara@gmail.com"
21
+ gem.authors = ["Yoshihiro TAKAHARA"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'mechanize', '>= 1.0.0'
25
+ # gem.add_runtime_dependency 'progressbar', '>= 0.9.0'
26
+ # gem.add_runtime_dependency 'httpclient', '>= 2.1.6.1'
27
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
28
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
29
+ end
30
+ Jeweler::RubygemsDotOrgTasks.new
31
+
32
+ require 'rake/testtask'
33
+ Rake::TestTask.new(:test) do |test|
34
+ test.libs << 'lib' << 'test'
35
+ test.pattern = 'test/**/test_*.rb'
36
+ test.verbose = true
37
+ end
38
+
39
+ require 'rcov/rcovtask'
40
+ Rcov::RcovTask.new do |test|
41
+ test.libs << 'test'
42
+ test.pattern = 'test/**/test_*.rb'
43
+ test.verbose = true
44
+ end
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "mechanize-downloader #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ =begin
4
+ agent = Mechanize.new
5
+ agent.download("","")
6
+ =end
7
+
8
+ require 'uri'
9
+ require 'net/http'
10
+
11
+ require "rubygems"
12
+ require 'httpclient'
13
+ require 'progressbar'
14
+ require 'mechanize'
15
+
16
+ class Mechanize
17
+ def download(url,file)
18
+ url = URI.parse(url)
19
+ cli = HTTPClient.new
20
+ @cookie_jar.cookies(url).each do |cookie|
21
+ cli.cookie_manager.parse(cookie.to_s,url)
22
+ end
23
+
24
+ length = 0;total = 0
25
+ while true
26
+ res = cli.head(url)
27
+ break unless res.status == 302 # HTTP::HTTPFound
28
+ url = URI.parse(res.header["Location"].to_s)
29
+ end
30
+
31
+ total = cli.head(url).header["Content-Length"].to_s.to_i
32
+ t = Thread.new {
33
+ conn = cli.get_async(url)
34
+ io = conn.pop.content
35
+
36
+ f = file
37
+ f = ::File::open(file, "wb") unless file.is_a?(IO) or file.is_a?(Tempfile)
38
+ while str = io.read(40)
39
+ f.write str
40
+ length += str.length
41
+ end
42
+ f.close unless (file.is_a?(IO) or file.is_a?(Tempfile))
43
+ }
44
+
45
+ pbar = ProgressBar.new("Loading",total)
46
+ while total > length
47
+ sleep 1
48
+ pbar.set(length)
49
+ end
50
+ pbar.finish
51
+ t.join
52
+ end
53
+ end
@@ -0,0 +1,71 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mechanize-downloader}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Yoshihiro TAKAHARA"]
12
+ s.date = %q{2011-01-22}
13
+ s.description = %q{This is a mechanize plugin of donwload files wtih progress bar.}
14
+ s.email = %q{y.takahara@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE.txt",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/mechanize-downloader.rb",
27
+ "mechanize-downloader.gemspec",
28
+ "test/helper.rb",
29
+ "test/test_mechanize-downloader.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/tumf/mechanize-downloader}
32
+ s.licenses = ["MIT"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.4.2}
35
+ s.summary = %q{Mechanize helper: Download files with progress bar}
36
+ s.test_files = [
37
+ "test/helper.rb",
38
+ "test/test_mechanize-downloader.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<mechanize>, [">= 1.0.0"])
46
+ s.add_runtime_dependency(%q<progressbar>, [">= 0.9.0"])
47
+ s.add_runtime_dependency(%q<httpclient>, [">= 2.1.6.1"])
48
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
49
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
50
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
51
+ s.add_development_dependency(%q<rcov>, [">= 0"])
52
+ else
53
+ s.add_dependency(%q<mechanize>, [">= 1.0.0"])
54
+ s.add_dependency(%q<progressbar>, [">= 0.9.0"])
55
+ s.add_dependency(%q<httpclient>, [">= 2.1.6.1"])
56
+ s.add_dependency(%q<shoulda>, [">= 0"])
57
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
58
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
59
+ s.add_dependency(%q<rcov>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<mechanize>, [">= 1.0.0"])
63
+ s.add_dependency(%q<progressbar>, [">= 0.9.0"])
64
+ s.add_dependency(%q<httpclient>, [">= 2.1.6.1"])
65
+ s.add_dependency(%q<shoulda>, [">= 0"])
66
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
67
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
68
+ s.add_dependency(%q<rcov>, [">= 0"])
69
+ end
70
+ end
71
+
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'mechanize-downloader'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,34 @@
1
+ require 'helper'
2
+ require 'tempfile'
3
+
4
+ class TestMechanizeDownloader < Test::Unit::TestCase
5
+ context "download from remote" do
6
+ should "download Dino logo file to path" do
7
+ url = "http://www.dino.co.jp/images/www/index_logo.gif"
8
+ Tempfile.open("logo"){|f|
9
+ Mechanize.new {|agent|
10
+ agent.download(url,f.path)
11
+ }
12
+ assert_equal(f.size,4133,"dino logo size is 4,133 bytes")
13
+ }
14
+ end
15
+ should "download Dino logo file from to Tempfile" do
16
+ url = "http://www.dino.co.jp/images/www/index_logo.gif"
17
+ Tempfile.open("logo"){|f|
18
+ Mechanize.new {|agent|
19
+ agent.download(url,f)
20
+ }
21
+ assert_equal(f.size,4133,"dino logo size is 4,133 bytes")
22
+ }
23
+ end
24
+ should "download Dino logo file from to IO" do
25
+ url = "http://www.dino.co.jp/images/www/index_logo.gif"
26
+ Tempfile.open("logo"){|f|
27
+ Mechanize.new {|agent|
28
+ agent.download(url,f.to_io)
29
+ }
30
+ assert_equal(f.size,4133,"dino logo size is 4,133 bytes")
31
+ }
32
+ end
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mechanize-downloader
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Yoshihiro TAKAHARA
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-22 00:00:00 +09:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ type: :runtime
23
+ name: mechanize
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ version: 1.0.0
35
+ prerelease: false
36
+ requirement: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ type: :runtime
39
+ name: progressbar
40
+ version_requirements: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 59
46
+ segments:
47
+ - 0
48
+ - 9
49
+ - 0
50
+ version: 0.9.0
51
+ prerelease: false
52
+ requirement: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ type: :runtime
55
+ name: httpclient
56
+ version_requirements: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 125
62
+ segments:
63
+ - 2
64
+ - 1
65
+ - 6
66
+ - 1
67
+ version: 2.1.6.1
68
+ prerelease: false
69
+ requirement: *id003
70
+ - !ruby/object:Gem::Dependency
71
+ type: :development
72
+ name: shoulda
73
+ version_requirements: &id004 !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ hash: 3
79
+ segments:
80
+ - 0
81
+ version: "0"
82
+ prerelease: false
83
+ requirement: *id004
84
+ - !ruby/object:Gem::Dependency
85
+ type: :development
86
+ name: bundler
87
+ version_requirements: &id005 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ~>
91
+ - !ruby/object:Gem::Version
92
+ hash: 23
93
+ segments:
94
+ - 1
95
+ - 0
96
+ - 0
97
+ version: 1.0.0
98
+ prerelease: false
99
+ requirement: *id005
100
+ - !ruby/object:Gem::Dependency
101
+ type: :development
102
+ name: jeweler
103
+ version_requirements: &id006 !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ~>
107
+ - !ruby/object:Gem::Version
108
+ hash: 7
109
+ segments:
110
+ - 1
111
+ - 5
112
+ - 2
113
+ version: 1.5.2
114
+ prerelease: false
115
+ requirement: *id006
116
+ - !ruby/object:Gem::Dependency
117
+ type: :development
118
+ name: rcov
119
+ version_requirements: &id007 !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ hash: 3
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ prerelease: false
129
+ requirement: *id007
130
+ description: This is a mechanize plugin of donwload files wtih progress bar.
131
+ email: y.takahara@gmail.com
132
+ executables: []
133
+
134
+ extensions: []
135
+
136
+ extra_rdoc_files:
137
+ - LICENSE.txt
138
+ - README.rdoc
139
+ files:
140
+ - Gemfile
141
+ - Gemfile.lock
142
+ - LICENSE.txt
143
+ - README.rdoc
144
+ - Rakefile
145
+ - VERSION
146
+ - lib/mechanize-downloader.rb
147
+ - mechanize-downloader.gemspec
148
+ - test/helper.rb
149
+ - test/test_mechanize-downloader.rb
150
+ has_rdoc: true
151
+ homepage: http://github.com/tumf/mechanize-downloader
152
+ licenses:
153
+ - MIT
154
+ post_install_message:
155
+ rdoc_options: []
156
+
157
+ require_paths:
158
+ - lib
159
+ required_ruby_version: !ruby/object:Gem::Requirement
160
+ none: false
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ hash: 3
165
+ segments:
166
+ - 0
167
+ version: "0"
168
+ required_rubygems_version: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ hash: 3
174
+ segments:
175
+ - 0
176
+ version: "0"
177
+ requirements: []
178
+
179
+ rubyforge_project:
180
+ rubygems_version: 1.4.2
181
+ signing_key:
182
+ specification_version: 3
183
+ summary: "Mechanize helper: Download files with progress bar"
184
+ test_files:
185
+ - test/helper.rb
186
+ - test/test_mechanize-downloader.rb