Created
May 16, 2012 12:11
-
-
Save anonymous/2709886 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
require 'eventmachine' | |
require 'em-http-request' | |
require 'yajl' | |
require 'pp' | |
require_relative File.join('..', 'etc', 'config.rb') | |
module MassiveProxy | |
class ProxyPool | |
include EM::Deferrable | |
attr_accessor :proxies | |
def self.get(lmax = nil) | |
pool = MassiveProxy::ProxyPool.new | |
req_opts = { | |
:query => { :hp => true, :lmax => lmax } | |
} | |
req = EM::HttpRequest.new("#{PROXIES_BASE_URL}/proxies").get req_opts | |
req.callback do |resp| | |
response = resp.response | |
json = StringIO.new response | |
parser = Yajl::Parser.new | |
pool.proxies = parser.parse(json) | |
puts pool.proxies.class # -> returns Array | |
end | |
req.errback do |resp| | |
$stderr.print "Fetching proxies failed.\n" | |
EM.stop | |
end | |
end | |
end | |
end | |
EM::run do | |
pool = MassiveProxy::ProxyPool.get(100) | |
pool.callback do |p| | |
puts p.class # -> returns EventMachine::HttpClient | |
EM.stop | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment