Created
March 7, 2014 18:43
-
-
Save krakatoa/0cb697a805e554dec3a5 to your computer and use it in GitHub Desktop.
Celluloid-IO + HTTP gem over SSL
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 '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