down 5.2.4 → 5.3.0

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
  SHA256:
3
- metadata.gz: c9fc55f0895c397eb0ebe2096c09962ac64beb208565e0602088bbe77250e6cd
4
- data.tar.gz: e988a2af579a4f728fc1f24f7d32aab4e617b777516c8f2cb7c204606a62b444
3
+ metadata.gz: eab7260ae741338fdb881a861e39cd7c2ed3a66588c52c4e18aed72c30db765a
4
+ data.tar.gz: 10d5e380f74c6cc99abc6b7be22cdf5a6d34e3641231242376df5a4cffc6fbcc
5
5
  SHA512:
6
- metadata.gz: 4a64b2b56f04640bf9255ae20bec88cc4f65f6a862d040197383972240679fdcf1a0434101cc91d0482f2ad4b3dc0356a3c41d1315ff5673aa4fc73fb168380e
7
- data.tar.gz: e967d81189426102f33c10a1d7105f1c4df436efedf83f3288890ccc8b75cb85506589ab2a6c895f500467fa1103c7ac678c701e76c3d8722239fc999eb0f850
6
+ metadata.gz: 0bbd0074ffeba8a93ab19a26d8a28e2761797d01ac348820a9b1342532daae12b9023de78bea2a20108183da0152a964e8077629653567be15e0d152dccbf7af
7
+ data.tar.gz: c3084685dc5e5115cf8843002ed5a656a89a850747a0f0f767aca99a88abe2b13a3f11cb99706f18721eaaa8cde468f47662028e36ebc4bf159a1395e4edda99
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 5.3.0 (2022-02-20)
2
+
3
+ * Add `:extension` argument to `Down.download` for overriding tempfile extension (@razum2um)
4
+
5
+ * Normalize response header names for http.rb and wget backends (@zarqman)
6
+
1
7
  ## 5.2.4 (2021-09-12)
2
8
 
3
9
  * Keep original cookies between redirections (@antprt)
data/README.md CHANGED
@@ -63,6 +63,13 @@ Down.download("http://example.com/image.jpg", destination: "/path/to/destination
63
63
  In this case `Down.download` won't have any return value, so if you need a File
64
64
  object you'll have to create it manually.
65
65
 
66
+ You can also keep the tempfile, but override the extension:
67
+
68
+ ```rb
69
+ tempfile = Down.download("http://example.com/some/file", extension: "txt")
70
+ File.extname(tempfile.path) #=> ".txt"
71
+ ```
72
+
66
73
  ### Basic authentication
67
74
 
68
75
  `Down.download` and `Down.open` will automatically detect and apply HTTP basic
@@ -157,7 +164,7 @@ You can access the response status and headers of the HTTP request that was made
157
164
  ```rb
158
165
  remote_file = Down.open("http://example.com/image.jpg")
159
166
  remote_file.data[:status] #=> 200
160
- remote_file.data[:headers] #=> { ... }
167
+ remote_file.data[:headers] #=> { "Content-Type" => "image/jpeg", ... } (header names are normalized)
161
168
  remote_file.data[:response] # returns the response object
162
169
  ```
163
170
 
data/lib/down/backend.rb CHANGED
@@ -29,5 +29,12 @@ module Down
29
29
 
30
30
  nil
31
31
  end
32
+
33
+ def normalize_headers(response_headers)
34
+ response_headers.inject({}) do |headers, (downcased_name, value)|
35
+ name = downcased_name.split("-").map(&:capitalize).join("-")
36
+ headers.merge!(name => value)
37
+ end
38
+ end
32
39
  end
33
40
  end
data/lib/down/http.rb CHANGED
@@ -25,7 +25,7 @@ module Down
25
25
 
26
26
  # Downlods the remote file to disk. Accepts HTTP.rb options via a hash or a
27
27
  # block, and some additional options as well.
28
- def download(url, max_size: nil, progress_proc: nil, content_length_proc: nil, destination: nil, **options, &block)
28
+ def download(url, max_size: nil, progress_proc: nil, content_length_proc: nil, destination: nil, extension: nil, **options, &block)
29
29
  response = request(url, **options, &block)
30
30
 
31
31
  content_length_proc.call(response.content_length) if content_length_proc && response.content_length
@@ -34,7 +34,7 @@ module Down
34
34
  raise Down::TooLarge, "file is too large (#{response.content_length/1024/1024}MB, max is #{max_size/1024/1024}MB)"
35
35
  end
36
36
 
37
- extname = File.extname(response.uri.path)
37
+ extname = extension ? ".#{extension}" : File.extname(response.uri.path)
38
38
  tempfile = Tempfile.new(["down-http", extname], binmode: true)
39
39
 
40
40
  stream_body(response) do |chunk|
@@ -71,7 +71,11 @@ module Down
71
71
  size: response.content_length,
72
72
  encoding: response.content_type.charset,
73
73
  rewindable: rewindable,
74
- data: { status: response.code, headers: response.headers.to_h, response: response },
74
+ data: {
75
+ status: response.code,
76
+ headers: normalize_headers(response.headers.to_h),
77
+ response: response
78
+ },
75
79
  )
76
80
  end
77
81
 
data/lib/down/net_http.rb CHANGED
@@ -40,6 +40,7 @@ module Down
40
40
  destination = options.delete(:destination)
41
41
  headers = options.delete(:headers)
42
42
  uri_normalizer = options.delete(:uri_normalizer)
43
+ extension = options.delete(:extension)
43
44
 
44
45
  # Use open-uri's :content_lenth_proc or :progress_proc to raise an
45
46
  # exception early if the file is too large.
@@ -93,7 +94,8 @@ module Down
93
94
  open_uri_file = open_uri(uri, open_uri_options, follows_remaining: max_redirects)
94
95
 
95
96
  # Handle the fact that open-uri returns StringIOs for small files.
96
- tempfile = ensure_tempfile(open_uri_file, File.extname(open_uri_file.base_uri.path))
97
+ extname = extension ? ".#{extension}" : File.extname(open_uri_file.base_uri.path)
98
+ tempfile = ensure_tempfile(open_uri_file, extname)
97
99
  OpenURI::Meta.init tempfile, open_uri_file # add back open-uri methods
98
100
  tempfile.extend Down::NetHttp::DownloadedFile
99
101
 
@@ -130,10 +132,7 @@ module Down
130
132
  on_close: -> { request.resume }, # close HTTP connnection
131
133
  data: {
132
134
  status: response.code.to_i,
133
- headers: response.each_header.inject({}) { |headers, (downcased_name, value)|
134
- name = downcased_name.split("-").map(&:capitalize).join("-")
135
- headers.merge!(name => value)
136
- },
135
+ headers: normalize_headers(response.each_header),
137
136
  response: response,
138
137
  },
139
138
  )
data/lib/down/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen-string-literal: true
2
2
 
3
3
  module Down
4
- VERSION = "5.2.4"
4
+ VERSION = "5.3.0"
5
5
  end
data/lib/down/wget.rb CHANGED
@@ -29,7 +29,7 @@ module Down
29
29
 
30
30
  # Downlods the remote file to disk. Accepts wget command-line options and
31
31
  # some additional options as well.
32
- def download(url, *args, max_size: nil, content_length_proc: nil, progress_proc: nil, destination: nil, **options)
32
+ def download(url, *args, max_size: nil, content_length_proc: nil, progress_proc: nil, destination: nil, extension: nil, **options)
33
33
  io = open(url, *args, **options, rewindable: false)
34
34
 
35
35
  content_length_proc.call(io.size) if content_length_proc && io.size
@@ -38,7 +38,7 @@ module Down
38
38
  raise Down::TooLarge, "file is too large (#{io.size/1024/1024}MB, max is #{max_size/1024/1024}MB)"
39
39
  end
40
40
 
41
- extname = File.extname(URI(url).path)
41
+ extname = extension ? ".#{extension}" : File.extname(URI(url).path)
42
42
  tempfile = Tempfile.new(["down-wget", extname], binmode: true)
43
43
 
44
44
  until io.eof?
@@ -94,7 +94,7 @@ module Down
94
94
  raise Down::Error, "failed to parse response headers"
95
95
  end
96
96
 
97
- headers = parser.headers
97
+ headers = normalize_headers(parser.headers)
98
98
  status = parser.status_code
99
99
 
100
100
  content_length = headers["Content-Length"].to_i if headers["Content-Length"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: down
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.4
4
+ version: 5.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-12 00:00:00.000000000 Z
11
+ date: 2022-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable
@@ -175,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
175
  - !ruby/object:Gem::Version
176
176
  version: '0'
177
177
  requirements: []
178
- rubygems_version: 3.2.15
178
+ rubygems_version: 3.3.3
179
179
  signing_key:
180
180
  specification_version: 4
181
181
  summary: Robust streaming downloads using Net::HTTP, HTTP.rb or wget.