Skip to content

Instantly share code, notes, and snippets.

@krakatoa
Created March 7, 2014 18:43
Show Gist options
  • Save krakatoa/0cb697a805e554dec3a5 to your computer and use it in GitHub Desktop.
Save krakatoa/0cb697a805e554dec3a5 to your computer and use it in GitHub Desktop.
Celluloid-IO + HTTP gem over SSL
require 'http'
require 'celluloid/io'
class HttpFetcher
include Celluloid::IO
def fetch(url)
context = ctx
# context = nil if rand(2) == 0
HTTP.get(url, socket_class: Celluloid::IO::TCPSocket, ssl_socket_class: Celluloid::IO::SSLSocket, :ssl_context => context)
end
def ctx
return @ctx if @ctx
@ctx = OpenSSL::SSL::SSLContext.new
@ctx.cert = OpenSSL::X509::Certificate.new(File.read("smrt.crt"))
@ctx.key = OpenSSL::PKey::RSA.new(File.read("smrt.key"))
@ctx
end
end
fetcher = HttpFetcher.new
urls = %w{ https://localhost:443 https://localhost:443 }
future = fetcher.future.fetch(urls[0])
puts future.value
#futures = urls.map { |u| [u, fetcher.future.fetch(u)] }
#
#futures.each do |url, future|
# response = future.value
# puts "Got #{url}: #{response.inspect}\n"
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment