I'm working with Curb, a ruby wrapper around cURL.
I want to start by saying that I know methods such as method missing can accept arbitrary number args and a block if defined like so:
def method_missing(meth, *args, &block); ... end
*args
is an array of parameters.
Now, I'm trying to refactor my calls to the HTTP GET, POST, etc methods, by doing the following with Curb:
req = Curl::Easy.send("http_#{method.to_s}", *args) do |curl| ... end
where args
could be either 1 parameter or 2 parameters, which I'm trying to define as follows:
args = [url]
args << data if data
But I get an error when I invoke the Curl::Easy.send
line.
both block arg and actual block given
here are some examples of the methods I'm trying to access from (https://github.com/taf2/curb/blob/master/lib/curl/easy.rb):
def http_get(*args)
c = Curl::Easy.new *args
yield c if block_given?
c.http_get
c
end
def http_put(url, data)
c = Curl::Easy.new url
yield c if block_given?
c.http_put data
c
end
def http_post(*args)
url = args.shift
c = Curl::Easy.new url
yield c if block_given?
c.http_post *args
c
end
def http_delete(*args)
c = Curl::Easy.new *args
yield c if block_given?
c.http_delete
c
end
all of them are set up to take arbitrary number of arguments, except put.
But, really, for http_get
I only need to pass a URL (with query params in the URL). so, I only want to pass one parametr for http_get
, and 2 params for the others.