public
anonymous / event_sync.rb
Created

EventSync Class

  • Download Gist
event_sync.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.