down 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +16 -0
  3. data/down.gemspec +1 -1
  4. data/lib/down.rb +9 -8
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65c086180eb4e40493ef9c6dc92f4d4a79466e3e
4
- data.tar.gz: 60b796696b95541ac518dd3a915d4cac8aca0435
3
+ metadata.gz: b92313f399fb288e1c324b1355cd26b3eb02ca00
4
+ data.tar.gz: bec353dfe70af7fb445b0203ecdd0f20cacc1a29
5
5
  SHA512:
6
- metadata.gz: 5d828860d9a2c813aac16c1e4839f726bbee83d82b18abf426109403ae3341dc8548a04bd1546a8f57e42847133bf5c1d37c3282c771e3ed6742eecd964797ef
7
- data.tar.gz: b60dea3f69f5b08d1e135962530677f0d8d44dd0b04e5907065065b7d8beab688b009d08ab6479d30c9b4cf5cd81182dcdfe52c527968f6b10c27e7a7a25a01c
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "down"
3
- spec.version = "1.0.2"
3
+ spec.version = "1.0.3"
4
4
  spec.authors = ["Janko Marohnić"]
5
5
  spec.email = ["janko.marohnic@gmail.com"]
6
6
 
@@ -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
- # is less than 10 KB, so if it happens we convert it back to Tempfile.
30
- if downloaded_file.is_a?(StringIO)
31
- stringio = downloaded_file
32
- downloaded_file = copy_to_tempfile("open-uri", stringio)
33
- OpenURI::Meta.init downloaded_file, stringio
34
- end
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.2
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-10-24 00:00:00.000000000 Z
11
+ date: 2015-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake