Sha256: 003170ee0914fc6c6e1a70964112cf2e396da0824df2f5b57d1e137ed232443b

Contents?: true

Size: 785 Bytes

Versions: 1

Compression:

Stored size: 785 Bytes

Contents

require "fileutils"
require "open-uri"
require "tmpdir"

module Photomosaic
  class ImageDownloader
    def initialize(save_dir = tmp_dir)
      @save_dir = save_dir
    end

    def download_images(image_url_list)
      image_url_list.inject([]) do |path_list, image_url|
        image_path = File.join(@save_dir, File.basename(image_url))

        begin
          download_image(image_url, image_path)
          path_list << image_path
        rescue
        end

        path_list
      end
    end

    def remove_save_dir
      FileUtils.remove_entry_secure(@save_dir)
    end

    private

    def download_image(image_url, image_path)
      open(image_path, "wb+") { |f| f.puts open(image_url).read }
    end

    def tmp_dir
      Dir.mktmpdir("photomosaic")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
photomosaic-0.0.1 lib/photomosaic/image_downloader.rb