down 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +16 -0
- data/down.gemspec +1 -1
- data/lib/down.rb +9 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b92313f399fb288e1c324b1355cd26b3eb02ca00
|
4
|
+
data.tar.gz: bec353dfe70af7fb445b0203ecdd0f20cacc1a29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64b80aa1853dcc253828360b4d8c903c22f4cf0ef4ee4f340b458bc37160852caf6544613476d24fb354b2c103df0bfdaf25b55cdcf5a51a09982ca32b99e949
|
7
|
+
data.tar.gz: a3d520d6b413114981037d589884f9c113bd0bbef0e5f384c0a5acabee15f73ccec76ca17490ecb5b0b1cf272f337562ed06338449d9c943e3a149c2d983d623
|
data/README.md
CHANGED
@@ -23,6 +23,11 @@ returns a `StringIO`. In my application I needed that the file is always
|
|
23
23
|
downloaded to disk. This was obviously a wrong design decision from the MRI
|
24
24
|
team, so Down patches this behaviour and always returns a `Tempfile`.
|
25
25
|
|
26
|
+
### File extension
|
27
|
+
|
28
|
+
When using `open-uri` directly, the extension of the remote file is not
|
29
|
+
preserved. Down patches that behaviour and preserves the file extension.
|
30
|
+
|
26
31
|
### Metadata
|
27
32
|
|
28
33
|
`open-uri` adds some metadata to the returned file, like `#content_type`. Down
|
@@ -86,6 +91,17 @@ You can specify the time after the request will time out:
|
|
86
91
|
Down.download "http://example.com/image.jpg", timeout: 5
|
87
92
|
```
|
88
93
|
|
94
|
+
### Copying to tempfile
|
95
|
+
|
96
|
+
Down has another "hidden" utility method, `#copy_to_tempfile`, which creates
|
97
|
+
a Tempfile out of the given file. The `#download` method uses it internally,
|
98
|
+
but it's also publicly available for direct use:
|
99
|
+
|
100
|
+
```rb
|
101
|
+
tempfile = Down.copy_to_tempfile "path/to/file.jpg"
|
102
|
+
tempfile.path #=> "/var/folders/k7/6zx6dx6x7ys3rv3srh0nyfj00000gn/T/down20151116-77262-jgcx65.jpg"
|
103
|
+
```
|
104
|
+
|
89
105
|
## Supported Ruby versions
|
90
106
|
|
91
107
|
* MRI 1.9.3
|
data/down.gemspec
CHANGED
data/lib/down.rb
CHANGED
@@ -25,13 +25,14 @@ module Down
|
|
25
25
|
redirect: false,
|
26
26
|
)
|
27
27
|
|
28
|
-
# open-uri will return a StringIO instead of a Tempfile if the filesize
|
29
|
-
#
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
28
|
+
# open-uri will return a StringIO instead of a Tempfile if the filesize is
|
29
|
+
# less than 10 KB, so if it happens we convert it back to Tempfile. We want
|
30
|
+
# to do this with a Tempfile as well, because open-uri doesn't preserve the
|
31
|
+
# file extension, so we want to run it against #copy_to_tempfile which
|
32
|
+
# does.
|
33
|
+
open_uri_file = downloaded_file
|
34
|
+
downloaded_file = copy_to_tempfile(URI(url).path, open_uri_file)
|
35
|
+
OpenURI::Meta.init downloaded_file, open_uri_file
|
35
36
|
|
36
37
|
downloaded_file.extend DownloadedFile
|
37
38
|
downloaded_file
|
@@ -42,7 +43,7 @@ module Down
|
|
42
43
|
end
|
43
44
|
|
44
45
|
def copy_to_tempfile(basename, io)
|
45
|
-
tempfile = Tempfile.new(basename, binmode: true)
|
46
|
+
tempfile = Tempfile.new(["down", File.extname(basename)], binmode: true)
|
46
47
|
IO.copy_stream(io, tempfile.path)
|
47
48
|
io.rewind
|
48
49
|
tempfile
|
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: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Janko Marohnić
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|