Skip to content

Instantly share code, notes, and snippets.

Created May 16, 2012 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2709886 to your computer and use it in GitHub Desktop.
Save anonymous/2709886 to your computer and use it in GitHub Desktop.
#!/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