Class: Gotenberg::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(api_url) ⇒ Client

Returns a new instance of Client.



24
25
26
# File 'lib/gotenberg.rb', line 24

def initialize(api_url)
  @api_url = api_url
end

Instance Method Details

#html(render, output) ⇒ Object

write the pdf file in output

pre:
  render, string, html that needs to be converted
  output, output file
post:
  pdf in the output file
  true if everything ok


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
# File 'lib/gotenberg.rb', line 38

def html(render,output)
    return false unless self.up?


    ind_html = Tempfile.new('index.html')
    ind_html.write(render)
    ind_html.rewind
    payload = {
      "index.html": Faraday::Multipart::FilePart.new(
          File.open(ind_html),
          'text/html',
          "index.html"
        )
    }
    url= "#{@api_url}/forms/chromium/convert/html"
  begin
    conn = Faraday.new(url) do |f|
      f.request :multipart, flat_encode: true
      f.adapter :net_http
    end
    response = conn.post(url, payload)

  rescue StandardError => e
    response=""
  end

  ind_html.close
  ind_html.unlink
  output.write(response.body.force_encoding("utf-8"))
  return true

end

#up?Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gotenberg.rb', line 71

def up?
  begin
    uri = URI.parse("#{@api_url}/health")
    request = Net::HTTP::Get.new(uri)
    req_options = {use_ssl: uri.scheme == "https",}

    response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
      http.request(request)
    end

    if response.code == "200" && JSON.parse(response.body)["status"]=="up"
      return true
    end
  rescue StandardError => e
    return false
  end
end