Class: GallerySync::DropboxSource

Inherits:
Gallery
  • Object
show all
Defined in:
lib/gallery_sync/gallery_sync.rb

Instance Method Summary collapse

Methods inherited from Gallery

#[], #albums, #serialized_albums, #sync_to

Constructor Details

#initialize(root, app_key, app_secret, user_key, user_secret) ⇒ DropboxSource

Creates a dropbox source. given as an absolute directory relative to the dropbox root directry. Must start with a slash (‘/’), e.g. /Photos/mygallery Directories are handled as albums.



316
317
318
319
320
321
322
# File 'lib/gallery_sync/gallery_sync.rb', line 316

def initialize(root, app_key, app_secret, user_key, user_secret)
  @root = root
  session = DropboxSession.new(app_key,app_secret)
  session.set_access_token(user_key,user_secret)
  session.assert_authorized
  @client = DropboxClient.new(session,:dropbox)
end

Instance Method Details

#clientObject



324
325
326
# File 'lib/gallery_sync/gallery_sync.rb', line 324

def client
  @client
end

#getFile(photo) ⇒ Object



328
329
330
# File 'lib/gallery_sync/gallery_sync.rb', line 328

def getFile photo
  @client.get_file(@root + photo.db_path)
end

#load_albumsObject



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/gallery_sync/gallery_sync.rb', line 332

def load_albums
  albums = []
  # retrieve all albums
  @client.(@root)['contents'].map { |c| c['path'] if c['is_dir'] }.each { |dir|  
    # retrieve all photos
    photos = []
    album = Album.new(File.basename(dir))
    @client.(dir)['contents'].map { |c| c['path'] unless c['is_dir'] }.each { |file|  
      if(file =~ /(\.png|\.jpg)\z/i) 
        photos << DropboxPhoto.new(album, File.basename(file), file, self)
      end 
      if(file =~ /album\.yml\z/)
        album.(@client.get_file(file))
      end
    }

    # only add album if it contains at least one photo 
    if( !photos.empty? )
      album.photos = photos
      album.album_photo = photos[0]
      albums << album
    end
  }
  albums
end