Sha256: a97f2f86c37a3b716d4194f845167ce8c382a310957b9b5577d49d88ae225b46

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

require 'typhoeus'
require 'pathname'

module PRSS
  class Downloader
    attr_reader :hydra

    def initialize(links)
      @links = links
      @hydra = Typhoeus::Hydra.new
    end

    def self.verify!(output_path)
      output = Pathname.new(output_path).expand_path
      raise 'output path is not directory' unless output.directory?
      output
    end

    def download_to(output_path)
      output = self.class.verify!(output_path)

      @files = []
      @links.each do |link|
        request = Typhoeus::Request.new(link, followlocation: true)

        request.on_complete do |response|
          @files << save_file(response, output)
        end

        hydra.queue request
      end

      hydra.run

      @files
    end

    def save_file(response, output)
      filename = response.headers_hash['Content-Disposition'][/filename="(.+)"$/ ,1]
      file = output.join(filename)

      return if file.exist?

      open(file, 'wb') do |file|
        file.write(response.body)
      end

      filename
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
prss-0.1.1 lib/prss/downloader.rb
prss-0.1.0 lib/prss/downloader.rb