Forum: Ruby The HTTP Gem 0.5.0: HTTP (and streaming) should be easy!

4131d2f57a0db2a2b4d9a62bd389fd44?d=identicon&s=25 Tony Arcieri (Guest)
on 2013-09-14 20:20
(Received via mailing list)
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: https://github.com/tarcieri/http

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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.