gdi / curb-fu

Simple, yet powerful wrapper for taf2's curb (libcurl) library
Other
57 stars 9 forks source link

curb-fu - easy-to-use wrapper around curb - the ruby wrapper around libcurl

Curb can be found at http://github.com/taf2/curb

License

This gem is released under the terms of the Ruby license. See the LICENSE file for details.

Troubleshooting

If you are POSTing data and curb seems to be locking up, try posting it with an explicit 'Expect: 100-continue' header.

You can set this per-request, e.g.

CurbFu.post({:host => 'example.com', :headers => { "Expect" => "100-continue" }}, { "data" => "here" })

or you can configure it as a global header, e.g.

CurbFu.global_headers = { "Expect" => "100-continue" }
# ... then make your requests as normal

however you feel best.

Prerequisites

Installation

$ gem install curb-fu --source http://gems.github.com

Or, if you have the source:

$ cd <source-dir>
$ rake gem
$ gem install pkg/

Examples

Urls can be requested using hashes of options or strings. The GET, POST, PUT, and DELETE methods are supported through their respective methods on CurbFu and CurbFu::Request.

String Examples

response = CurbFu.get('http://slashdot.org')
puts response.body

response = CurbFu.post('http://example.com/some/resource', { :color => 'red', :shape => 'sphere' })
puts response.body unless response.success?

If you need to pass custom headers, you can still pass a string URL with :url :

response = CurbFu.post(:url => 'http://example.com/some/resource', :headers => {'Content-Type' => 'application/xml'})

Hash Examples

response = CurbFu.get(:host => 'github.com', :path => '/gdi/curb-fu')
puts response.body

response = CurbFu.post({:host => 'example.com', :path => '/some/resource'}, { :color => 'red', :shape => 'sphere' })
puts response.body unless response.success?

if you need https:

response = CurbFu.post({:host => 'example.com', :path => '/some/resource', :protocol => "https"}, { :color => 'red', :shape => 'sphere' })

Cookies; changes as of 0.6.1

if you want to send a cookie, previous to 0.6.1 you have to pass a block to the HTTP verb method like so:

response = CurbFu.get("http://myhost.com") do |curb|
  curb.cookies = "SekretToken=123234234235;"
end

As of 0.6.1 one can set the cookies either as an optional final parameter or via a hash, e.g.:

response = CurbFu.get("http://myhost.com", { :param => "value" }, "SekretToken=123234;")
# or with a hash:
response = CurbFu.get({ :host => "http://myhost", :cookies => "SekretToken=1234;" })

etc.

Following Redirects, and other advanced topics

you can pass a block to the CurbFu methods, we will yield the Curl::Easy object to this block to allow you to reach as far into the Curb guts as you want. This is particularly useful if you want to follow redirects:

resp = CurbFu.get('http://google.com') { |curb| curb.follow_location = true }

n.b. we would happily entertain pull requests to add some sugar to common configuration items.

Have fun!

Contributing

Contributors

Original Authorship