Skip to content

Instantly share code, notes, and snippets.

@kineticac
Created April 8, 2010 22:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kineticac/360631 to your computer and use it in GitHub Desktop.
Save kineticac/360631 to your computer and use it in GitHub Desktop.
require 'open-uri'
require 'net/http'
# example with a Flickr image
require 'flickraw'
FlickRaw.api_key=FLICKR_API_KEY
FlickRaw.shared_secret=FLICKR_SECRET
# grab the most interesting unrestricted image from Flickr
photos = flickr.photos.search :per_page => 1, :page => 1, :content_type => 1, :license => 7, :sort => 'interestingness-desc'
# form the url to the image
image_url = "http://farm#{photo.farm}.static.flickr.com/#{photo.server}/#{photo.id}_#{photo.secret}_s.jpg"
# open the image to a file
file = open image_url
# find a user
user = User.find :first
# set his favorite photo, where favorite_photo is
# already setup in the user model as
# has_attached_file :favorite_photo
user.favorite_photo = file
user.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment