Class: Dropscreen::Client
- Inherits:
-
Object
- Object
- Dropscreen::Client
- Defined in:
- lib/dropscreen.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
Returns the value of attribute client.
Instance Method Summary collapse
- #exists?(path) ⇒ Boolean
-
#initialize(opts) ⇒ Client
constructor
A new instance of Client.
- #pull(src, dest = src) ⇒ Object
- #push(src, dest = src) ⇒ Object
- #remove(path) ⇒ Object
Constructor Details
#initialize(opts) ⇒ Client
Returns a new instance of 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
#client ⇒ Object
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
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dropscreen.rb', line 36 def exists? path fullpath = File.join(@remote_base_dir,path) begin = @client.(fullpath) return not(['is_deleted']) rescue DropboxError => e if e..include? 'not found' return false else raise e end end end |
#pull(src, dest = src) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# 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.(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 30 31 32 33 34 |
# 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) ⇒ Object
66 67 68 69 |
# 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 |