Skip to content

Instantly share code, notes, and snippets.

Created November 27, 2012 22:37
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 anonymous/4157654 to your computer and use it in GitHub Desktop.
Save anonymous/4157654 to your computer and use it in GitHub Desktop.
EventSync Class
class EventSync
attr_reader :event
def initialize(event_or_id)
@event = Event.find(event_or_id)
@data = ["Starting sync for #{event.name}"]
@skipped_photos = 0
end
def data
@data ||= []
end
def sync
sync_start = Time.now
dropbox_files.each do |f|
process_file(f)
end
data << "Completed in #{Time.now - sync_start} seconds. #{@skipped_photos} photos skipped."
data
end
private
def photo_size
1024
end
def process_file(file)
start = Time.now
if new_photos.include? file.path.split('/').last
direct_url = file.direct_url.url
event.photos.create do |f|
f.remote_filename_url = direct_url
end
data << "Photo Added: #{file.path} in #{Time.now - start} seconds."
else
@skipped_photos +=1
end
end
def client
@client ||= Dropbox::API::Client.new(:token => 'uuzpqar2m5839eo', :secret => 'nr9tmx0vc8qh892')
end
def dropbox_files
@dropbox_files ||= client.ls "images/#{event.keyword}/#{photo_size}/"
end
def all_photos
@all_photos = Array.new
@dropbox_files.each do |dbf|
@all_photos << dbf.path.split('/').last
end
@all_photos
end
def existing_photos
@existing_photos = Array.new
event.photos.each do |ep|
@existing_photos << URI.unescape(ep.dropbox_path.split('/').last) rescue []
end
@existing_photos
end
def new_photos
all_photos - existing_photos rescue []
end
#/private
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment