Excon: a sane alternative to Net::HTTP

Here’s how I use Net::HTTP:

# It rubs the lotion on its skin. It does this whenever it's told.
url = URI.parse('http://www.google.com/')

# It rubs the lotion on its skin or else it gets the hose again.
# Ruff! Yes it will, precious, it will get the hose.
File.open('result.txt', 'w') {|f|

  # It places the lotion in the basket.
  res = Net::HTTP.start(url.host, url.port) {|http|

    # Now it places the lotion in the basket.
    http.get('/') do |str|

      # PUT THE FUCKING LOTION IN THE BASKET!
      f.write str
    end
  }
}

Excon is a pleasant alternative to Net::HTTP written by Wesley Beary (@geemus). It has a simple API:

# get
Excon.get("http://localhost:4567/").body

# get with headers
Excon.get(
  "http://localhost:4567/",
  :headers => {"User-Agent", "Excon"})

# get chunked
Excon.get("http://localhost:4567/") do |chuck|
  puts chunk
end

# keep-alive
@connection = Excon.new("http://localhost:4567")
@connection.request(:method => "GET", :path => "/foo")
@connection.request(:method => "GET", :path => "/bar")

Mocking is built in:

Excon.mock = true

# any request returns Hello
Excon.stub({}, {:body => "Hello"})

# be more specific about the request
Excon.stub({:path => "/foo"}, {:body => "Foo"})

Though it doesn’t support posting form data from a simple hash or multipart file uploads:

puts Excon.post("http://localhost:4567/", :body => "foo=bar&wam=bam")

I haven’t benchmarked anything, but it has performed well for me. Also, it’s compatible with Windows out of the box, an issue with curb.

Give it a try.