Class: SolrWrapper::Downloader

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

Defined Under Namespace

Classes: SafeProgressBar

Class Method Summary collapse

Class Method Details

.fetch_with_progressbar(url, output) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/solr_wrapper/downloader.rb', line 7

def self.fetch_with_progressbar(url, output)
  pbar = SafeProgressBar.new(title: File.basename(url), total: nil, format: '%t: |%B| %p%% (%e )')

  client = Faraday.new(url) do |faraday|
    faraday.use Faraday::FollowRedirects::Middleware
    faraday.adapter Faraday.default_adapter
  end

  File.open(output, 'wb') do |f|
    client.get do |req|
      req.options.on_data = Proc.new do |chunk, overall_received_bytes, env|
        if env
          pbar.total = env.response_headers['content-length'].to_i
          pbar.progress = overall_received_bytes
        else
          pbar.increment
        end

        f.write(chunk)
      end
    end
  end

  true
rescue Faraday::Error => e
  raise SolrWrapperError, "Unable to download solr from #{url}\n#{e}"
end