开发者

Best RoR approach for CURLing

开发者 https://www.devze.com 2023-04-13 08:03 出处:网络
I have the following piece of code in PHP. I\'m looking for the best way to convert it to Ruby. I\'ve looked at a few ap开发者_开发百科proaches, including open-uri and the curb and wrapper curb-fu lib

I have the following piece of code in PHP. I'm looking for the best way to convert it to Ruby. I've looked at a few ap开发者_开发百科proaches, including open-uri and the curb and wrapper curb-fu libraries. open-uri doesn't look very good, but I really like the curb-fu approach. But, I have a feeling using two libraries for this is overkill, there has to be a simpler way to accomplish what this piece of code is doing.

 #Setup connection
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $resource_uri);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_USERPWD, $site_public_key . ":" . $site_private_key);
    curl_setopt($curl, CURLOPT_TIMEOUT, 15);
    curl_setopt($curl, CURLOPT_VERBOSE, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
    curl_setopt($curl, CURLOPT_FAILONERROR, 0);

    #Send request
    $result_json = curl_exec($curl)


Your best bet is to use rest-client. Its api is really cool and lightweight :

result = RestClient::Request.new({:user => "username", :password => "password", 
                      :method => :get, :url => "www.whatever.com"}).execute

or if you don't need auth you can simply do :

result = RestClient.get("http://www.whatever.com")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号