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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fb4c356466b4e4e41b1db9a02b10a55c29eab05f
4
- data.tar.gz: e0b65f85469cb03b40fea1ecb9e6ed17b24be785
3
+ metadata.gz: 3a80035d5c1821c148bd9895ec7aeb4944d0f480
4
+ data.tar.gz: dc62ba3d63f0763bfef7b0980b0ccac4d4b035c4
5
5
  SHA512:
6
- metadata.gz: 115d4f880f88188aa63a5c796fd53d3c01d1fc7eb1aea07c76231ec3de15f3129eb2792c96a2aef052a6e24eb6015543b2c95de13d98d5dd53d0f09b4e81f047
7
- data.tar.gz: ade2a68e5afafe4feae374c38690a2bd60c96c5e329413e13ed1584a1c675dcdc55e5d2655d156fc39f66575dd575dbeb6aa32d8dee9302824249f94da806ae0
6
+ metadata.gz: 4c9947310faa5671f25816ccc4fad372ae526363d672c8a96efea6d17fab47db0804dd960178435451608e2ab376bae7f0031e51ce305ca4721210dc352d981f
7
+ data.tar.gz: 64556525afd68810858bb952dd585739f1d26a32b158844d3a1e399bf371834704b2ea3d648e9eed572d88e9046b4e3d98a9b8126e8aeaa74f93d4407c14eee6
@@ -1,3 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.1.2
3
4
  - 2.0.0
@@ -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.add_development_dependency 'bundler', "~> 1.6"
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
@@ -1,2 +1,3 @@
1
1
  require "downloadr/version"
2
- require "downloadr/http"
2
+ require "downloadr/http"
3
+ require "downloadr/exception"
@@ -0,0 +1,10 @@
1
+ module Downloadr
2
+ class ResourceNotFound < StandardError
3
+ end
4
+
5
+ class UnknownDownloadPath < StandardError
6
+ end
7
+
8
+ class SocketError < StandardError
9
+ end
10
+ end
@@ -1,25 +1,57 @@
1
- require "net/http"
2
- require "uri"
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
- attr_reader :uri, :path
8
-
9
- # @param [String] uri
10
- # @param [String] path
11
- def initialize(uri, path)
12
- @uri = uri
13
- @path = path
14
- end
15
-
16
- def download
17
- File.write(@path, Net::HTTP.get(URI.parse(@uri)))
18
- end
19
-
20
- def self.download(uri, path)
21
- downloader = Downloadr::HTTP.new(uri, path)
22
- downloader.download
23
- end
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
@@ -1,3 +1,3 @@
1
1
  module Downloadr
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -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
- @file_path = Tempfile.new('downloadr')
8
+ @download_path = Tempfile.new('downloadr')
9
9
  @uri = "http://www.google.com/index.html"
10
- @http_downloadr = Downloadr::HTTP.new(@uri, @file_path)
10
+ @http_downloadr = Downloadr::HTTP.new(@uri, @download_path)
11
11
  end
12
12
 
13
13
  subject{@http_downloadr}
14
14
 
15
- it { should be_kind_of Downloadr::HTTP}
15
+ it {should be_kind_of Downloadr::HTTP}
16
16
 
17
17
  it "should have the right uri" do
18
- expect(subject.uri).to be(@uri)
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(@file_path)
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
- @file_path = Tempfile.new('downloadr')
62
+ @download_path = Tempfile.new('downloadr')
29
63
  @uri = "http://www.google.com/index.html"
30
- @http_downloadr = Downloadr::HTTP.new(@uri, @file_path)
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
- @file_path = Tempfile.new('downloadr')
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(@file_path.size).to be(0)
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, @file_path)).to be_kind_of ::Fixnum
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, @file_path)
70
- expect(@file_path.size).to be > 0
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, @file_path)
75
- expect(File.read(@file_path)).to include("google")
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.3
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-13 00:00:00.000000000 Z
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