Reputation: 1221
How would I emulate the behavior of curl -r (range) in a ruby script without using backticks or other methods to run the curl binary?
I know how to start from the beginning of a file and save a file in chunks, but how would I start at a specific portion of the remote file then download for X bytes?
Say the file was 1000 bytes long, and I wanted to start at byte 60 and download 20 bytes from that point (finishing at 80). How would this be done?
Is there a gem wrapper around curl which is cross platform, or another gem that provides this support? Or is this possible with the standard open-uri or net::http libraries?
Thanks!
Upvotes: 0
Views: 112
Reputation: 106147
You can do this with any library that lets you set request headers (all curl -r
does is set the Range
header), which should be pretty much any HTTP library. Net::HTTP, for its part, has a set_range
convenience method that takes as arguments either a single Range object (e.g. 60...80
) or a start index and length:
require "net/http"
require "uri"
url = URI.parse("http://example.com/foo")
req = Net::HTTP::Get.new(url.path)
req.set_range(60, 20)
res = Net::HTTP.new(url.host, url.port).start do |http|
http.request(req)
end
puts res.body
Upvotes: 1