down 1.0.5 → 1.1.0

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 +18 -1
  3. data/down.gemspec +1 -1
  4. data/lib/down.rb +10 -6
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 404e5e406674a9f3edf5871d3769f865cf352e04
4
- data.tar.gz: 10291cb0d07bb70e739118a7474bd18809fb16e9
3
+ metadata.gz: ae5eb943b2a1423b964812f6eabaab6e2db3ac80
4
+ data.tar.gz: d94df3114f9784599967e67bd74b2efaa53ff0de
5
5
  SHA512:
6
- metadata.gz: 5353aac0da3393128585deb6571938e781e1907afc71bcd2555c450a9ed9c690df96d9f3b69ceaa9e91b29b1ca70d674b9058450fa0f03334c14de71aebfa2f9
7
- data.tar.gz: 86151e26fa4ab4b8a82e6653022286fe48965fac21aa083712af22d17d6904d545b162038c101e481d7285939333a49fc4463aa86329017eaa3b973a8dbb0d07
6
+ metadata.gz: 858b972338a5b2252c6674cd55199ce29b491406fba507bac8c86ae8c342c62ca78d9f84bbf7d6f64f3c739e5d15e4328268d0be77e4682da58b1600cb8dda90
7
+ data.tar.gz: 33399d6ee408a32882b3077fc88478527fc3a5d3ce2fd1bbd53e14e77df9ff5bb3668e6950b0720d636c30b8d029c1af5b775ca22959fe00dc1a6c60505ae227
data/README.md CHANGED
@@ -8,6 +8,14 @@ Down is a wrapper around `open-uri` for safe downloading of remote files.
8
8
  gem 'down'
9
9
  ```
10
10
 
11
+ ## Usage
12
+
13
+ ```rb
14
+ require "down"
15
+ tempfile = Down.download("http://example.com/nature.jpg")
16
+ tempfile #=> #<Tempfile:/var/folders/k7/6zx6dx6x7ys3rv3srh0nyfj00000gn/T/20150925-55456-z7vxqz.jpg>
17
+ ```
18
+
11
19
  ## Features
12
20
 
13
21
  If you're downloading files from URLs that come from you, then it's probably
@@ -37,7 +45,7 @@ adds `#original_filename` as well, which is extracted from the URL.
37
45
  require "down"
38
46
  tempfile = Down.download("http://example.com/nature.jpg")
39
47
 
40
- tempfile #=> #<Tempfile:/var/folders/k7/6zx6dx6x7ys3rv3srh0nyfj00000gn/T/20150925-55456-z7vxqz>
48
+ tempfile #=> #<Tempfile:/var/folders/k7/6zx6dx6x7ys3rv3srh0nyfj00000gn/T/20150925-55456-z7vxqz.jpg>
41
49
  tempfile.content_type #=> "image/jpeg"
42
50
  tempfile.original_filename #=> "nature.jpg"
43
51
  ```
@@ -91,6 +99,15 @@ You can specify the time after the request will time out:
91
99
  Down.download "http://example.com/image.jpg", timeout: 5
92
100
  ```
93
101
 
102
+ ### Additional options
103
+
104
+ Any additional options will be forwarded to open-uri, so you can for example
105
+ add basic authentication:
106
+
107
+ ```rb
108
+ Down.download "http://example.com/image.jpg", http_basic_authentication: ['john', 'secret']
109
+ ```
110
+
94
111
  ### Copying to tempfile
95
112
 
96
113
  Down has another "hidden" utility method, `#copy_to_tempfile`, which creates
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "down"
3
- spec.version = "1.0.5"
3
+ spec.version = "1.1.0"
4
4
  spec.authors = ["Janko Marohnić"]
5
5
  spec.email = ["janko.marohnic@gmail.com"]
6
6
 
@@ -13,18 +13,22 @@ module Down
13
13
  def download(url, options = {})
14
14
  url = URI.encode(URI.decode(url))
15
15
 
16
- downloaded_file = URI(url).open(
16
+ max_size = options.delete(:max_size)
17
+ progress = options.delete(:progress)
18
+ timeout = options.delete(:timeout)
19
+
20
+ downloaded_file = URI(url).open({
17
21
  "User-Agent" => "Down/1.0.0",
18
22
  content_length_proc: proc { |size|
19
- raise Down::TooLarge if size && options[:max_size] && size > options[:max_size]
23
+ raise Down::TooLarge if size && max_size && size > max_size
20
24
  },
21
25
  progress_proc: proc { |current_size|
22
- raise Down::TooLarge if options[:max_size] && current_size > options[:max_size]
23
- options[:progress].call(current_size) if options[:progress]
26
+ raise Down::TooLarge if max_size && current_size > max_size
27
+ progress.call(current_size) if progress
24
28
  },
25
- read_timeout: options[:timeout],
29
+ read_timeout: timeout,
26
30
  redirect: false,
27
- )
31
+ }.merge(options))
28
32
 
29
33
  # open-uri will return a StringIO instead of a Tempfile if the filesize is
30
34
  # less than 10 KB, so if it happens we convert it back to Tempfile. We want
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.5
4
+ version: 1.1.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: 2015-12-18 00:00:00.000000000 Z
11
+ date: 2016-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake