Skip to content

Instantly share code, notes, and snippets.

@v-kolesnikov
Last active April 3, 2023 11:31
Show Gist options
  • Save v-kolesnikov/c5807aab0ac7ba5d1ba5e31be32e21e6 to your computer and use it in GitHub Desktop.
Save v-kolesnikov/c5807aab0ac7ba5d1ba5e31be32e21e6 to your computer and use it in GitHub Desktop.
Asynchronous files downloader.
# frozen_string_literal: true
require 'down'
require 'dry/monads'
require 'dry/monads/result'
Down.backend :http
module DL
class Run
include Dry::Monads[:task]
attr_reader :urls
attr_reader :destination
def initialize(urls:, destination:)
@urls = urls
@destination = destination
end
def call
@urls.map do |url|
Task[:io] { Down.download(url, destination: destination) }
end.map(&:to_result)
end
end
end
# frozen_string_literal: true
source "https://rubygems.org"
gem 'down'
gem 'dry-monads'
gem 'http'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment