Class: Async::HTTP::Internet
- Inherits:
-
Object
- Object
- Async::HTTP::Internet
- Defined in:
- lib/async/http/internet.rb
Instance Attribute Summary collapse
-
#clients ⇒ Object
A cache of clients.
Instance Method Summary collapse
- #call(method, url, headers = nil, body = nil) ⇒ Object
- #client_for(endpoint) ⇒ Object
- #close ⇒ Object
-
#initialize(**options) ⇒ Internet
constructor
A new instance of Internet.
Constructor Details
#initialize(**options) ⇒ Internet
Returns a new instance of Internet.
31 32 33 34 |
# File 'lib/async/http/internet.rb', line 31 def initialize(**) @clients = {} @options = end |
Instance Attribute Details
#clients ⇒ Object
A cache of clients.
38 39 40 |
# File 'lib/async/http/internet.rb', line 38 def clients @clients end |
Instance Method Details
#call(method, url, headers = nil, body = nil) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/async/http/internet.rb', line 40 def call(method, url, headers = nil, body = nil) endpoint = Endpoint.parse(url) key = host_key(endpoint) client = @clients.fetch(key) do @clients[key] = self.client_for(endpoint) end body = Body::Buffered.wrap(body) headers = ::Protocol::HTTP::Headers[headers] request = ::Protocol::HTTP::Request.new(client.scheme, endpoint., method, endpoint.path, nil, headers, body) return client.call(request) end |
#client_for(endpoint) ⇒ Object
56 57 58 |
# File 'lib/async/http/internet.rb', line 56 def client_for(endpoint) Client.new(endpoint, **@options) end |
#close ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/async/http/internet.rb', line 60 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 |