downspout 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +0 -15
- data/VERSION +1 -1
- data/lib/downspout/downloader.rb +34 -36
- data/test/fixtures/faux_headers.txt +10 -0
- data/test/unit/downloader_test.rb +25 -1
- metadata +4 -3
data/Rakefile
CHANGED
@@ -51,18 +51,3 @@ Rake::TestTask.new(:test) do |test|
|
|
51
51
|
end
|
52
52
|
|
53
53
|
task :default => :test
|
54
|
-
|
55
|
-
namespace :basic do
|
56
|
-
desc "Basic Steps to take when updating this gem via GemCutter"
|
57
|
-
task :steps do
|
58
|
-
puts "rake version:bump:patch"
|
59
|
-
puts "rake gemspec"
|
60
|
-
puts ">>> write code <<<"
|
61
|
-
puts "rake test"
|
62
|
-
puts "rake build"
|
63
|
-
puts "gem install pkg/downspout-{version}.gem"
|
64
|
-
puts ">>> manual tests <<<"
|
65
|
-
puts "git commit"
|
66
|
-
puts "gem push pkg/downspout-{version}.gem"
|
67
|
-
end
|
68
|
-
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.5
|
data/lib/downspout/downloader.rb
CHANGED
@@ -171,9 +171,7 @@ module Downspout
|
|
171
171
|
ftp = Net::FTP.open( @uri.host ) do |ftp|
|
172
172
|
ftp.login( cred.user_name, cred.pass_word ) unless cred.nil?
|
173
173
|
ftp.passive
|
174
|
-
ftp.chdir( File.dirname( @uri.path ) )
|
175
|
-
|
176
|
-
$logger.debug("downspout | downloader | net_ftp_download | Local Path : #{@path} ...")
|
174
|
+
ftp.chdir( File.dirname( @uri.path ) )
|
177
175
|
ftp.getbinaryfile( self.basename, @path )
|
178
176
|
end
|
179
177
|
rescue Exception => e
|
@@ -181,10 +179,12 @@ module Downspout
|
|
181
179
|
raise e
|
182
180
|
end
|
183
181
|
|
184
|
-
|
185
|
-
|
182
|
+
if !(File.exist?( @path )) then
|
183
|
+
$logger.error("downspout | downloader | net_ftp_download | #{basename} download failed.")
|
184
|
+
return false
|
185
|
+
end
|
186
186
|
|
187
|
-
return
|
187
|
+
return true
|
188
188
|
end
|
189
189
|
|
190
190
|
def net_http_download
|
@@ -193,12 +193,8 @@ module Downspout
|
|
193
193
|
begin
|
194
194
|
response = net_http_fetch( @url , 1)
|
195
195
|
open( @path, "wb" ) do |file|
|
196
|
-
|
197
196
|
file.write(response.body)
|
198
197
|
end
|
199
|
-
|
200
|
-
$logger.debug("downspout | downloader | net_http_download | Response Body : #{response.body[0..5].strip}")
|
201
|
-
|
202
198
|
rescue SocketError => dns_err
|
203
199
|
$logger.error("downspout | downloader | net_http_download | Net/HTTP DNS Error | #{@uri.host} | #{dns_err.inspect}")
|
204
200
|
remove_file_at_target_path
|
@@ -212,23 +208,19 @@ module Downspout
|
|
212
208
|
@response.each_header do |k,v|
|
213
209
|
new_header_str += "#{k}: #{v}\r\n"
|
214
210
|
end
|
215
|
-
|
216
|
-
|
211
|
+
parse_headers_from_string!( new_header_str )
|
217
212
|
|
218
213
|
if ((response.code.to_i != 200) and (response.code.to_i != 202)) then
|
219
214
|
# missing file, failed download - delete the response body [if downloaded]
|
220
215
|
remove_file_at_target_path
|
221
216
|
return false
|
222
217
|
end
|
223
|
-
|
224
|
-
$logger.debug("downspout | downloader | net_http_download | Headers : #{response.header}")
|
225
218
|
|
226
219
|
if !( File.exist?( @path ) ) then
|
227
220
|
$logger.error("downspout | downloader | net_http_download | Missing File at download path : #{@path}")
|
228
221
|
return false
|
229
222
|
end
|
230
223
|
|
231
|
-
$logger.debug("downspout | downloader | net_http_download | Successful.")
|
232
224
|
return true
|
233
225
|
end
|
234
226
|
|
@@ -244,19 +236,17 @@ module Downspout
|
|
244
236
|
# TODO : implement credentials for downloads via net_http_fetch
|
245
237
|
my_request.basic_auth 'account', 'p4ssw0rd'
|
246
238
|
|
247
|
-
$logger.debug("downspout | downloader | net_http_fetch | Firing...")
|
248
239
|
@response = Net::HTTP.start( u.host, u.port ) do |http|
|
249
240
|
http.request( my_request )
|
250
241
|
end
|
251
242
|
|
252
|
-
$logger.debug("downspout | downloader | net_http_fetch | Response : #{@response}")
|
253
|
-
|
254
243
|
case @response
|
255
244
|
when Net::HTTPSuccess
|
256
245
|
@response
|
257
246
|
when Net::HTTPRedirection
|
258
247
|
net_http_fetch( @response['location'], limit - 1 )
|
259
248
|
else
|
249
|
+
$logger.error("downspout | downloader | net_http_fetch | Response : #{@response}")
|
260
250
|
@response.error!
|
261
251
|
end
|
262
252
|
end
|
@@ -278,10 +268,8 @@ module Downspout
|
|
278
268
|
remove_file_at_target_path
|
279
269
|
end
|
280
270
|
|
281
|
-
$logger.debug("downspout | downloader | curb_http_download | Headers : #{curb.header_str}")
|
282
|
-
|
283
271
|
# populate the response headers from curb header string
|
284
|
-
|
272
|
+
parse_headers_from_string!( curb.header_str )
|
285
273
|
|
286
274
|
# populate a 'proxy' HTTPResponse object with the Curb data...
|
287
275
|
hr_klass = Net::HTTPResponse.send('response_class', curb.response_code.to_s)
|
@@ -291,19 +279,15 @@ module Downspout
|
|
291
279
|
curb.response_code,
|
292
280
|
@response_headers["HTTP"][:message] )
|
293
281
|
|
294
|
-
$logger.debug("downspout | downloader | curb_http_download | Response : #{@response.inspect}")
|
295
|
-
|
296
282
|
if !( File.exist?( @path ) ) then
|
297
283
|
$logger.error("downspout | downloader | curb_http_download | Missing File at download path : #{@path}")
|
298
284
|
return false
|
299
285
|
end
|
300
286
|
|
301
|
-
$logger.debug("downspout | downloader | curb_http_download | Successful.")
|
302
287
|
return true
|
303
288
|
end
|
304
289
|
|
305
|
-
def parse_headers_from_string( header_str )
|
306
|
-
$logger.debug("downspout | downloader | parse_headers_from_string | String : #{header_str}")
|
290
|
+
def parse_headers_from_string!( header_str )
|
307
291
|
header_hash = {}
|
308
292
|
http_hash = {}
|
309
293
|
headers = header_str.split("\r\n")
|
@@ -314,40 +298,54 @@ module Downspout
|
|
314
298
|
http_hash[:code] = (http_info.split("\r\n")[0].split(" ")[1]).to_i
|
315
299
|
http_hash[:message] = http_info.split("\r\n")[0].split(" ")[2]
|
316
300
|
|
317
|
-
$logger.debug("downspout | downloader | parse_headers_from_string | Response : #{http_hash[:version]}, #{http_hash[:code]}, #{http_hash[:message]}")
|
318
301
|
header_hash["HTTP"] = http_hash
|
319
302
|
|
320
303
|
headers[1..-1].each do |line|
|
321
304
|
header_name, header_value = line.match(/([\w\-\s]+)\:\s?(.*)/)[1..2]
|
322
305
|
header_hash[header_name] = header_value
|
323
306
|
end
|
324
|
-
|
325
|
-
|
307
|
+
|
308
|
+
@response_headers = header_hash
|
326
309
|
end
|
327
310
|
|
328
311
|
def generate_file_name
|
312
|
+
result = nil
|
313
|
+
|
314
|
+
result = file_name_from_content_disposition
|
315
|
+
result = file_name_from_content_type if result.nil?
|
316
|
+
|
317
|
+
return result
|
318
|
+
end
|
319
|
+
|
320
|
+
def file_name_from_content_disposition
|
321
|
+
file_name = nil
|
329
322
|
|
330
323
|
cd_key = response_headers.keys.select{|k| k =~ /content-disposition/i }.first
|
331
|
-
|
324
|
+
|
332
325
|
if cd_key then
|
333
326
|
disposition = @response_headers[cd_key]
|
334
327
|
if disposition then
|
335
|
-
|
336
|
-
|
328
|
+
# example : Content-Disposition: attachment; filename="iPad_User_Guide.pdf"
|
329
|
+
file_name = disposition.match("filename=\"?(.+)\"?")[1]
|
337
330
|
end
|
338
331
|
end
|
339
332
|
|
333
|
+
$logger.debug("downspout | downloader | file_name_from_content_disposition | #{file_name}")
|
334
|
+
return file_name
|
335
|
+
end
|
336
|
+
|
337
|
+
def file_name_from_content_type
|
340
338
|
ct_key = response_headers.keys.select{|k| k =~ /content-type/i }.first
|
341
339
|
return nil unless ct_key
|
342
340
|
|
343
341
|
file_type = @response_headers[ct_key]
|
344
342
|
return nil unless file_type
|
345
343
|
|
346
|
-
#
|
347
|
-
|
348
|
-
return "#{@basename || 'default'}.pdf" if file_type =~ /pdf/
|
344
|
+
file_name = "#{@basename || 'default'}.html" if (file_type =~ /html/)
|
345
|
+
file_name = "#{@basename || 'default'}.pdf" if (file_type =~ /pdf/) && file_name.nil?
|
349
346
|
|
350
|
-
|
347
|
+
$logger.debug("downspout | downloader | file_name_from_content_type | #{file_name}")
|
348
|
+
return file_name
|
351
349
|
end
|
352
350
|
|
353
351
|
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
HTTP/1.1 200 OK
|
2
|
+
Date: Wed, 30 Mar 2011 19:40:11 GMT
|
3
|
+
Server: Apache/2.2.11 (Unix) DAV/2 PHP/5.3.1
|
4
|
+
Content-Length: 1181801
|
5
|
+
Content-Disposition: attachement; filename=0123456789_X.pdf
|
6
|
+
X-HP-CAM-COLOR: V=1;ServerAddr=wbO8IqKJzqoYjXdW+68u4A==;GUID=1|0-iccMJzdDzi5kYvyeH1L5A_QSAtcJxSoyASpOXx9kY.|L3ByaW1pczY0L2Rvd25sb2FkL2N1c3RvbWJvb2s.
|
7
|
+
X-Powered-By: Servlet/2.5 JSP/2.1
|
8
|
+
Vary: Accept-Encoding,User-Agent
|
9
|
+
Content-Type: application/pdf
|
10
|
+
Connection: close
|
@@ -2,7 +2,7 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class DownloaderTest < Test::Unit::TestCase
|
4
4
|
|
5
|
-
|
5
|
+
context "Downspout" do
|
6
6
|
context "Downloader" do
|
7
7
|
|
8
8
|
should "respond to URL" do
|
@@ -208,4 +208,28 @@ class DownloaderTest < Test::Unit::TestCase
|
|
208
208
|
assert g.response_headers.keys.include?('content-type') # Note case difference for Net/HTTP vs Curb
|
209
209
|
end
|
210
210
|
|
211
|
+
context "with content disposition header defining file name" do
|
212
|
+
setup do
|
213
|
+
@dlx = Downspout::Downloader.new()
|
214
|
+
@faux_headers = File.read( File.join( Test::App.root, "test", "fixtures", "faux_headers.txt" ) )
|
215
|
+
|
216
|
+
@dlx.send("parse_headers_from_string!", @faux_headers )
|
217
|
+
end
|
218
|
+
|
219
|
+
should "have expected keys parsed from headers" do
|
220
|
+
all_keys = @dlx.response_headers.keys
|
221
|
+
assert_equal 10, all_keys.size
|
222
|
+
end
|
223
|
+
|
224
|
+
should "have a key for the content dispositon header" do
|
225
|
+
all_keys = @dlx.response_headers.keys
|
226
|
+
the_key = all_keys.select{|k| k =~ /content-disposition/i }.first
|
227
|
+
assert the_key
|
228
|
+
end
|
229
|
+
|
230
|
+
should "extract the appropriate file name" do
|
231
|
+
assert_equal "0123456789_X.pdf", @dlx.send("generate_file_name")
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
211
235
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: downspout
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 29
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 5
|
10
|
+
version: 0.2.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Phi.Sanders
|
@@ -120,6 +120,7 @@ files:
|
|
120
120
|
- lib/downspout/logger.rb
|
121
121
|
- lib/downspout/tmp_file.rb
|
122
122
|
- test/downspout_test.rb
|
123
|
+
- test/fixtures/faux_headers.txt
|
123
124
|
- test/fixtures/ruby.png
|
124
125
|
- test/servlet.rb
|
125
126
|
- test/test_helper.rb
|