ortega 0.0.6 → 0.0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f56eb0e7e3b21f9980007be2393a63bac1db3e29aa12ebeda31c1a38910603a6
4
- data.tar.gz: 3bc66aeefd7227f2b999afe23ff71f2c18b149a0d9912e49a5399b942fda23b7
3
+ metadata.gz: aaee603cae0ab279d2525fcd1c1999c23d8a2edf5c5427efdb987e4e26311854
4
+ data.tar.gz: 9762414bbe6d17a1d31a6c18518ad4b717e431c8e708ccaba24c92956556d63a
5
5
  SHA512:
6
- metadata.gz: 8fbe9c71f5b1f280eb0063c849222605536f43fd91cea4f213a8d7d91c0e0081b2671bd0e9635cd781ddd4be1d21b52f54fb408a8dee3231092ccfc8ff43643a
7
- data.tar.gz: c5dbca6d91b708b6805051d38052c56f382880c253b947f482ee0aef4a9a99181ffbbacee11f6b3b91b2ae43d23c3d052293e32f61004b366a0bc644e48d4529
6
+ metadata.gz: 0f5f2975a82ac9d6f0ac21342689d338b4e3d592a30f6c5ce576caa24a71aced6a67937da15d7a8b79c8ab67e0f935d9a2e462a854c687d7780f3ac76e7c03a2
7
+ data.tar.gz: 356d32a47c94bddeede35636f23714920d83280ed96b273c774e5741aa351ee515e67aacf96fc81071ac29ddc42071c7fd2c2ed37774c530d0a8176059fff97b
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Ortega
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/ortega`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
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
 
@@ -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
- puts "Downloading #{@name}"
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
- $stdout.print "#{'#' * percent} #{percent.to_s.rjust(103 - percent, ' ')} %\r"
21
- $stdout.flush
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
@@ -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 = HTTP.url_helper(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
-
@@ -0,0 +1,8 @@
1
+ module Ortega
2
+ module URI
3
+ def url_helper(url)
4
+ url.insert(0, 'http://') unless url.match(/http/)
5
+ return URI url
6
+ end
7
+ end
8
+ end
@@ -1,3 +1,3 @@
1
1
  module Ortega
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -26,4 +26,5 @@ require "ortega/version"
26
26
  spec.add_development_dependency "rspec", "~> 3.0"
27
27
 
28
28
  spec.add_dependency 'activesupport', '~> 5.2', '>= 5.2.2'
29
- end
29
+ spec.add_dependency 'corol', '~> 0.2.0'
30
+ end
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.6
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-02-17 00:00:00.000000000 Z
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