File: stream_download.rb

package info (click to toggle)
ruby-httparty 0.24.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 964 kB
  • sloc: ruby: 7,521; xml: 425; sh: 35; makefile: 14
file content (26 lines) | stat: -rw-r--r-- 755 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'pp'

# download file linux-4.6.4.tar.xz without using the memory
response = nil
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 = HTTParty.get(url, stream_body: true) do |fragment|
    if [301, 302].include?(fragment.code)
      print "skip writing for redirect"
    elsif fragment.code == 200
      print "."
      file.write(fragment)
    else
      raise StandardError, "Non-success status code while streaming #{fragment.code}"
    end
  end
end
puts

pp "Success: #{response.success?}"
pp File.stat(filename).inspect
File.unlink(filename)