Skip to content

Instantly share code, notes, and snippets.

@r38y
Created May 2, 2013 16:34
Show Gist options
  • Save r38y/5503449 to your computer and use it in GitHub Desktop.
Save r38y/5503449 to your computer and use it in GitHub Desktop.
Handling files from the internet
require 'open-uri'
require 'securerandom'
url = 'http://bntp-production.imgix.net/uploads/photos/file/14/4a902ce7e2.jpg'
file = open(url)
puts file.class
# at this point you can read from it and use it just like
# the file you read from disk
# puts file.read
# optional if you want to save the file
file_on_disk_path = "/Users/r38y/Desktop/yeah-#{SecureRandom.hex(5)}.jpg"
File.open(file_on_disk_path, 'w') {|f|
f.write(file.read)
}
puts "check out #{file_on_disk_path}!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment