Class: FileDownloader::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/file_downloader/service.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, filepath, logger: nil) ⇒ Service

Returns a new instance of Service.



9
10
11
12
13
14
# File 'lib/file_downloader/service.rb', line 9

def initialize(url, filepath, logger: nil)
  @url = url
  @filepath = filepath
  @retry_count = 0
  @logger = logger || Logger.new($stdout)
end

Instance Method Details

#executeObject



16
17
18
19
20
21
22
23
24
# File 'lib/file_downloader/service.rb', line 16

def execute
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)

  content_length = fetch_content_length(http, uri)
  raise NoResponseBodyError if content_length.to_i.zero?
  download_file(http, uri, content_length)
  filepath
end