Class: Async::HTTP::Internet
- Inherits:
-
Object
- Object
- Async::HTTP::Internet
- Extended by:
- Thread::Local
- Defined in:
- lib/async/http/internet.rb,
lib/async/http/internet/instance.rb
Instance Attribute Summary collapse
-
#clients ⇒ Object
A cache of clients.
Instance Method Summary collapse
-
#call(method, url, headers = nil, body = nil) ⇒ Object
Make a request to the internet with the given ‘method` and `url`.
- #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.
16 17 18 19 |
# File 'lib/async/http/internet.rb', line 16 def initialize(**) @clients = Hash.new @options = end |
Instance Attribute Details
#clients ⇒ Object
A cache of clients.
23 24 25 |
# File 'lib/async/http/internet.rb', line 23 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.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/async/http/internet.rb', line 41 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., method, endpoint.path, nil, headers, body) return client.call(request) end |
#client_for(endpoint) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/async/http/internet.rb', line 25 def client_for(endpoint) key = host_key(endpoint) @clients.fetch(key) do @clients[key] = self.make_client(endpoint) end end |
#close ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/async/http/internet.rb', line 53 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 |