mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
Simplify the streamdownload example
This commit is contained in:
parent
b05ffba1cd
commit
df9aa97fab
1 changed files with 7 additions and 11 deletions
|
@ -2,23 +2,19 @@ dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
|||
require File.join(dir, 'httparty')
|
||||
require 'pp'
|
||||
|
||||
class StreamDownload
|
||||
include HTTParty
|
||||
|
||||
base_uri 'https://cdn.kernel.org/pub/linux/kernel/v4.x'
|
||||
end
|
||||
|
||||
# download file linux-4.6.4.tar.xz without using the memory
|
||||
response = nil
|
||||
filename = 'linux-4.6.4.tar.xz'
|
||||
filename = "linux-4.6.4.tar.xz"
|
||||
url = "https://cdn.kernel.org/pub/linux/kernel/v4.x/#{filename}"
|
||||
|
||||
File.open(filename, 'w') do |file|
|
||||
response = StreamDownload.get('/linux-4.6.4.tar.xz', stream_body: true) do |fragment|
|
||||
file.binmode
|
||||
File.open(filename, "w") do |file|
|
||||
response = HTTParty.get(url, stream_body: true) do |fragment|
|
||||
print "."
|
||||
file.write(fragment)
|
||||
end
|
||||
end
|
||||
puts
|
||||
|
||||
pp "Success: #{response.success?}"
|
||||
|
||||
pp File.stat(filename).inspect
|
||||
File.unlink(filename)
|
||||
|
|
Loading…
Add table
Reference in a new issue