Class: Html2img::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/html2img/transport.rb

Overview

HTTP transport for the client.

The default is built on Net::HTTP from the standard library, so the gem has no runtime dependencies. Anything responding to #call with the same signature can be passed to Client as transport:, which is the seam for Faraday, HTTPX, a proxy, retry middleware, or a stub in your tests.

A transport returns the raw [status, body] pair for any HTTP response, including 4xx and 5xx. Mapping a status onto a typed error is the client's job. A transport only raises when no response was received at all.

Instance Method Summary collapse

Instance Method Details

#call(method:, url:, headers:, body:, timeout:) ⇒ Array(Integer, String)

Returns status and body.

Parameters:

  • method (String)

    "POST" or "GET"

  • url (String)

    the absolute URL

  • headers (Hash{String => String})
  • body (String, nil)

    the request body

  • timeout (Float)

    seconds

Returns:

  • (Array(Integer, String))

    status and body



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/html2img/transport.rb', line 25

def call(method:, url:, headers:, body:, timeout:)
  uri = URI.parse(url)
  request = build_request(method, uri, headers, body)

  response = http(uri, timeout).request(request)

  [response.code.to_i, response.body.to_s]
# Net::OpenTimeout and Net::ReadTimeout both descend from Timeout::Error.
rescue Timeout::Error => e
  raise TimeoutError, "The request to #{url} timed out after #{timeout} seconds: #{e.message}"
rescue SocketError, SystemCallError, OpenSSL::SSL::SSLError, IOError, Net::HTTPBadResponse => e
  raise ConnectionError, "Could not reach the html2img API: #{e.message}"
end