Sha256: 98887f35995ea5688a3e39ba10373496c6b50f2e0576dca74276d71aee9d2ab3

Contents?: true

Size: 870 Bytes

Versions: 7

Compression:

Stored size: 870 Bytes

Contents

#!/usr/bin/env ruby

require_relative '../../lib/async'

require 'async/http/endpoint'
require 'async/http/server'

require 'async/http/internet'

# To query the web server:
# curl http://localhost:9292/kittens

Async do |parent|
	endpoint = Async::HTTP::Endpoint.parse("http://localhost:9292")
	internet = Async::HTTP::Internet.new
	
	server = Async::HTTP::Server.for(endpoint) do |request|
		if request.path =~ /\/(.*)/
			keyword = $1
			
			response = internet.get("https://www.google.com/search?q=#{keyword}")
			
			count = response.read.scan(keyword).size
			
			Protocol::HTTP::Response[200, [], ["Google found #{count} instance(s) of #{keyword}.\n"]]
		else
			Protocol::HTTP::Response[404, [], []]
		end
	end
	
	tasks = server.run
	
	#while true
	parent.sleep(10)
	parent.reactor.print_hierarchy
	#end
	
	parent.stop # -> Async::Stop
	
	tasks.each(&:stop)
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
async-1.24.1 examples/stop/sleep.rb
async-1.24.0 examples/stop/sleep.rb
async-1.23.0 examples/stop/sleep.rb
async-1.22.2 examples/stop/sleep.rb
async-1.22.1 examples/stop/sleep.rb
async-1.22.0 examples/stop/sleep.rb
async-1.21.0 examples/stop/sleep.rb