examples/cli_example.rb in dropbox-sdk-1.6.4 vs examples/cli_example.rb in dropbox-sdk-1.6.5

- old
+ new

@@ -13,181 +13,181 @@ # Find this at https://www.dropbox.com/developers APP_KEY = '' APP_SECRET = '' class DropboxCLI - LOGIN_REQUIRED = %w{put get cp mv rm ls mkdir info logout search thumbnail} + LOGIN_REQUIRED = %w{put get cp mv rm ls mkdir info logout search thumbnail} - def initialize - if APP_KEY == '' or APP_SECRET == '' - puts "You must set your APP_KEY and APP_SECRET in cli_example.rb!" - puts "Find this in your apps page at https://www.dropbox.com/developers/" - exit - end - - @client = nil + def initialize + if APP_KEY == '' or APP_SECRET == '' + puts "You must set your APP_KEY and APP_SECRET in cli_example.rb!" + puts "Find this in your apps page at https://www.dropbox.com/developers/" + exit end - def login - if not @client.nil? - puts "already logged in!" - else - web_auth = DropboxOAuth2FlowNoRedirect.new(APP_KEY, APP_SECRET) - authorize_url = web_auth.start() - puts "1. Go to: #{authorize_url}" - puts "2. Click \"Allow\" (you might have to log in first)." - puts "3. Copy the authorization code." + @client = nil + end - print "Enter the authorization code here: " - STDOUT.flush - auth_code = STDIN.gets.strip + def login + if not @client.nil? + puts "already logged in!" + else + web_auth = DropboxOAuth2FlowNoRedirect.new(APP_KEY, APP_SECRET) + authorize_url = web_auth.start() + puts "1. Go to: #{authorize_url}" + puts "2. Click \"Allow\" (you might have to log in first)." + puts "3. Copy the authorization code." - access_token, user_id = web_auth.finish(auth_code) + print "Enter the authorization code here: " + STDOUT.flush + auth_code = STDIN.gets.strip - @client = DropboxClient.new(access_token) - puts "You are logged in. Your access token is #{access_token}." - end - end + access_token, user_id = web_auth.finish(auth_code) - def command_loop - puts "Enter a command or 'help' or 'exit'" - command_line = '' - while command_line.strip != 'exit' - begin - execute_dropbox_command(command_line) - rescue RuntimeError => e - puts "Command Line Error! #{e.class}: #{e}" - puts e.backtrace - end - print '> ' - command_line = gets.strip - end - puts 'goodbye' - exit(0) + @client = DropboxClient.new(access_token) + puts "You are logged in. Your access token is #{access_token}." end + end - def execute_dropbox_command(cmd_line) - command = Shellwords.shellwords cmd_line - method = command.first - if LOGIN_REQUIRED.include? method - if @client - send(method.to_sym, command) - else - puts 'must be logged in; type \'login\' to get started.' - end - elsif ['login', 'help'].include? method - send(method.to_sym) - else - if command.first && !command.first.strip.empty? - puts 'invalid command. type \'help\' to see commands.' - end - end + def command_loop + puts "Enter a command or 'help' or 'exit'" + command_line = '' + while command_line.strip != 'exit' + begin + execute_dropbox_command(command_line) + rescue RuntimeError => e + puts "Command Line Error! #{e.class}: #{e}" + puts e.backtrace + end + print '> ' + command_line = gets.strip end + puts 'goodbye' + exit(0) + end - def logout(command) - @client = nil - puts "You are logged out." + def execute_dropbox_command(cmd_line) + command = Shellwords.shellwords cmd_line + method = command.first + if LOGIN_REQUIRED.include? method + if @client + send(method.to_sym, command) + else + puts 'must be logged in; type \'login\' to get started.' + end + elsif ['login', 'help'].include? method + send(method.to_sym) + else + if command.first && !command.first.strip.empty? + puts 'invalid command. type \'help\' to see commands.' + end end + end - def put(command) - fname = command[1] + def logout(command) + @client = nil + puts "You are logged out." + end - #If the user didn't specifiy the file name, just use the name of the file on disk - if command[2] - new_name = command[2] - else - new_name = File.basename(fname) - end + def put(command) + fname = command[1] - if fname && !fname.empty? && File.exists?(fname) && (File.ftype(fname) == 'file') && File.stat(fname).readable? - #This is where we call the the Dropbox Client - pp @client.put_file(new_name, open(fname)) - else - puts "couldn't find the file #{ fname }" - end + #If the user didn't specifiy the file name, just use the name of the file on disk + if command[2] + new_name = command[2] + else + new_name = File.basename(fname) end - def get(command) - dest = command[2] - if !command[1] || command[1].empty? - puts "please specify item to get" - elsif !dest || dest.empty? - puts "please specify full local path to dest, i.e. the file to write to" - elsif File.exists?(dest) - puts "error: File #{dest} already exists." - else - src = clean_up(command[1]) - out,metadata = @client.get_file_and_metadata('/' + src) - puts "Metadata:" - pp metadata - open(dest, 'w'){|f| f.puts out } - puts "wrote file #{dest}." - end + if fname && !fname.empty? && File.exists?(fname) && (File.ftype(fname) == 'file') && File.stat(fname).readable? + #This is where we call the the Dropbox Client + pp @client.put_file(new_name, open(fname)) + else + puts "couldn't find the file #{ fname }" end + end - def mkdir(command) - pp @client.file_create_folder(command[1]) + def get(command) + dest = command[2] + if !command[1] || command[1].empty? + puts "please specify item to get" + elsif !dest || dest.empty? + puts "please specify full local path to dest, i.e. the file to write to" + elsif File.exists?(dest) + puts "error: File #{dest} already exists." + else + src = clean_up(command[1]) + out,metadata = @client.get_file_and_metadata('/' + src) + puts "Metadata:" + pp metadata + open(dest, 'w'){|f| f.write out } + puts "wrote file #{dest}." end + end - # Example: - # > thumbnail pic1.jpg ~/pic1-local.jpg large - def thumbnail(command) - dest = command[2] - command[3] ||= 'small' - out,metadata = @client.thumbnail_and_metadata(command[1], command[3]) - puts "Metadata:" - pp metadata - open(dest, 'w'){|f| f.puts out } - puts "wrote thumbnail#{dest}." - end + def mkdir(command) + pp @client.file_create_folder(command[1]) + end - def cp(command) - src = clean_up(command[1]) - dest = clean_up(command[2]) - pp @client.file_copy(src, dest) - end + # Example: + # > thumbnail pic1.jpg ~/pic1-local.jpg large + def thumbnail(command) + dest = command[2] + command[3] ||= 'small' + out,metadata = @client.thumbnail_and_metadata(command[1], command[3]) + puts "Metadata:" + pp metadata + open(dest, 'w'){|f| f.write out } + puts "wrote thumbnail#{dest}." + end - def mv(command) - src = clean_up(command[1]) - dest = clean_up(command[2]) - pp @client.file_move(src, dest) - end + def cp(command) + src = clean_up(command[1]) + dest = clean_up(command[2]) + pp @client.file_copy(src, dest) + end - def rm(command) - pp @client.file_delete(clean_up(command[1])) - end + def mv(command) + src = clean_up(command[1]) + dest = clean_up(command[2]) + pp @client.file_move(src, dest) + end - def search(command) - resp = @client.search('/',clean_up(command[1])) + def rm(command) + pp @client.file_delete(clean_up(command[1])) + end - for item in resp - puts item['path'] - end - end + def search(command) + resp = @client.search('/',clean_up(command[1])) - def info(command) - pp @client.account_info + for item in resp + puts item['path'] end + end - def ls(command) - command[1] = '/' + clean_up(command[1] || '') - resp = @client.metadata(command[1]) + def info(command) + pp @client.account_info + end - if resp['contents'].length > 0 - for item in resp['contents'] - puts item['path'] - end - end - end + def ls(command) + command[1] = '/' + clean_up(command[1] || '') + resp = @client.metadata(command[1]) - def help - puts "commands are: login #{LOGIN_REQUIRED.join(' ')} help exit" + if resp['contents'].length > 0 + for item in resp['contents'] + puts item['path'] + end end + end - def clean_up(str) - return str.gsub(/^\/+/, '') if str - str - end + def help + puts "commands are: login #{LOGIN_REQUIRED.join(' ')} help exit" + end + + def clean_up(str) + return str.gsub(/^\/+/, '') if str + str + end end cli = DropboxCLI.new cli.command_loop