Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Ruby API从Dropbox下载_Ruby_Dropbox_Dropbox Api - Fatal编程技术网

使用Ruby API从Dropbox下载

使用Ruby API从Dropbox下载,ruby,dropbox,dropbox-api,Ruby,Dropbox,Dropbox Api,我目前正在命令行Dropbox应用程序中使用gem 现在,我正在重新考虑我下载文件的方式。代码如下所示: begin contents = env['dropbox-client'].download env['download_file_name'] rescue Dropbox::API::Error::NotFound => e say "File Not Found! Could not download", :red exit 1 rescue Dropbox::API:

我目前正在命令行Dropbox应用程序中使用gem

现在,我正在重新考虑我下载文件的方式。代码如下所示:

begin
contents = env['dropbox-client'].download env['download_file_name']
rescue Dropbox::API::Error::NotFound => e
  say "File Not Found! Could not download", :red
  exit 1
rescue Dropbox::API::Error => e
  say "Connection to Dropbox failed (#{e})", :red
  exit 1
end

File.open(env['download_file_name'], 'w') {|f| f.write(contents) }
我觉得这段代码可以改进

我的基本问题是:

是否有一种更细微的方法从
contents
中的dropbox中获取API请求的主体,并使用它来创建一个比我在这里做的更好的文件

File.open(env['download_file_name'], 'w') {|f| f.write(contents) }
可以写得更清楚:

File.write(env['download_file_name'], contents)
并尝试以下未经测试的代码:

rescue Dropbox::API::Error::NotFound, Dropbox::API::Error => e
  msg = if (e == Dropbox::API::Error::NotFound)
    "File Not Found! Could not download" 
  else
    "Connection to Dropbox failed (#{e})"
  end

  say msg, :red
  exit 1
end

(我赶时间,但看起来不错。)

嗨!代码更简洁,但我更想知道是否有更好的方法获取http主体并将其写入文件。如果File.write是我使用的最好的方法:)你将很难找到比
File.write更简单和/或更好的文件写入方法。