Created
November 28, 2012 09:32
-
-
Save matsadler/4160149 to your computer and use it in GitHub Desktop.
Example of an auto-piplining HTTP client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "net/http/persistent" | |
require "net/http/pipeline" | |
class DeferredProxy < BasicObject | |
def initialize(&generator) | |
@generator = generator | |
end | |
def method_missing(*args, &block) | |
@source ||= @generator.call | |
@source.send(*args, &block) | |
end | |
end | |
# Usage: | |
# client = Client.new("http://www.apple.com") | |
# | |
# queue up some requests: | |
# a = client.get("/") | |
# b = client.get("/mac/") | |
# c = client.get("/ipad/") | |
# | |
# requests aren't made untill one of the responses is actually used. | |
# puts a.code | |
class Client | |
def initialize(url) | |
@uri = URI.parse(url) | |
@http = Net::HTTP::Persistent.new(self.class.name) | |
@requests = [] | |
@responses = {} | |
end | |
def get(path) | |
request = Net::HTTP::Get.new(path, {}) | |
@requests << request | |
DeferredProxy.new do | |
do_requests unless @responses.key?(request) | |
@responses.delete(request) | |
end | |
end | |
private | |
def do_requests | |
requests = @requests.dup | |
@responses.merge!(Hash[requests.zip(@http.pipeline(@uri, @requests))]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment