seancdavis
12/7/2015 - 12:24 PM

Download a collection of images from URLs using Ruby

Download a collection of images from URLs using Ruby

require 'open-uri'

def download_image(url, dest)
  open(url) do |u|
    File.open(dest, 'wb') { |f| f.write(u.read) }
  end
end

urls = [
  'http://petsfans.com/wp-content/uploads/2014/11/edfsaf.jpg',
  'http://dailynewsdig.com/wp-content/uploads/2012/06/funny-cats.jpg',
  'https://i.ytimg.com/vi/tntOCGkgt98/maxresdefault.jpg'
]

urls.each { |url| download_image(url, url.split('/').last) }