0

I'd like to set --limit-rate option for downloads done by Curb gem (ruby interface to curl).

In curl:

curl --limit-rate 10K http://server/large_file.rar

For downloads by Curb I have this code (plus progressbar, but that's not relevant to this question):

require 'rubygems'
require 'curb'

request = 'http://server/large_file.rar'    
filename = 'large_file.rar'

f = open(filename, 'wb')

c = Curl::Easy.new(request) do |curl|    
  curl.on_body { |d| f << d; d.length }
end

c.perform

f.close

How do I set --limit-rate option in this script? As long as I can tell, there's no easy way (I've already read rdoc and done some googling).

Lukas Stejskal
  • 2,542
  • 19
  • 30

1 Answers1

0

You would do this by setting CURLOPT_MAX_RECV_SPEED_LARGE in libcurl. Through the curb API, you would do:

c = Curl::Easy.new(request) do |curl|
  curl.set(:max_recv_speed_large, download_limit)
  curl.on_body { |d| f << d; d.length }
end

Where download_limit is an integer for the maximum download rate in bytes per second.

For more info: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLOPTMAXRECVSPEEDLARGE

andrewle
  • 1,731
  • 13
  • 11
  • Doesn't work for me :(, it throws: "undefined method 'set' for Curl::Easy". Tried both Ruby 1.8.7 and 1.9.2 (both on RVM), using the latest curb version: 0.7.15. – Lukas Stejskal Aug 31 '11 at 20:59
  • 1
    I don't think it is possible to set that option without modifying curb's ruby extension C source. See: https://github.com/taf2/curb/issues/49 – mwolfetech Sep 01 '11 at 01:00