Class: Ortega::File

Inherits:
Object
  • Object
show all
Defined in:
lib/ortega/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ File

Returns a new instance of File.



7
8
9
10
11
# File 'lib/ortega/file.rb', line 7

def initialize(args = {})
  @name = args[:name]
  @destination = args[:path]
  @extension = args[:extension]
end

Instance Attribute Details

#destinationObject (readonly)

Returns the value of attribute destination.



5
6
7
# File 'lib/ortega/file.rb', line 5

def destination
  @destination
end

#extensionObject (readonly)

Returns the value of attribute extension.



5
6
7
# File 'lib/ortega/file.rb', line 5

def extension
  @extension
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/ortega/file.rb', line 5

def name
  @name
end

Class Method Details

.get_path(options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ortega/file.rb', line 41

def self.get_path(options)
  options = options.with_indifferent_access
  file = new(options)

  file.instance_eval do
    @extension[0] == '.' ? '' : @extension.insert(0, '.')
    @name = @name.split('/').last + @extension
    @destination = ::File.join(
      "#{file.destination ? ::File.expand_path(file.destination) :  '.'}",
      file.name)
  end

  return file
end

Instance Method Details

#write(response, options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ortega/file.rb', line 13

def write(response, options)
  ::File.open(@destination, 'wb') do |io|
    content_length = response.header['Content-Length']
    chunk_size = 0
    # if response.is_a?(Net::HTTPRedirection)
    #   puts 'redirect'
    #   exit
    # end
    puts "Downloading #{@name}".green unless options[:bar] == false
    response.read_body do |chunk|
      io.write chunk
      chunk_size += chunk.size
      percent = ((chunk_size * 100) / content_length.to_i)
      if percent <= 20
        hashtag = '#'.red
      elsif percent > 20 && percent <= 80
        hashtag = '#'.yellow
      elsif percent > 80
        hashtag = '#'.green
      end
      unless options[:bar] == false
        $stdout.print "#{hashtag * percent} #{percent.to_s.rjust(103 - percent, ' ')} %\r"
        $stdout.flush
      end
    end
  end
end