tarcieri/http — pull request #5 by bitserf (2012-02-09 08:56:17+00:00)
Title: Ability to inject custom hook just before request and just after response

Not sure how useful this would be to a wider audience, but I wanted some visibility into what was going out onto the wire and what was coming back.

Instead of hacking ugly log statements into Http::Client, and checking if a verbose option was supplied, I thought something a tad more general would be better, so this commit adds the ability to register a block to be invoked just before and just after the request is sent and response is received.

The block is passed request and response objects (currently the Net::HTTP ones, as they give me the most info).

Example usage:

```ruby
def dump_request(request)
puts "> #{request.method} #{request.path}"
request.each_header do |header, value|
puts "> #{header}: #{value}"
end
end

def dump_response(response)
puts
puts "< HTTP/#{response.http_version} #{response.code} #{response.msg}"
response.each_header do |header, value|
puts "< #{header}: #{value}"
end
puts
end

def login(uri, access_code, password)
form = {ac: access_code, pw: password}
Http.with('User-Agent' => USER_AGENT)
.on(:request) {|req| dump_request(req)}
.on(:response) {|resp| dump_response(resp)}
.post(uri.to_s, :form => form)
end
```

Cheers for the awesome lib.