Class: Krikri::AsyncUriGetter
- Inherits:
-
Object
- Object
- Krikri::AsyncUriGetter
- Defined in:
- lib/krikri/async_uri_getter.rb
Overview
Helper class for fetching multiple URLs concurrently.
At this point, 5 threads are launched to fetch the list of URLs. We can wait for them all to finish if we want to make sure we don’t continue until all threads have terminated: ‘requests.map(&:join)`
Or simply access the responses and have our current thread block until they’re available:
requests.each do |request|
request.with_response do |response|
if response.status == 200
puts "Response body: #{response.body}"
else
puts "Got return status: #{response.status}"
end
end
end
Defined Under Namespace
Classes: Request
Constant Summary collapse
- MAX_REDIRECTS =
10
Instance Method Summary collapse
-
#add_request(uri: nil, headers: {}, opts: {}) ⇒ Object
Run a request (in a new thread) and return a promise-like object for the response.
-
#initialize(opts: {}) ⇒ AsyncUriGetter
constructor
Create a new asynchronous URL fetcher.
Constructor Details
#initialize(opts: {}) ⇒ AsyncUriGetter
Create a new asynchronous URL fetcher.
56 57 58 |
# File 'lib/krikri/async_uri_getter.rb', line 56 def initialize(opts: {}) @default_opts = { max_redirects: MAX_REDIRECTS }.merge(opts) end |
Instance Method Details
#add_request(uri: nil, headers: {}, opts: {}) ⇒ Object
Run a request (in a new thread) and return a promise-like object for the response.
70 71 72 73 |
# File 'lib/krikri/async_uri_getter.rb', line 70 def add_request(uri: nil, headers: {}, opts: {}) fail ArgumentError, "uri must be a URI; got: #{uri}" unless uri.is_a?(URI) Request.new(uri, headers, @default_opts.merge(opts)) end |