The HTTP Gem provides an easy-to-use JQuery-like chaining API for
constructing HTTP requests. It’s similar to the Requests library from
Python:
gem install http
Find it on Github at: GitHub - httprb/http: HTTP (The Gem! a.k.a. http.rb) - a fast Ruby HTTP client with a chainable API, streaming support, and timeouts
The HTTP Gem is also compatible with Celluloid::IO and can be used to
make
multiple parallel requests easily, similar to what you can do with gems
like Typhoeus.
Here’s how you can write a parallel HTTP fetcher that runs in a single
thread using Celluloid::IO:
– snip –
require ‘celluloid/io’
require ‘http’
class HttpFetcher
include Celluloid::IO
def fetch(url)
HTTP.get(url, socket_class: Celluloid::IO::TCPSocket).response
end
end
fetcher = HttpFetcher.new
urls = %w(http://www.ruby-lang.org/ http://www.rubygems.org/
http://celluloid.io/)
Kick off a bunch of future calls to HttpFetcher to grab the URLs in
parallel
futures = urls.map { |u| [u, fetcher.future.fetch(u)] }
Consume the results as they come in
futures.each do |url, future|
Wait for HttpFetcher#fetch to complete for this request
response = future.value
puts “Got #{url}: #{response.inspect}”
end
– snip –
Full CHANGELOG follows:
- Add query string support
- New response delegator allows HTTP.get(uri).response
- HTTP::Chainable#stream provides a shorter alias for
with_response(:object) - Better string inspect for HTTP::Response
- Curb compatibility layer removed