ortega 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -4
- data/lib/ortega/file.rb +25 -10
- data/lib/ortega/http.rb +9 -12
- data/lib/ortega/uri.rb +8 -0
- data/lib/ortega/version.rb +1 -1
- data/ortega.gemspec +2 -1
- metadata +17 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aaee603cae0ab279d2525fcd1c1999c23d8a2edf5c5427efdb987e4e26311854
|
4
|
+
data.tar.gz: 9762414bbe6d17a1d31a6c18518ad4b717e431c8e708ccaba24c92956556d63a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f5f2975a82ac9d6f0ac21342689d338b4e3d592a30f6c5ce576caa24a71aced6a67937da15d7a8b79c8ab67e0f935d9a2e462a854c687d7780f3ac76e7c03a2
|
7
|
+
data.tar.gz: 356d32a47c94bddeede35636f23714920d83280ed96b273c774e5741aa351ee515e67aacf96fc81071ac29ddc42071c7fd2c2ed37774c530d0a8176059fff97b
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Ortega
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Ruby file downloader
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,32 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
```ruby
|
24
|
+
require 'ortega'
|
25
|
+
|
26
|
+
Ortega.download('example.com/downloadable_file.pdf')
|
27
|
+
# Output
|
28
|
+
# Downloading downloadable_file.pdf
|
29
|
+
############ 12 %
|
30
|
+
```
|
31
|
+
|
32
|
+
You can specify target path, file name and its extension by passing arguments
|
33
|
+
|
34
|
+
Example
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
Ortega.download('example.com/downloadable_file.pdf', name: 'my_file.pdf', path: '~/Desktop')
|
38
|
+
```
|
39
|
+
If you want to hide progress bar, set ```bar``` option to false
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
Ortega.download('example.com/downloadable_file.pdf', bar: false)
|
43
|
+
```
|
44
|
+
|
45
|
+
If url does not contain the extension, it should be good to spectify it manually as:
|
46
|
+
```ruby
|
47
|
+
Ortega.download('example.com/downloadable_file', extension: 'pdf')
|
48
|
+
```
|
26
49
|
|
27
50
|
## Development
|
28
51
|
|
data/lib/ortega/file.rb
CHANGED
@@ -1,24 +1,39 @@
|
|
1
|
+
require 'corol'
|
2
|
+
|
1
3
|
module Ortega
|
2
4
|
class File
|
3
|
-
attr_reader :name, :destination
|
4
|
-
|
5
|
+
attr_reader :name, :destination, :extension
|
6
|
+
|
5
7
|
def initialize(args = {})
|
6
8
|
@name = args[:name]
|
7
9
|
@destination = args[:path]
|
8
10
|
@extension = args[:extension]
|
9
11
|
end
|
10
12
|
|
11
|
-
def write(response)
|
13
|
+
def write(response, options)
|
12
14
|
::File.open(@destination, 'wb') do |io|
|
13
15
|
content_length = response.header['Content-Length']
|
14
16
|
chunk_size = 0
|
15
|
-
|
17
|
+
# if response.is_a?(Net::HTTPRedirection)
|
18
|
+
# puts 'redirect'
|
19
|
+
# exit
|
20
|
+
# end
|
21
|
+
puts "Downloading #{@name}".green unless options[:bar] == false
|
16
22
|
response.read_body do |chunk|
|
17
23
|
io.write chunk
|
18
24
|
chunk_size += chunk.size
|
19
25
|
percent = ((chunk_size * 100) / content_length.to_i)
|
20
|
-
|
21
|
-
|
26
|
+
if percent <= 20
|
27
|
+
hashtag = '#'.red
|
28
|
+
elsif percent > 20 && percent <= 80
|
29
|
+
hashtag = '#'.yellow
|
30
|
+
elsif percent > 80
|
31
|
+
hashtag = '#'.green
|
32
|
+
end
|
33
|
+
unless options[:bar] == false
|
34
|
+
$stdout.print "#{hashtag * percent} #{percent.to_s.rjust(103 - percent, ' ')} %\r"
|
35
|
+
$stdout.flush
|
36
|
+
end
|
22
37
|
end
|
23
38
|
end
|
24
39
|
end
|
@@ -27,14 +42,14 @@ module Ortega
|
|
27
42
|
options = options.with_indifferent_access
|
28
43
|
file = new(options)
|
29
44
|
|
30
|
-
file.instance_eval do
|
31
|
-
@name = @name.split('/').last
|
45
|
+
file.instance_eval do
|
46
|
+
@name = @name.split('/').last + (@extension[0] == '.' ? '' : @extension.insert(0, '.'))
|
32
47
|
@destination = ::File.join(
|
33
48
|
"#{file.destination ? ::File.expand_path(file.destination) : '.'}",
|
34
49
|
file.name)
|
35
50
|
end
|
36
51
|
|
37
52
|
return file
|
38
|
-
end
|
53
|
+
end
|
39
54
|
end
|
40
|
-
end
|
55
|
+
end
|
data/lib/ortega/http.rb
CHANGED
@@ -1,31 +1,28 @@
|
|
1
1
|
require 'net/http'
|
2
2
|
require 'ortega/file'
|
3
|
+
require 'ortega/uri'
|
3
4
|
require 'active_support/core_ext/hash/indifferent_access'
|
4
5
|
|
5
6
|
module Ortega
|
6
7
|
module HTTP
|
8
|
+
include Ortega::URI
|
9
|
+
|
7
10
|
def download(url, options = {}, &block)
|
8
11
|
options = options.with_indifferent_access
|
9
|
-
url =
|
12
|
+
url = url_helper(url)
|
10
13
|
options[:name] = url.path if options[:name].nil?
|
14
|
+
options[:extension] = ::File.extname(url.path) if options[:extension].nil?
|
15
|
+
options[:bar] = true if options[:bar].nil?
|
11
16
|
file = Ortega::File.get_path(options)
|
12
|
-
|
17
|
+
|
13
18
|
http = Net::HTTP.new(url.host, url.port)
|
14
19
|
http.use_ssl = true if url.scheme == 'https'
|
15
|
-
|
20
|
+
|
16
21
|
http.start do |http|
|
17
22
|
http.request Net::HTTP::Get.new url do |response|
|
18
|
-
file.write(response)
|
23
|
+
file.write(response, options)
|
19
24
|
end
|
20
25
|
end
|
21
26
|
end
|
22
|
-
|
23
|
-
class << self
|
24
|
-
def url_helper(url)
|
25
|
-
url.insert(0, 'http://') unless url.match(/http/)
|
26
|
-
return URI url
|
27
|
-
end
|
28
|
-
end
|
29
27
|
end
|
30
28
|
end
|
31
|
-
|
data/lib/ortega/uri.rb
ADDED
data/lib/ortega/version.rb
CHANGED
data/ortega.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ortega
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Farhad
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -86,6 +86,20 @@ dependencies:
|
|
86
86
|
- - ">="
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: 5.2.2
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: corol
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: 0.2.0
|
96
|
+
type: :runtime
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: 0.2.0
|
89
103
|
description:
|
90
104
|
email:
|
91
105
|
- farhad9801@gmail.com
|
@@ -106,6 +120,7 @@ files:
|
|
106
120
|
- lib/ortega/errors.rb
|
107
121
|
- lib/ortega/file.rb
|
108
122
|
- lib/ortega/http.rb
|
123
|
+
- lib/ortega/uri.rb
|
109
124
|
- lib/ortega/version.rb
|
110
125
|
- ortega.gemspec
|
111
126
|
homepage: https://github.com/0x2C6/ortega_ruby
|