lib/dbox/api.rb in dbox-0.1.0 vs lib/dbox/api.rb in dbox-0.2.0

- old
+ new

@@ -1,11 +1,11 @@ module Dbox class API + include Loggable + def self.authorize - puts conf.inspect auth = Authenticator.new(conf) - puts auth.inspect authorize_url = auth.get_request_token puts "Please visit the following URL in your browser, log into Dropbox, and authorize the app you created.\n\n#{authorize_url}\n\nWhen you have done so, press [ENTER] to continue." STDIN.readline res = auth.get_access_token puts "export DROPBOX_AUTH_KEY=#{res.token}" @@ -41,42 +41,53 @@ @client = DropboxClient.new(@conf["server"], @conf["content_server"], @conf["port"], @auth) end def metadata(path = "/") path = escape_path(path) - puts "[api] fetching metadata for #{path}" - @client.metadata(@conf["root"], path) + log.debug "Fetching metadata for #{path}" + begin + case res = @client.metadata(@conf["root"], path) + when Hash + res + when Net::HTTPNotFound + raise "Remote path does not exist" + else + raise "Unexpected result from GET /metadata: #{res.inspect}" + end + rescue DropboxError => e + raise "Server error -- might be a hiccup, please try your request again (#{e.message})" + end end def create_dir(path) path = escape_path(path) - puts "[api] creating #{path}" + log.info "Creating #{path}" @client.file_create_folder(@conf["root"], path) end def delete_dir(path) path = escape_path(path) - puts "[api] deleting #{path}" + log.info "Deleting #{path}" @client.file_delete(@conf["root"], path) end def get_file(path) path = escape_path(path) - puts "[api] downloading #{path}" + log.info "Downloading #{path}" @client.get_file(@conf["root"], path) end def put_file(path, file_obj) path = escape_path(path) - puts "[api] uploading #{path}" + log.info "Uploading #{path}" dir = File.dirname(path) name = File.basename(path) @client.put_file(@conf["root"], dir, name, file_obj) end def delete_file(path) path = escape_path(path) - puts "[api] deleting #{path}" + log.info "Deleting #{path}" @client.file_delete(@conf["root"], path) end def escape_path(path) URI.escape(path)