Sha256: 4dcf686a0151f4432bd0c4f620e4ef9d892709d70c90539a8e040d94b7659e16

Contents?: true

Size: 908 Bytes

Versions: 2

Compression:

Stored size: 908 Bytes

Contents

require 'typhoeus'
require 'pathname'

module PRSS
  class Downloader
    attr_reader :hydra

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

    def download_to(output_path)
      output = Pathname.new(output_path).expand_path
      raise 'output path is not directory' unless output.directory?

      @files = []
      @links.each do |link|
        request = Typhoeus::Request.new(link, :follow_location => 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)

      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.0.4 lib/prss/downloader.rb
prss-0.0.3 lib/prss/downloader.rb