Class: Rager::Http::Adapters::AsyncHttp
- Extended by:
- T::Sig
- Defined in:
- lib/rager/http/adapters/async_http.rb
Instance Method Summary collapse
- #body_enum(response) ⇒ Object
-
#initialize ⇒ AsyncHttp
constructor
A new instance of AsyncHttp.
- #make_request(request) ⇒ Object
- #wrap_if_timeout(timeout, &block) ⇒ Object
Constructor Details
#initialize ⇒ AsyncHttp
Returns a new instance of AsyncHttp.
13 14 15 16 17 18 19 20 21 |
# File 'lib/rager/http/adapters/async_http.rb', line 13 def initialize begin require "async/http" rescue LoadError raise Rager::Errors::DependencyError.new("async-http", details: "Please install the async-http gem to use the AsyncHttp adapter") end @internet = T.let(Async::HTTP::Internet.new, Async::HTTP::Internet) end |
Instance Method Details
#body_enum(response) ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/rager/http/adapters/async_http.rb', line 109 def body_enum(response) Enumerator.new do |yielder| response.body.each { |chunk| yielder << chunk } ensure response.close end end |
#make_request(request) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/rager/http/adapters/async_http.rb', line 28 def make_request(request) response = wrap_if_timeout(request.timeout) do @internet.call( request.verb.serialize, request.url, request.headers.to_a, request.body ) end body = if response.body.nil? nil elsif request.streaming body_enum(response) else response.body.join end Response.new( status: response.status, headers: response.headers.to_h, body: body ) rescue SocketError => e raise Rager::Errors::HttpError.new( self, request.url, 0, body: nil, details: "DNS resolution failed: #{e.}" ) rescue Errno::ECONNREFUSED => e raise Rager::Errors::HttpError.new( self, request.url, 0, body: nil, details: "Connection refused: #{e.}" ) rescue Errno::ETIMEDOUT => e raise Rager::Errors::HttpError.new( self, request.url, 0, body: nil, details: "Connection timed out: #{e.}" ) rescue Async::TimeoutError => e raise Rager::Errors::HttpError.new( self, request.url, 0, body: nil, details: "Request timed out: #{e.}" ) rescue EOFError => e raise Rager::Errors::HttpError.new( self, request.url, 0, body: nil, details: "Connection closed unexpectedly: #{e.}" ) end |
#wrap_if_timeout(timeout, &block) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/rager/http/adapters/async_http.rb', line 94 def wrap_if_timeout(timeout, &block) if timeout Async::Task.current.with_timeout(timeout) do block.call end else block.call end end |