Class: Async::HTTP::Internet
- Inherits:
-
Object
- Object
- Async::HTTP::Internet
- Defined in:
- lib/async/http/internet.rb,
lib/async/http/internet/instance.rb
Instance Attribute Summary collapse
-
#clients ⇒ Object
A cache of clients.
Class Method Summary collapse
-
.instance ⇒ Object
The global instance of the internet.
Instance Method Summary collapse
-
#call(verb, url, *arguments, **options, &block) ⇒ 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.
17 18 19 20 |
# File 'lib/async/http/internet.rb', line 17 def initialize(**) @clients = Hash.new @options = end |
Instance Attribute Details
#clients ⇒ Object
A cache of clients.
24 25 26 |
# File 'lib/async/http/internet.rb', line 24 def clients @clients end |
Class Method Details
.instance ⇒ Object
The global instance of the internet.
14 15 16 |
# File 'lib/async/http/internet/instance.rb', line 14 def self.instance ::Thread.current.async_http_internet_instance ||= self.new end |
Instance Method Details
#call(verb, url, *arguments, **options, &block) ⇒ Object
Make a request to the internet with the given ‘method` and `url`.
If you provide non-frozen headers, they may be mutated.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/async/http/internet.rb', line 42 def call(verb, url, *arguments, **, &block) endpoint = Endpoint[url] client = self.client_for(endpoint) [:authority] ||= endpoint. [:scheme] ||= endpoint.scheme request = ::Protocol::HTTP::Request[verb, endpoint.path, *arguments, **] response = client.call(request) return response unless block_given? begin yield response ensure response.close end end |
#client_for(endpoint) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/async/http/internet.rb', line 26 def client_for(endpoint) key = host_key(endpoint) @clients.fetch(key) do @clients[key] = self.make_client(endpoint) end end |
#close ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/async/http/internet.rb', line 62 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 |