Last active
December 18, 2015 10:29
-
-
Save lmars/5768634 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
require 'net/http' | |
require 'uri' | |
random_url = URI('http://www.random.org/integers/?num=1&min=1&max=6&col=1&base=10&format=plain&rnd=new') | |
ints = [] | |
resp = Net::HTTP.get_response(random_url) | |
ints << resp.body.strip | |
puts "Random integers: #{ints.inspect}" | |
resp = Net::HTTP.get_response(random_url) | |
ints << resp.body.strip | |
puts "Random integers: #{ints.inspect}" | |
resp = Net::HTTP.get_response(random_url) | |
ints << resp.body.strip | |
puts "Random integers: #{ints.inspect}" | |
resp = Net::HTTP.get_response(random_url) | |
ints << resp.body.strip | |
puts "Random integers: #{ints.inspect}" | |
resp = Net::HTTP.get_response(random_url) | |
ints << resp.body.strip | |
puts "Random integers: #{ints.inspect}" |
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 'bundler/setup' | |
require 'eventmachine' | |
require 'em-http-request' | |
random_url = URI('http://www.random.org/integers/?num=1&min=1&max=6&col=1&base=10&format=plain&rnd=new') | |
ints = [] | |
EM.run do | |
http = EM::HttpRequest.new(random_url).get.callback do | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
http = EM::HttpRequest.new(random_url).get.callback do | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
http = EM::HttpRequest.new(random_url).get.callback do | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
http = EM::HttpRequest.new(random_url).get.callback do | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
http = EM::HttpRequest.new(random_url).get.callback do | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
puts 'stopping' | |
EM.stop | |
end | |
end | |
end | |
end | |
end | |
end |
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 'bundler/setup' | |
require 'eventmachine' | |
require 'em-http-request' | |
random_url = URI('http://www.random.org/integers/?num=1&min=1&max=6&col=1&base=10&format=plain&rnd=new') | |
ints = [] | |
EM.run do | |
EM::HttpRequest.new(random_url).promise_get.then { |http| | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
EM::HttpRequest.new(random_url).promise_get | |
}.then { |http| | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
EM::HttpRequest.new(random_url).promise_get | |
}.then { |http| | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
EM::HttpRequest.new(random_url).promise_get | |
}.then { |http| | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
EM::HttpRequest.new(random_url).promise_get | |
}.then { |http| | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
EM.stop | |
} | |
end |
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 'fiber' | |
require 'bundler/setup' | |
require 'eventmachine' | |
require 'em-http-request' | |
random_url = URI('http://www.random.org/integers/?num=1&min=1&max=6&col=1&base=10&format=plain&rnd=new') | |
ints = [] | |
def fiber_get(url) | |
fiber = Fiber.current | |
http = EM::HttpRequest.new(url).get | |
http.callback { fiber.resume(http) } | |
return Fiber.yield | |
end | |
EM.run do | |
Fiber.new { | |
http = fiber_get(random_url) | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
http = fiber_get(random_url) | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
http = fiber_get(random_url) | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
http = fiber_get(random_url) | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
http = fiber_get(random_url) | |
ints << http.response.strip | |
puts "Random integers: #{ints.inspect}" | |
EM.stop | |
}.resume | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment