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

Returns a new instance of Client.



9
10
11
12
13
14
# File 'lib/dropscreen.rb', line 9

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.



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

def client
  @client
end

Instance Method Details

#exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dropscreen.rb', line 26

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



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dropscreen.rb', line 38

def pull(src, dest=src)
  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)
    contents = @client.get_file(file['path'])
    open(destination_filename, 'wb') {|f| f.puts contents }
  end 
end

#push(src, dest = src) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/dropscreen.rb', line 16

def push(src, dest=src)
  @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)
     @client.put_file(File.join(@remote_base_dir, dest, dropbox_file_name), file, true)
   end
end

#remove(path) ⇒ Object



50
51
52
53
# File 'lib/dropscreen.rb', line 50

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