class Dropscreen::Client

Attributes

client[RW]

Public Class Methods

new(opts) click to toggle source
# File lib/dropscreen.rb, line 10
def initialize opts
  @token = ENV['DROPBOX_ACCESS_TOKEN']
  @remote_base_dir = opts[:remote_base_dir] || '/'
  @local_base_dir = opts[:local_base_dir] || '/'
  @client = DropboxClient.new @token
end

Public Instance Methods

exists?(path) click to toggle source
# File lib/dropscreen.rb, line 36
def exists? path
  fullpath = File.join(@remote_base_dir,path)
  begin 
    metadata = @client.metadata(fullpath)
    return not(metadata['is_deleted'])
  rescue DropboxError => e
    if e.message.include? 'not found'
      return false
    else
      raise e
    end
  end 
end
pull(src, dest=src) click to toggle source
# File lib/dropscreen.rb, line 50
def pull(src, dest=src)
  threads = []
  dest = File.join(@local_base_dir, dest)
  FileUtils.mkdir_p(dest) unless File.exists?(dest)
  files = @client.metadata(File.join(@remote_base_dir, src))['contents']
  files.each do |file|
    basename = File.basename(file['path'])
    destination_filename = File.join(dest, basename)
    threads << Thread.new {
      contents = @client.get_file(file['path'])
      open(destination_filename, 'wb') {|f| f.puts contents }
    }
  end
  ThreadsWait.all_waits(*threads)
end
push(src, dest=src) click to toggle source
# File lib/dropscreen.rb, line 17
def push(src, dest=src)
  threads = []
  @client.file_create_folder(File.join(@remote_base_dir, dest)) unless exists? dest
    max_threads = 6
    files = Dir.entries(File.join(@local_base_dir, src)).delete_if { |filename| filename == '.' or filename == '..' or filename == '.DS_Store' }
    files.each_slice(max_threads) do |filenames| 
      
      threads << Thread.new do
        filenames.each do |filename|
          file = open(File.join(@local_base_dir, src, filename))
          dropbox_file_name = File.basename(filename)
          result = @client.put_file(File.join(@remote_base_dir, dest, dropbox_file_name), file, true)
        end
      end
    
    end
  ThreadsWait.all_waits(*threads)
end
remove(path) click to toggle source
# File lib/dropscreen.rb, line 66
def remove path 
  is_deleted = exists?(path) ? @client.file_delete(File.join(@remote_base_dir, path))['is_deleted'] : true
  return is_deleted 
end