Class: Crawlora::DefaultTransport
- Inherits:
-
Object
- Object
- Crawlora::DefaultTransport
- Defined in:
- lib/crawlora/client.rb
Overview
Default keep-alive transport: reuses one Net::HTTP connection per origin so repeated calls share a TCP/TLS session. Inject a callable transport for tests or custom HTTP stacks.
Instance Method Summary collapse
- #call(method:, url:, headers:, body:, timeout:) ⇒ Object
- #close ⇒ Object
-
#initialize ⇒ DefaultTransport
constructor
A new instance of DefaultTransport.
Constructor Details
#initialize ⇒ DefaultTransport
Returns a new instance of DefaultTransport.
28 29 30 31 |
# File 'lib/crawlora/client.rb', line 28 def initialize @connections = {} @mutex = Mutex.new end |
Instance Method Details
#call(method:, url:, headers:, body:, timeout:) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/crawlora/client.rb', line 33 def call(method:, url:, headers:, body:, timeout:) uri = URI.parse(url) http = connection(uri, timeout) request = build_request(method, uri, headers, body) response = http.request(request) Response.new(response.code.to_i, response.to_hash.transform_values { |v| v.is_a?(Array) ? v.join(", ") : v }, response.body || "") end |
#close ⇒ Object
41 42 43 44 45 46 |
# File 'lib/crawlora/client.rb', line 41 def close @mutex.synchronize do @connections.each_value { |http| http.finish if http.started? } @connections.clear end end |