Sha256: 204981eb2479a04540019aeb601021d8ef2b30f1a161101af68eef796c38e292

Contents?: true

Size: 1.67 KB

Versions: 17

Compression:

Stored size: 1.67 KB

Contents

#!/usr/bin/env ruby

require "async"
require "async/clock"
require "protocol/http/middleware"
require_relative "../../lib/async/http"

URL = "https://www.google.com/search"
ENDPOINT = Async::HTTP::Endpoint.parse(URL)

# Console.logger.enable(Async::IO::Stream, Console::Logger::DEBUG)

class Google < Protocol::HTTP::Middleware
	def search(term)
		Async.logger.info(self) {"Searching for #{term}..."}
		
		self.get("/search?q=#{term}", {"user-agent" => "Hi Google!"})
	end
end

terms = %w{thoughtful fear size payment lethal modern recognise face morning sulky mountainous contain science snow uncle skirt truthful door travel snails closed rotten halting creator teeny-tiny beautiful cherries unruly level follow strip team things suggest pretty warm end cannon bad pig consider airport strengthen youthful fog three walk furry pickle moaning fax book ruddy sigh plate cakes shame stem faulty bushes dislike train sleet one colour behavior bitter suit count loutish squeak learn watery orange idiotic seat wholesale omniscient nostalgic arithmetic instruct committee puffy program cream cake whistle rely encourage war flagrant amusing fluffy prick utter wacky occur daily son check}

if count = ENV.fetch('COUNT', 20)&.to_i
	terms = terms.first(count)
end

Async do |task|
	client = Async::HTTP::Client.new(ENDPOINT)
	google = Google.new(client)
	
	google.search("null").finish
	
	duration = Async::Clock.measure do
		counts = terms.map do |term|
			task.async do
				response = google.search(term)
				[term, response.read.scan(term).count]
			end
		end.map(&:wait).to_h
		
		Async.logger.info(self, name: 'counts') {counts}
	end
	
	Async.logger.info(self, name: 'duration') {duration}
ensure
	google.close
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
async-http-0.52.4 examples/google/search.rb
async-http-0.52.3 examples/google/search.rb
async-http-0.52.2 examples/google/search.rb
async-http-0.52.1 examples/google/search.rb
async-http-0.52.0 examples/google/search.rb
async-http-0.51.6 examples/google/search.rb
async-http-0.51.5 examples/google/search.rb
async-http-0.51.4 examples/google/search.rb
async-http-0.51.3 examples/google/search.rb
async-http-0.51.2 examples/google/search.rb
async-http-0.51.1 examples/google/search.rb
async-http-0.51.0 examples/google/search.rb
async-http-0.50.13 examples/google/search.rb
async-http-0.50.12 examples/google/search.rb
async-http-0.50.11 examples/google/search.rb
async-http-0.50.10 examples/google/search.rb
async-http-0.50.9 examples/google/search.rb