Class: Async::HTTP::Internet

Inherits:
Object
  • Object
show all
Extended by:
Thread::Local
Defined in:
lib/async/http/internet.rb,
lib/async/http/internet/instance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Internet

Returns a new instance of Internet.



33
34
35
36
# File 'lib/async/http/internet.rb', line 33

def initialize(**options)
	@clients = Hash.new
	@options = options
end

Instance Attribute Details

#clientsObject

A cache of clients.



40
41
42
# File 'lib/async/http/internet.rb', line 40

def clients
  @clients
end

Instance Method Details

#call(method, url, headers = nil, body = nil) ⇒ Object

Make a request to the internet with the given ‘method` and `url`.

If you provide non-frozen headers, they may be mutated.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/async/http/internet.rb', line 58

def call(method, url, headers = nil, body = nil)
	endpoint = Endpoint.parse(url)
	client = self.client_for(endpoint)
	
	body = Body::Buffered.wrap(body)
	headers = ::Protocol::HTTP::Headers[headers]
	
	request = ::Protocol::HTTP::Request.new(endpoint.scheme, endpoint.authority, method, endpoint.path, nil, headers, body)
	
	return client.call(request)
end

#client_for(endpoint) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/async/http/internet.rb', line 42

def client_for(endpoint)
	key = host_key(endpoint)
	
	@clients.fetch(key) do
		@clients[key] = self.make_client(endpoint)
	end
end

#closeObject



70
71
72
73
74
75
76
# File 'lib/async/http/internet.rb', line 70

def close
	# The order of operations here is to avoid a race condition between iterating over clients (#close may yield) and creating new clients.
	clients = @clients.values
	@clients.clear
	
	clients.each(&:close)
end