Class: RGet::Dropbox

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDropbox

Returns a new instance of Dropbox.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dropbox.rb', line 10

def initialize
  token = Pit::get('rget-dropbox')
  unless token[:api_token]
    if token[:api_key]
      api_key = token[:api_key]
    else
      print "Enter dropbox app key: "
      api_key = $stdin.gets.chomp
    end

    if token[:api_secret]
      api_secret = token[:api_secret]
    else
      print "Enter dropbox app secret: "
      api_secret = $stdin.gets.chomp
    end

    authenticator = DropboxApi::Authenticator.new(api_key, api_secret)
    puts "\nGo to this url and click 'Authorize' to get the token:"
    puts authenticator.authorize_url

    token.clear # delete all old settings
    print "Enter the token: "
    code = $stdin.gets.chomp
    token[:api_token] = authenticator.get_token(code).token
    Pit::set('rget-dropbox', data: token)
  end
  @client = DropboxApi::Client.new(token[:api_token])
end

Class Method Details

.clientObject



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

def self.client
  self.new
end

Instance Method Details

#exist?(dst, dropbox_path) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/dropbox.rb', line 40

def exist?(dst, dropbox_path)
  !(@client.list_folder(dropbox_path).entries.find{|e|e.name == dst} == nil)
end

#upload(dropbox_path) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/dropbox.rb', line 44

def upload(dropbox_path)
  info = DropboxApi::::CommitInfo.new('path'=>dropbox_path, 'mode'=>:add)
  cursor = @client.upload_session_start('')
  while data = yield
    @client.upload_session_append_v2(cursor, data)
  end
  @client.upload_session_finish(cursor, info)
end