Class: Nyaa::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/nyaa/downloader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, path, retries = 3) ⇒ Downloader

Returns a new instance of Downloader.



7
8
9
10
11
12
13
14
15
# File 'lib/nyaa/downloader.rb', line 7

def initialize(url, path, retries = 3)
  self.target      = url
  self.destination = Nyaa::Utils.safe_path(path)
  self.retries     = retries
  self.response    = request
  self.filename    = name_from_disposition

  @fail = nil
end

Instance Attribute Details

#destinationObject

Returns the value of attribute destination.



4
5
6
# File 'lib/nyaa/downloader.rb', line 4

def destination
  @destination
end

#filenameObject

Returns the value of attribute filename.



5
6
7
# File 'lib/nyaa/downloader.rb', line 5

def filename
  @filename
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/nyaa/downloader.rb', line 5

def response
  @response
end

#retriesObject

Returns the value of attribute retries.



4
5
6
# File 'lib/nyaa/downloader.rb', line 4

def retries
  @retries
end

#targetObject

Returns the value of attribute target.



4
5
6
# File 'lib/nyaa/downloader.rb', line 4

def target
  @target
end

Instance Method Details

#failed?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/nyaa/downloader.rb', line 28

def failed?
  @fail
end

#saveObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/nyaa/downloader.rb', line 17

def save
  unless @fail
    path = self.destination + '/' + filename;
    File.open("#{self.destination}/#{filename}", 'w') do |f|
      f.write(self.response.read)
    end
    return path;
  end
  return nil;
end