Skip to content

Instantly share code, notes, and snippets.

@reagent
Created January 11, 2009 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reagent/45619 to your computer and use it in GitHub Desktop.
Save reagent/45619 to your computer and use it in GitHub Desktop.
A sample implementation of using Fleakr w/ ActiveRecord
class Photo < ActiveRecord::Base
belongs_to :user
def self.flickr_mapping
{
:flickr_id => :id,
:description => :description,
:title => :title,
:url => 'small.url'
}
end
def self.from_flickr(flickr_photo)
photo = self.find_by_flickr_id(flickr_photo.id)
if photo.nil?
photo = self.new
flickr_mapping.each do |attribute, flickr_attribute|
photo.send("#{attribute}=".to_sym, eval("flickr_photo.#{flickr_attribute}"))
end
end
photo
end
end
require 'forwardable'
class PhotoSet < ActiveRecord::Base
extend Forwardable
def_delegator :flickr, :photos, :flickr_photos
belongs_to :user
has_many :photos, :foreign_key => :set_id
def self.flickr_mapping
{
:flickr_id => :id,
:description => :description,
:title => :title
}
end
def save_photos
flickr_photos.each do |flickr_photo|
photo = self.photos.from_flickr flickr_photo
photo.save!
end
end
def self.from_flickr(flickr_set)
set = PhotoSet.find_by_flickr_id(flickr_set.id)
if set.nil?
set = self.new
flickr_mapping.each do |attribute, flickr_attribute|
set.send("#{attribute}=".to_sym, eval("flickr_set.#{flickr_attribute}"))
end
end
set
end
def flickr
@flickr ||= begin
set = Fleakr::Objects::Set.new
set.id = self.flickr_id
set
end
end
private :flickr
end
require 'forwardable'
class User < ActiveRecord::Base
extend Forwardable
def_delegator :flickr, :photos, :flickr_photos
def_delegator :flickr, :sets, :flickr_sets
has_many :photos
has_many :sets, :class_name => 'PhotoSet'
def save_photos
flickr_photos.each do |flickr_photo|
photo = self.photos.from_flickr flickr_photo
photo.save!
end
end
def flickr
@flickr ||= Fleakr.user(self.flickr_username)
end
private :flickr
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment