Class: Coelacanth::Client::Gotenberg

Inherits:
Base
  • Object
show all
Defined in:
lib/coelacanth/client/gotenberg.rb

Overview

Client for capturing screenshots through a Gotenberg Chromium service.

Constant Summary collapse

SCREENSHOT_URL_PATH =
"/forms/chromium/screenshot/url"

Instance Method Summary collapse

Methods inherited from Base

#client, #initialize

Constructor Details

This class inherits a constructor from Coelacanth::Client::Base

Instance Method Details

#get_responseObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/coelacanth/client/gotenberg.rb', line 14

def get_response
  uri = URI.parse(@url)
  response = Coelacanth::HTTP.get_response(
    uri,
    open_timeout: Coelacanth::HTTP::DEFAULT_OPEN_TIMEOUT,
    read_timeout: Coelacanth::HTTP::DEFAULT_READ_TIMEOUT
  )
  @origin_response = response
  @status_code = response.code.to_i

  return response.body if response.is_a?(Net::HTTPSuccess)

  Coelacanth::HTTP.raise_http_error(uri, response)
end

#get_screenshotObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/coelacanth/client/gotenberg.rb', line 29

def get_screenshot
  response = Net::HTTP.start(
    endpoint_uri.host,
    endpoint_uri.port,
    use_ssl: endpoint_uri.scheme == "https",
    open_timeout: open_timeout,
    read_timeout: read_timeout
  ) do |http|
    http.request(screenshot_request)
  end

  return response.body if response.is_a?(Net::HTTPSuccess)

  raise "Failed to fetch screenshot from Gotenberg: #{response.code} #{response.message}"
rescue Net::OpenTimeout, Net::ReadTimeout, Timeout::Error => e
  raise Coelacanth::TimeoutError, "Gotenberg screenshot request timed out: #{e.message}"
end