Created
December 11, 2025 12:55
-
-
Save havenwood/9c77127732be7ad549282b23354b7aec to your computer and use it in GitHub Desktop.
An example showing streaming a zipped tsv via a pipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'async' | |
| require 'async/http/internet/instance' | |
| require 'csv' | |
| require 'zlib' | |
| def stream_zipped_tsv(url, &) | |
| Sync do | |
| response = Async::HTTP::Internet.get(url) | |
| unless response.success? | |
| response.close | |
| abort "Request failed: #{response.status}" | |
| end | |
| reader, writer = IO.pipe(binmode: true) | |
| task = Async do | |
| response.body.each { |chunk| writer << chunk } | |
| ensure | |
| response.close | |
| writer.close | |
| end | |
| stream = Zlib::GzipReader.new(reader) | |
| CSV.new(stream, col_sep: "\t", headers: true).each(&) | |
| ensure | |
| task&.stop | |
| stream&.close | |
| reader&.close | |
| end | |
| end | |
| url = 'https://raw.githubusercontent.com/Tapad/tapad-sample-gzip/master/file.gz' | |
| stream_zipped_tsv(url) { |row| puts row.headers.first } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment