Created
May 2, 2013 16:34
-
-
Save r38y/5503449 to your computer and use it in GitHub Desktop.
Handling files from the internet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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