Class: Dropscreen::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Client



10
11
12
13
14
15
# 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

Instance Attribute Details

#clientObject

Returns the value of attribute client.



8
9
10
# File 'lib/dropscreen.rb', line 8

def client
  @client
end

Instance Method Details

#exists?(path) ⇒ Boolean



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dropscreen.rb', line 31

def exists? path
  fullpath = File.join(@remote_base_dir,path)
  @client.(fullpath)
  return true
rescue DropboxError => e
  if e.message.include? 'not found'
    return false
  else
    raise e
  end
end

#pull(src, dest = src) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dropscreen.rb', line 43

def pull(src, dest=src)
  threads = []
  dest = File.join(@local_base_dir, dest)
  FileUtils.mkdir_p(dest) unless File.exists?(dest)
  files = @client.(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) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# 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
   Dir.foreach(File.join(@local_base_dir, src)) do |filename|
    next if filename == '.' or filename == '..' or filename == '.DS_Store'
    file = open(File.join(@local_base_dir, src, filename))
    dropbox_file_name = File.basename(filename)
    threads << Thread.new {
      @client.put_file(File.join(@remote_base_dir, dest, dropbox_file_name), file, true)
    }
  end
  ThreadsWait.all_waits(*threads)
end

#remove(path) ⇒ Object



59
60
61
62
# File 'lib/dropscreen.rb', line 59

def remove path 
  is_deleted = exists?(path) ? @client.file_delete(File.join(@remote_base_dir, path))['is_deleted'] : true
  return is_deleted 
end