Sha256: bf8f13fafe69e03f0b8d1f29322e765a643802ff815597bc9ee0cfea55f90450

Contents?: true

Size: 840 Bytes

Versions: 3

Compression:

Stored size: 840 Bytes

Contents

require 'net/http'
require 'pathname'

module PRSS
  class Downloader

    def initialize(links)
      @links = links
    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.map do |uri|
        response = Net::HTTP.get_response(uri)
        save_file(response, output)
      end.compact
    end

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

      return if file.exist?

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

      filename
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
prss-0.2.4 lib/prss/downloader.rb
prss-0.2.3 lib/prss/downloader.rb
prss-0.2.2 lib/prss/downloader.rb