Search code examples
rubyrubygemscurb

How to set limit-rate option in Curb gem?


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).


Solution

  • 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