|
2 | 2 | # frozen_string_literal: true |
3 | 3 |
|
4 | 4 | # Released under the MIT License. |
5 | | -# Copyright, 2020-2025, by Samuel Williams. |
| 5 | +# Copyright, 2020-2026, by Samuel Williams. |
6 | 6 |
|
7 | 7 | require "async" |
8 | 8 | require "async/clock" |
|
20 | 20 | headers = {"user-agent" => "curl/7.69.1", "accept" => "*/*"} |
21 | 21 |
|
22 | 22 | file = File.open("products.csv", "w") |
23 | | - Console.info(self) {"Saving download to #{Dir.pwd}"} |
| 23 | + Console.info(self){"Saving download to #{Dir.pwd}"} |
24 | 24 |
|
25 | 25 | begin |
26 | 26 | response = client.head(endpoint.path, headers) |
|
39 | 39 | response&.close |
40 | 40 | end |
41 | 41 |
|
42 | | - Console.info(self) {"Content length: #{content_length/(1024**2)}MiB"} |
| 42 | + Console.info(self){"Content length: #{content_length/(1024**2)}MiB"} |
43 | 43 |
|
44 | 44 | parts = [] |
45 | 45 | offset = 0 |
|
56 | 56 | offset += chunk_size |
57 | 57 | end |
58 | 58 |
|
59 | | - Console.info(self) {"Breaking download into #{parts.size} parts..."} |
| 59 | + Console.info(self){"Breaking download into #{parts.size} parts..."} |
60 | 60 |
|
61 | 61 | semaphore = Async::Semaphore.new(8) |
62 | 62 | barrier = Async::Barrier.new(parent: semaphore) |
|
65 | 65 | barrier.async do |
66 | 66 | part = parts.shift |
67 | 67 |
|
68 | | - Console.info(self) {"Issuing range request range: bytes=#{part.min}-#{part.max}"} |
| 68 | + Console.info(self){"Issuing range request range: bytes=#{part.min}-#{part.max}"} |
69 | 69 |
|
70 | 70 | response = client.get(endpoint.path, [ |
71 | 71 | ["range", "bytes=#{part.min}-#{part.max-1}"], |
72 | 72 | *headers |
73 | 73 | ]) |
74 | 74 |
|
75 | 75 | if response.success? |
76 | | - Console.info(self) {"Got response: #{response}... writing data for #{part}."} |
| 76 | + Console.info(self){"Got response: #{response}... writing data for #{part}."} |
77 | 77 | written = file.pwrite(response.read, part.min) |
78 | 78 |
|
79 | 79 | amount += written |
80 | 80 |
|
81 | 81 | duration = Async::Clock.now - start_time |
82 | | - Console.info(self) {"Rate: #{((amount.to_f/(1024**2))/duration).round(2)}MiB/s"} |
| 82 | + Console.info(self){"Rate: #{((amount.to_f/(1024**2))/duration).round(2)}MiB/s"} |
83 | 83 | end |
84 | 84 | end |
85 | 85 | end |
|
0 commit comments