Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.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 Net::HTTP.get显示进度_Ruby - Fatal编程技术网

Ruby Net::HTTP.get显示进度

Ruby Net::HTTP.get显示进度,ruby,Ruby,我想向用户展示这里发生的事情,比如进度,因为文件的大小可能很大。但只有用户感兴趣的信息,而不是所有调试信息 Net::HTTP.get有这样的能力吗?你可以在这里找到相关信息: 文档中用于此类内容的示例代码段如下: require 'net/http' File.write(file_name, Net::HTTP.get(URI.parse(url))) 我的回答有帮助吗?你的例子只表明发生了一些事情,但这并不是我想要的。我想要%的进度。足够公平。。。编辑以显示来自SF公共数据的JSON实

我想向用户展示这里发生的事情,比如进度,因为文件的大小可能很大。但只有用户感兴趣的信息,而不是所有调试信息


Net::HTTP.get
有这样的能力吗?

你可以在这里找到相关信息:

文档中用于此类内容的示例代码段如下:

require 'net/http'

File.write(file_name, Net::HTTP.get(URI.parse(url)))

我的回答有帮助吗?你的例子只表明发生了一些事情,但这并不是我想要的。我想要%的进度。足够公平。。。编辑以显示来自SF公共数据的JSON实际伪文件的百分比。
require 'net/http'

uri = URI("http://apps.sfgov.org/datafiles/view.php?file=sfgis/citylots.zip")

Net::HTTP.start(uri.host, uri.port) do |http|
  request = Net::HTTP::Get.new uri

  http.request request do |response|
    file_size = response['content-length'].to_i
    amount_downloaded = 0

    open 'large_file', 'wb' do |io| # 'b' opens the file in binary mode 
      response.read_body do |chunk|
        io.write chunk
        amount_downloaded += chunk.size
        puts "%.2f%" % (amount_downloaded.to_f / file_size * 100)
      end
    end
  end
end