dropbox-sdk 1.6.4 → 1.6.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b96fef902b9021bea9e01dcc8637e056c4a077bf
4
+ data.tar.gz: 384f6e747666976c9c18727854d6a116d949c9e6
5
+ SHA512:
6
+ metadata.gz: 5f938338dce089ed42bd35c78b9dac8b22a4f3caeb33d6c00f914ddca42c9ce3be4d3e5fc75ed5e6ecd3c92868b34ffd0a3b4e4dd825e04f0790d5f1b746bf3c
7
+ data.tar.gz: 1bd83195c8d90988c3f1e3c26b72e7fd965e72cd61b2dce05c105456c79766f39a448d5a3487aa9a9cce691fd5f2cf351c26e0e68a27bbfdb8719237502c27b8
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ * Add support for /previews endpoint.
2
+
3
+ 1.6.5 (2015-7-28)
4
+ * Update SDK to include a few new fixes
5
+ * Sync up github and rubygems version of this SDK
6
+
1
7
  1.6.4 (2014-4-12)
2
8
  * Bugfixes
3
9
  * Update gemspec to include all examples
data/README CHANGED
@@ -50,3 +50,12 @@ Running the Examples
50
50
  There are example programs included in the tarball. Before you can run an
51
51
  example, you need to edit the ".rb" file and put your Dropbox API app key and
52
52
  secret in the "APP_KEY" and "APP_SECRET" constants.
53
+
54
+ ----------------------------------
55
+ Running the Tests
56
+
57
+ # gem install bundler
58
+
59
+ # bundle install
60
+ # cd test
61
+ # DROPBOX_RUBY_SDK_ACCESS_TOKEN=<oauth2-access-token> bundle exec ruby sdk_test.rb
@@ -10,61 +10,61 @@ APP_SECRET = ''
10
10
  STATE_FILE = 'search_cache.json'
11
11
 
12
12
  def main()
13
- if APP_KEY == '' or APP_SECRET == ''
14
- warn "ERROR: Set your APP_KEY and APP_SECRET at the top of search_cache.rb"
15
- exit
16
- end
17
- prog_name = __FILE__
18
- args = ARGV
19
- if args.size == 0
20
- warn("Usage:\n")
21
- warn(" #{prog_name} <local-file-path> <dropbox-target-path> <chunk-size-in-bytes>")
22
- exit
23
- end
13
+ if APP_KEY == '' or APP_SECRET == ''
14
+ warn "ERROR: Set your APP_KEY and APP_SECRET at the top of search_cache.rb"
15
+ exit
16
+ end
17
+ prog_name = __FILE__
18
+ args = ARGV
19
+ if args.size == 0
20
+ warn("Usage:\n")
21
+ warn(" #{prog_name} <local-file-path> <dropbox-target-path> <chunk-size-in-bytes>")
22
+ exit
23
+ end
24
24
 
25
- if args.size != 3
26
- warn "ERROR: expecting exactly three arguments. Run with no arguments for help."
27
- exit(1)
28
- end
25
+ if args.size != 3
26
+ warn "ERROR: expecting exactly three arguments. Run with no arguments for help."
27
+ exit(1)
28
+ end
29
29
 
30
- web_auth = DropboxOAuth2FlowNoRedirect.new(APP_KEY, APP_SECRET)
31
- authorize_url = web_auth.start()
32
- puts "1. Go to: #{authorize_url}"
33
- puts "2. Click \"Allow\" (you might have to log in first)."
34
- puts "3. Copy the authorization code."
30
+ web_auth = DropboxOAuth2FlowNoRedirect.new(APP_KEY, APP_SECRET)
31
+ authorize_url = web_auth.start()
32
+ puts "1. Go to: #{authorize_url}"
33
+ puts "2. Click \"Allow\" (you might have to log in first)."
34
+ puts "3. Copy the authorization code."
35
35
 
36
- print "Enter the authorization code here: "
37
- STDOUT.flush
38
- auth_code = STDIN.gets.strip
36
+ print "Enter the authorization code here: "
37
+ STDOUT.flush
38
+ auth_code = STDIN.gets.strip
39
39
 
40
- access_token, user_id = web_auth.finish(auth_code)
40
+ access_token, user_id = web_auth.finish(auth_code)
41
41
 
42
- c = DropboxClient.new(access_token)
42
+ c = DropboxClient.new(access_token)
43
43
 
44
- local_file_path = args[0]
45
- dropbox_target_path = args[1]
46
- chunk_size = args[2].to_i
44
+ local_file_path = args[0]
45
+ dropbox_target_path = args[1]
46
+ chunk_size = args[2].to_i
47
47
 
48
- # Upload the file
49
- local_file_size = File.size(local_file_path)
50
- uploader = c.get_chunked_uploader(File.new(local_file_path, "r"), local_file_size)
51
- retries = 0
52
- puts "Uploading..."
53
- while uploader.offset < uploader.total_size
54
- begin
55
- uploader.upload(chunk_size)
56
- rescue DropboxError => e
57
- if retries > 10
58
- puts "- Error uploading, giving up."
59
- break
60
- end
61
- puts "- Error uploading, trying again..."
62
- retries += 1
63
- end
48
+ # Upload the file
49
+ local_file_size = File.size(local_file_path)
50
+ uploader = c.get_chunked_uploader(File.new(local_file_path, "r"), local_file_size)
51
+ retries = 0
52
+ puts "Uploading..."
53
+ while uploader.offset < uploader.total_size
54
+ begin
55
+ uploader.upload(chunk_size)
56
+ rescue DropboxError => e
57
+ if retries > 10
58
+ puts "- Error uploading, giving up."
59
+ break
60
+ end
61
+ puts "- Error uploading, trying again..."
62
+ retries += 1
64
63
  end
65
- puts "Finishing upload..."
66
- uploader.finish(dropbox_target_path)
67
- puts "Done."
64
+ end
65
+ puts "Finishing upload..."
66
+ uploader.finish(dropbox_target_path)
67
+ puts "Done."
68
68
 
69
69
  end
70
70
 
@@ -15,178 +15,178 @@ APP_KEY = ''
15
15
  APP_SECRET = ''
16
16
 
17
17
  class DropboxCLI
18
- LOGIN_REQUIRED = %w{put get cp mv rm ls mkdir info logout search thumbnail}
19
-
20
- def initialize
21
- if APP_KEY == '' or APP_SECRET == ''
22
- puts "You must set your APP_KEY and APP_SECRET in cli_example.rb!"
23
- puts "Find this in your apps page at https://www.dropbox.com/developers/"
24
- exit
25
- end
26
-
27
- @client = nil
28
- end
29
-
30
- def login
31
- if not @client.nil?
32
- puts "already logged in!"
33
- else
34
- web_auth = DropboxOAuth2FlowNoRedirect.new(APP_KEY, APP_SECRET)
35
- authorize_url = web_auth.start()
36
- puts "1. Go to: #{authorize_url}"
37
- puts "2. Click \"Allow\" (you might have to log in first)."
38
- puts "3. Copy the authorization code."
39
-
40
- print "Enter the authorization code here: "
41
- STDOUT.flush
42
- auth_code = STDIN.gets.strip
43
-
44
- access_token, user_id = web_auth.finish(auth_code)
45
-
46
- @client = DropboxClient.new(access_token)
47
- puts "You are logged in. Your access token is #{access_token}."
48
- end
49
- end
50
-
51
- def command_loop
52
- puts "Enter a command or 'help' or 'exit'"
53
- command_line = ''
54
- while command_line.strip != 'exit'
55
- begin
56
- execute_dropbox_command(command_line)
57
- rescue RuntimeError => e
58
- puts "Command Line Error! #{e.class}: #{e}"
59
- puts e.backtrace
60
- end
61
- print '> '
62
- command_line = gets.strip
63
- end
64
- puts 'goodbye'
65
- exit(0)
66
- end
67
-
68
- def execute_dropbox_command(cmd_line)
69
- command = Shellwords.shellwords cmd_line
70
- method = command.first
71
- if LOGIN_REQUIRED.include? method
72
- if @client
73
- send(method.to_sym, command)
74
- else
75
- puts 'must be logged in; type \'login\' to get started.'
76
- end
77
- elsif ['login', 'help'].include? method
78
- send(method.to_sym)
79
- else
80
- if command.first && !command.first.strip.empty?
81
- puts 'invalid command. type \'help\' to see commands.'
82
- end
83
- end
84
- end
85
-
86
- def logout(command)
87
- @client = nil
88
- puts "You are logged out."
89
- end
90
-
91
- def put(command)
92
- fname = command[1]
93
-
94
- #If the user didn't specifiy the file name, just use the name of the file on disk
95
- if command[2]
96
- new_name = command[2]
97
- else
98
- new_name = File.basename(fname)
99
- end
100
-
101
- if fname && !fname.empty? && File.exists?(fname) && (File.ftype(fname) == 'file') && File.stat(fname).readable?
102
- #This is where we call the the Dropbox Client
103
- pp @client.put_file(new_name, open(fname))
104
- else
105
- puts "couldn't find the file #{ fname }"
106
- end
107
- end
108
-
109
- def get(command)
110
- dest = command[2]
111
- if !command[1] || command[1].empty?
112
- puts "please specify item to get"
113
- elsif !dest || dest.empty?
114
- puts "please specify full local path to dest, i.e. the file to write to"
115
- elsif File.exists?(dest)
116
- puts "error: File #{dest} already exists."
117
- else
118
- src = clean_up(command[1])
119
- out,metadata = @client.get_file_and_metadata('/' + src)
120
- puts "Metadata:"
121
- pp metadata
122
- open(dest, 'w'){|f| f.puts out }
123
- puts "wrote file #{dest}."
124
- end
125
- end
126
-
127
- def mkdir(command)
128
- pp @client.file_create_folder(command[1])
129
- end
130
-
131
- # Example:
132
- # > thumbnail pic1.jpg ~/pic1-local.jpg large
133
- def thumbnail(command)
134
- dest = command[2]
135
- command[3] ||= 'small'
136
- out,metadata = @client.thumbnail_and_metadata(command[1], command[3])
137
- puts "Metadata:"
138
- pp metadata
139
- open(dest, 'w'){|f| f.puts out }
140
- puts "wrote thumbnail#{dest}."
141
- end
142
-
143
- def cp(command)
144
- src = clean_up(command[1])
145
- dest = clean_up(command[2])
146
- pp @client.file_copy(src, dest)
147
- end
148
-
149
- def mv(command)
150
- src = clean_up(command[1])
151
- dest = clean_up(command[2])
152
- pp @client.file_move(src, dest)
153
- end
154
-
155
- def rm(command)
156
- pp @client.file_delete(clean_up(command[1]))
157
- end
158
-
159
- def search(command)
160
- resp = @client.search('/',clean_up(command[1]))
161
-
162
- for item in resp
163
- puts item['path']
164
- end
165
- end
166
-
167
- def info(command)
168
- pp @client.account_info
169
- end
170
-
171
- def ls(command)
172
- command[1] = '/' + clean_up(command[1] || '')
173
- resp = @client.metadata(command[1])
174
-
175
- if resp['contents'].length > 0
176
- for item in resp['contents']
177
- puts item['path']
178
- end
179
- end
180
- end
181
-
182
- def help
183
- puts "commands are: login #{LOGIN_REQUIRED.join(' ')} help exit"
184
- end
185
-
186
- def clean_up(str)
187
- return str.gsub(/^\/+/, '') if str
188
- str
189
- end
18
+ LOGIN_REQUIRED = %w{put get cp mv rm ls mkdir info logout search thumbnail}
19
+
20
+ def initialize
21
+ if APP_KEY == '' or APP_SECRET == ''
22
+ puts "You must set your APP_KEY and APP_SECRET in cli_example.rb!"
23
+ puts "Find this in your apps page at https://www.dropbox.com/developers/"
24
+ exit
25
+ end
26
+
27
+ @client = nil
28
+ end
29
+
30
+ def login
31
+ if not @client.nil?
32
+ puts "already logged in!"
33
+ else
34
+ web_auth = DropboxOAuth2FlowNoRedirect.new(APP_KEY, APP_SECRET)
35
+ authorize_url = web_auth.start()
36
+ puts "1. Go to: #{authorize_url}"
37
+ puts "2. Click \"Allow\" (you might have to log in first)."
38
+ puts "3. Copy the authorization code."
39
+
40
+ print "Enter the authorization code here: "
41
+ STDOUT.flush
42
+ auth_code = STDIN.gets.strip
43
+
44
+ access_token, user_id = web_auth.finish(auth_code)
45
+
46
+ @client = DropboxClient.new(access_token)
47
+ puts "You are logged in. Your access token is #{access_token}."
48
+ end
49
+ end
50
+
51
+ def command_loop
52
+ puts "Enter a command or 'help' or 'exit'"
53
+ command_line = ''
54
+ while command_line.strip != 'exit'
55
+ begin
56
+ execute_dropbox_command(command_line)
57
+ rescue RuntimeError => e
58
+ puts "Command Line Error! #{e.class}: #{e}"
59
+ puts e.backtrace
60
+ end
61
+ print '> '
62
+ command_line = gets.strip
63
+ end
64
+ puts 'goodbye'
65
+ exit(0)
66
+ end
67
+
68
+ def execute_dropbox_command(cmd_line)
69
+ command = Shellwords.shellwords cmd_line
70
+ method = command.first
71
+ if LOGIN_REQUIRED.include? method
72
+ if @client
73
+ send(method.to_sym, command)
74
+ else
75
+ puts 'must be logged in; type \'login\' to get started.'
76
+ end
77
+ elsif ['login', 'help'].include? method
78
+ send(method.to_sym)
79
+ else
80
+ if command.first && !command.first.strip.empty?
81
+ puts 'invalid command. type \'help\' to see commands.'
82
+ end
83
+ end
84
+ end
85
+
86
+ def logout(command)
87
+ @client = nil
88
+ puts "You are logged out."
89
+ end
90
+
91
+ def put(command)
92
+ fname = command[1]
93
+
94
+ #If the user didn't specifiy the file name, just use the name of the file on disk
95
+ if command[2]
96
+ new_name = command[2]
97
+ else
98
+ new_name = File.basename(fname)
99
+ end
100
+
101
+ if fname && !fname.empty? && File.exists?(fname) && (File.ftype(fname) == 'file') && File.stat(fname).readable?
102
+ #This is where we call the the Dropbox Client
103
+ pp @client.put_file(new_name, open(fname))
104
+ else
105
+ puts "couldn't find the file #{ fname }"
106
+ end
107
+ end
108
+
109
+ def get(command)
110
+ dest = command[2]
111
+ if !command[1] || command[1].empty?
112
+ puts "please specify item to get"
113
+ elsif !dest || dest.empty?
114
+ puts "please specify full local path to dest, i.e. the file to write to"
115
+ elsif File.exists?(dest)
116
+ puts "error: File #{dest} already exists."
117
+ else
118
+ src = clean_up(command[1])
119
+ out,metadata = @client.get_file_and_metadata('/' + src)
120
+ puts "Metadata:"
121
+ pp metadata
122
+ open(dest, 'w'){|f| f.write out }
123
+ puts "wrote file #{dest}."
124
+ end
125
+ end
126
+
127
+ def mkdir(command)
128
+ pp @client.file_create_folder(command[1])
129
+ end
130
+
131
+ # Example:
132
+ # > thumbnail pic1.jpg ~/pic1-local.jpg large
133
+ def thumbnail(command)
134
+ dest = command[2]
135
+ command[3] ||= 'small'
136
+ out,metadata = @client.thumbnail_and_metadata(command[1], command[3])
137
+ puts "Metadata:"
138
+ pp metadata
139
+ open(dest, 'w'){|f| f.write out }
140
+ puts "wrote thumbnail#{dest}."
141
+ end
142
+
143
+ def cp(command)
144
+ src = clean_up(command[1])
145
+ dest = clean_up(command[2])
146
+ pp @client.file_copy(src, dest)
147
+ end
148
+
149
+ def mv(command)
150
+ src = clean_up(command[1])
151
+ dest = clean_up(command[2])
152
+ pp @client.file_move(src, dest)
153
+ end
154
+
155
+ def rm(command)
156
+ pp @client.file_delete(clean_up(command[1]))
157
+ end
158
+
159
+ def search(command)
160
+ resp = @client.search('/',clean_up(command[1]))
161
+
162
+ for item in resp
163
+ puts item['path']
164
+ end
165
+ end
166
+
167
+ def info(command)
168
+ pp @client.account_info
169
+ end
170
+
171
+ def ls(command)
172
+ command[1] = '/' + clean_up(command[1] || '')
173
+ resp = @client.metadata(command[1])
174
+
175
+ if resp['contents'].length > 0
176
+ for item in resp['contents']
177
+ puts item['path']
178
+ end
179
+ end
180
+ end
181
+
182
+ def help
183
+ puts "commands are: login #{LOGIN_REQUIRED.join(' ')} help exit"
184
+ end
185
+
186
+ def clean_up(str)
187
+ return str.gsub(/^\/+/, '') if str
188
+ str
189
+ end
190
190
  end
191
191
 
192
192
  cli = DropboxCLI.new