downloadr 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/downloadr.gemspec +4 -1
- data/lib/downloadr.rb +2 -1
- data/lib/downloadr/exception.rb +10 -0
- data/lib/downloadr/http.rb +52 -20
- data/lib/downloadr/version.rb +1 -1
- data/spec/downloadr/http_spec.rb +97 -14
- metadata +31 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a80035d5c1821c148bd9895ec7aeb4944d0f480
|
4
|
+
data.tar.gz: dc62ba3d63f0763bfef7b0980b0ccac4d4b035c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c9947310faa5671f25816ccc4fad372ae526363d672c8a96efea6d17fab47db0804dd960178435451608e2ab376bae7f0031e51ce305ca4721210dc352d981f
|
7
|
+
data.tar.gz: 64556525afd68810858bb952dd585739f1d26a32b158844d3a1e399bf371834704b2ea3d648e9eed572d88e9046b4e3d98a9b8126e8aeaa74f93d4407c14eee6
|
data/.travis.yml
CHANGED
data/downloadr.gemspec
CHANGED
@@ -18,7 +18,10 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.
|
21
|
+
spec.add_dependency 'rest-client', '~> 1.7'
|
22
|
+
spec.add_dependency 'addressable', '~> 2.3'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
25
|
spec.add_development_dependency 'rake', '~> 0'
|
23
26
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
24
27
|
end
|
data/lib/downloadr.rb
CHANGED
data/lib/downloadr/http.rb
CHANGED
@@ -1,25 +1,57 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'rest_client'
|
2
|
+
require 'addressable/uri'
|
3
|
+
|
4
|
+
# HTTP/HTTPS Transport handler for downloading a file
|
3
5
|
|
4
6
|
module Downloadr
|
5
|
-
# HTTP/HTTPS Transport handler for downloading a file
|
6
7
|
class HTTP
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
8
|
+
attr_reader :uri, :path
|
9
|
+
|
10
|
+
# @param [String] uri
|
11
|
+
# @param [String] download_path
|
12
|
+
def initialize(uri, download_path = nil)
|
13
|
+
@uri = ::Addressable::URI.parse(uri)
|
14
|
+
@path = normalize_path(download_path)
|
15
|
+
end
|
16
|
+
|
17
|
+
def download
|
18
|
+
begin
|
19
|
+
response = ::RestClient::Request.execute(
|
20
|
+
:method => :get,
|
21
|
+
:url => @uri.to_s,
|
22
|
+
:timeout => 100,
|
23
|
+
:open_timeout => 10
|
24
|
+
)
|
25
|
+
|
26
|
+
rescue ::SocketError
|
27
|
+
raise Downloadr::SocketError
|
28
|
+
rescue RestClient::ResourceNotFound
|
29
|
+
raise Downloadr::ResourceNotFound
|
30
|
+
end
|
31
|
+
|
32
|
+
File.write(@path, response.to_str)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.download(uri, download_path = nil)
|
36
|
+
downloader = Downloadr::HTTP.new(uri, download_path)
|
37
|
+
downloader.download
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def normalize_path(download_path)
|
42
|
+
if download_path.nil?
|
43
|
+
if @uri.basename.empty? ||
|
44
|
+
@uri.basename.nil?
|
45
|
+
raise Downloadr::UnknownDownloadPath
|
46
|
+
else
|
47
|
+
path = @uri.basename
|
48
|
+
end
|
49
|
+
else
|
50
|
+
path = download_path
|
51
|
+
end
|
52
|
+
|
53
|
+
return path
|
54
|
+
end
|
55
|
+
|
24
56
|
end
|
25
57
|
end
|
data/lib/downloadr/version.rb
CHANGED
data/spec/downloadr/http_spec.rb
CHANGED
@@ -5,29 +5,63 @@ module Downloader
|
|
5
5
|
describe "HTTP" do
|
6
6
|
context "when initializing Downloadr::HTTP" do
|
7
7
|
before :each do
|
8
|
-
@
|
8
|
+
@download_path = Tempfile.new('downloadr')
|
9
9
|
@uri = "http://www.google.com/index.html"
|
10
|
-
@http_downloadr = Downloadr::HTTP.new(@uri, @
|
10
|
+
@http_downloadr = Downloadr::HTTP.new(@uri, @download_path)
|
11
11
|
end
|
12
12
|
|
13
13
|
subject{@http_downloadr}
|
14
14
|
|
15
|
-
it {
|
15
|
+
it {should be_kind_of Downloadr::HTTP}
|
16
16
|
|
17
17
|
it "should have the right uri" do
|
18
|
-
expect(subject.uri).to
|
18
|
+
expect(subject.uri).to eql(::Addressable::URI.parse(@uri))
|
19
19
|
end
|
20
20
|
|
21
21
|
it "should have the right file path" do
|
22
|
-
expect(subject.path).to be(@
|
22
|
+
expect(subject.path).to be(@download_path)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
context "when initializing Downloadr::HTTP w/o a download path" do
|
27
|
+
before :each do
|
28
|
+
@uri = "http://www.google.com/index.html"
|
29
|
+
@http_downloadr = Downloadr::HTTP.new(@uri)
|
30
|
+
end
|
31
|
+
|
32
|
+
subject{@http_downloadr}
|
33
|
+
|
34
|
+
it {should be_kind_of Downloadr::HTTP}
|
35
|
+
|
36
|
+
it "should have the right uri" do
|
37
|
+
expect(subject.uri).to eql(::Addressable::URI.parse(@uri))
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should have the right file path" do
|
41
|
+
expect(subject.path).to eql("index.html")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "when initializing Downloadr::HTTP w/o a download path or uri filename" do
|
46
|
+
before :each do
|
47
|
+
@uri = "http://www.google.com"
|
48
|
+
end
|
49
|
+
|
50
|
+
subject{@http_downloadr}
|
51
|
+
|
52
|
+
it "should have the right uri" do
|
53
|
+
expect {
|
54
|
+
Downloadr::HTTP.new(@uri)
|
55
|
+
}.to raise_error(Downloadr::UnknownDownloadPath)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
26
60
|
context "when downloading a file via HTTP" do
|
27
61
|
before :each do
|
28
|
-
@
|
62
|
+
@download_path = Tempfile.new('downloadr')
|
29
63
|
@uri = "http://www.google.com/index.html"
|
30
|
-
@http_downloadr = Downloadr::HTTP.new(@uri, @
|
64
|
+
@http_downloadr = Downloadr::HTTP.new(@uri, @download_path)
|
31
65
|
end
|
32
66
|
|
33
67
|
subject{@http_downloadr}
|
@@ -53,27 +87,76 @@ module Downloader
|
|
53
87
|
|
54
88
|
context "when downloading via self.download" do
|
55
89
|
before :each do
|
56
|
-
@
|
90
|
+
@download_path = Tempfile.new('downloadr')
|
57
91
|
@uri = "http://www.google.com/index.html"
|
58
92
|
end
|
59
93
|
|
60
94
|
it "should start with an empty file" do
|
61
|
-
expect(@
|
95
|
+
expect(@download_path.size).to be(0)
|
62
96
|
end
|
63
97
|
|
64
98
|
it "should return a Fixnum when being downloaded" do
|
65
|
-
expect(Downloadr::HTTP.download(@uri, @
|
99
|
+
expect(Downloadr::HTTP.download(@uri, @download_path)).to be_kind_of ::Fixnum
|
66
100
|
end
|
67
101
|
|
68
102
|
it "should have created a file on disk" do
|
69
|
-
Downloadr::HTTP.download(@uri, @
|
70
|
-
expect(@
|
103
|
+
Downloadr::HTTP.download(@uri, @download_path)
|
104
|
+
expect(@download_path.size).to be > 0
|
71
105
|
end
|
72
106
|
|
73
107
|
it "should have content that looks like google" do
|
74
|
-
Downloadr::HTTP.download(@uri, @
|
75
|
-
expect(File.read(@
|
108
|
+
Downloadr::HTTP.download(@uri, @download_path)
|
109
|
+
expect(File.read(@download_path)).to include("google")
|
76
110
|
end
|
77
111
|
end
|
112
|
+
|
113
|
+
context "when attempting to download from a uri that returns a 404" do
|
114
|
+
before :each do
|
115
|
+
@download_path = Tempfile.new('downloadr')
|
116
|
+
@uri = "http://www.google.com/this_file_does_not_exist"
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should start with an empty file" do
|
120
|
+
expect(@download_path.size).to be(0)
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should throw an exception when attempting to download" do
|
124
|
+
expect {
|
125
|
+
Downloadr::HTTP.download(@uri, @download_path)
|
126
|
+
}.to raise_error(Downloadr::ResourceNotFound)
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should not have created a file on disk" do
|
130
|
+
expect {
|
131
|
+
Downloadr::HTTP.download(@uri, @download_path)
|
132
|
+
}.to raise_error(Downloadr::ResourceNotFound)
|
133
|
+
expect(@download_path.size).to be(0)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
context "when attempting to download from a uri that doesn't resolve" do
|
138
|
+
before :each do
|
139
|
+
@download_path = Tempfile.new('downloadr')
|
140
|
+
@uri = "http://abc.local/this_server_does_not_exist"
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should start with an empty file" do
|
144
|
+
expect(@download_path.size).to be(0)
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should throw an exception when attempting to download" do
|
148
|
+
expect {
|
149
|
+
Downloadr::HTTP.download(@uri, @download_path)
|
150
|
+
}.to raise_error(Downloadr::SocketError)
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should not have created a file on disk" do
|
154
|
+
expect {
|
155
|
+
Downloadr::HTTP.download(@uri, @download_path)
|
156
|
+
}.to raise_error(Downloadr::SocketError)
|
157
|
+
expect(@download_path.size).to be(0)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
78
161
|
end
|
79
162
|
end
|
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: downloadr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Claudius
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :runtime
|
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: addressable
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.3'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.3'
|
13
41
|
- !ruby/object:Gem::Dependency
|
14
42
|
name: bundler
|
15
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -70,6 +98,7 @@ files:
|
|
70
98
|
- Rakefile
|
71
99
|
- downloadr.gemspec
|
72
100
|
- lib/downloadr.rb
|
101
|
+
- lib/downloadr/exception.rb
|
73
102
|
- lib/downloadr/http.rb
|
74
103
|
- lib/downloadr/version.rb
|
75
104
|
- spec/downloadr/http_spec.rb
|