download 1.0.0 → 1.1.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
  SHA1:
3
- metadata.gz: 1d199ab74fcb858aa23df63831f09b1fd012f156
4
- data.tar.gz: b3591e4c243edfaa606d1499e7ae6c461d6645d7
3
+ metadata.gz: 7b374492161fe8ab621081b7fe14ffd09f1e013d
4
+ data.tar.gz: 0be4af9659cdeddbbabf3a67085551aaa1adab51
5
5
  SHA512:
6
- metadata.gz: 2493a20d5551ff0224668ad13051110b55be003338598ed9050a4881f8eec8d15e8488184f3bb46c08fb4e92e7015d305fd75b8239b3ec5c8f1db36d4bc905c8
7
- data.tar.gz: 98dbc8a3f50bb694481f2c8fe6ed30044dcbb369b488a5a995f7c6eaa27481ea01989f3ec152f32e62d3cf8f117090fde0c821a5b129385350f8ed9c8fdc2aa7
6
+ metadata.gz: 0e4daefccc7bc6230592b0b57317d82e76fe930d3d25808df39e6e90d2948deaae6536aedb175858489d8744fcc678da177b3e8ea6548beafec5d18811750375
7
+ data.tar.gz: c8661238df7f3148f7c5f7d6c6af7d658a7d8dee4d466d52da4ae403b98a53fdb17102edc3d8aac4b8e0ea58d3a49d582fcbd274a93140007df6ba649f105cff
data/README.md CHANGED
@@ -4,6 +4,8 @@ Ruby Download helper that can handle even big file downloads too
4
4
 
5
5
  ## Use case
6
6
 
7
+ auth less use case
8
+
7
9
  ```ruby
8
10
 
9
11
  require 'download'
@@ -11,6 +13,17 @@ Ruby Download helper that can handle even big file downloads too
11
13
 
12
14
  ```
13
15
 
16
+ with basic auth
17
+
18
+ ```ruby
19
+
20
+ Download.file(
21
+ 'http://www.example.com/something.zip',
22
+ http_basic_authentication: ['username', '123abc']
23
+ )
24
+
25
+ ```
26
+
14
27
  ## Behavior
15
28
 
16
29
  if the target file path is a directory, the file name will be based on the uri-s file name.
@@ -5,13 +5,13 @@ module Download
5
5
 
6
6
  class Object
7
7
 
8
+ attr_accessor :url, :path, :options
9
+
8
10
  def initialize(hash={})
9
11
  set_multi(hash)
10
- raise(ArgumentError,'url is required') unless url
12
+ raise(ArgumentError, 'url is required') unless url
11
13
  end
12
14
 
13
- attr_accessor :url,:path
14
-
15
15
  def uri_file_name
16
16
  @uri_file_name ||= URI.parse(url).path.to_s.split('/').last
17
17
  end
@@ -19,9 +19,9 @@ module Download
19
19
  # return a string with a file path where the file will be saved
20
20
  def file_path
21
21
 
22
- self.path= File.join(Dir.pwd,uri_file_name) unless path
22
+ self.path= File.join(Dir.pwd, uri_file_name) unless path
23
23
  if File.directory?(path)
24
- self.path= File.join(self.path,uri_file_name)
24
+ self.path= File.join(self.path, uri_file_name)
25
25
  end
26
26
 
27
27
  self.path
@@ -34,8 +34,8 @@ module Download
34
34
  set_multi(hash)
35
35
 
36
36
  File.delete(file_path) if File.exist?(file_path)
37
- File.open(file_path,'wb') do |file_obj|
38
- Kernel.open(url) do |fin|
37
+ File.open(file_path, 'wb') do |file_obj|
38
+ Kernel.open(*[url,options].compact) do |fin|
39
39
  while (buf = fin.read(8192))
40
40
  file_obj << buf
41
41
  end
@@ -49,18 +49,23 @@ module Download
49
49
  protected
50
50
 
51
51
  def set_multi(hash={})
52
- raise(ArgumentError,'input must be hash!') unless hash.is_a?(Hash)
53
- hash.each_pair do |key,value|
54
- self.public_send("#{key}=",value)
55
- end
52
+ raise(ArgumentError, 'input must be hash!') unless hash.is_a?(Hash)
53
+ hash.each_pair { |key, value| self.public_send("#{key}=", value) unless value.nil? }
56
54
  end
57
55
 
58
56
  end
59
57
 
60
58
  class << self
61
59
 
62
- def file(file_url,target_file_path=nil)
63
- Download::Object.new(url: file_url,path: target_file_path).start
60
+ # first argumnet is url, required
61
+ # second argument is path, optional
62
+ # third argument is open-uri options, optional
63
+ def file(*args)
64
+ Download::Object.new(
65
+ url: args.shift,
66
+ path: (args[0].is_a?(String) ? args[0] : nil),
67
+ options: (args.last.is_a?(Hash) ? args.last : nil)
68
+ ).start
64
69
  end
65
70
 
66
71
  end
@@ -1,3 +1,3 @@
1
1
  module Download
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: download
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-09 00:00:00.000000000 Z
11
+ date: 2015-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler