Class: DCP::Dropbox

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

Instance Method Summary collapse

Constructor Details

#initialize(token) ⇒ Dropbox

Returns a new instance of Dropbox.



6
7
8
# File 'lib/dcp/dropbox.rb', line 6

def initialize(token)
	@client = DropboxApi::Client.new(token)
end

Instance Method Details

#directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'lib/dcp/dropbox.rb', line 34

def directory?(path)
	return true if path.empty? || path == '/'
	begin
		@client.(path).class == DropboxApi::Metadata::Folder
	rescue ::DropboxApi::Errors::NotFoundError
		raise Errno::ENOENT.new('file not found on dropbox')
	end
end

#file?(path) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
# File 'lib/dcp/dropbox.rb', line 25

def file?(path)
	return false if path.empty? || path == '/'
	begin
		@client.(path).class == DropboxApi::Metadata::File
	rescue ::DropboxApi::Errors::NotFoundError
		raise Errno::ENOENT.new('file not found on dropbox')
	end
end

#open(path, opts) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dcp/dropbox.rb', line 10

def open(path, opts)
	if opts =~ /w/ # write
		client = @client
		info = DropboxApi::Metadata::CommitInfo.new('path'=>path, 'mode'=>:add)
		cursor = client.upload_session_start('')
		cursor.define_singleton_method(:write) do |data|
			client.upload_session_append_v2(cursor, data)
		end
		yield cursor
		client.upload_session_finish(cursor, info)
	else # read (default)
		raise StandardError.new('read from dropbox does not implement.')
	end
end