Sha256: cfa97db71c4c818d824c1cd02f65be546152c7f1dd451c7783dbdced45674f66

Contents?: true

Size: 1.99 KB

Versions: 1

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true
require 'net/http'
require 'uri'
require 'faraday'
require 'faraday/multipart'

Faraday.default_adapter = :net_http

require 'tempfile'
require_relative "gotenberg/version"

module Gotenberg
  class Error < StandardError; end
  # Your code goes here...

  class Assets
    def self.include_css(file)
      raise "Not in Rails project" unless !Rails.nil?
      abs_path = Rails.root.join('public', 'stylesheets',file)
      return "<style type='text/css'>#{File.read(abs_path)}</style>".html_safe
    end
  end
  class Client
    def initialize(api_url)
      @api_url = api_url
    end

=begin
  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

=end
  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

    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gotenberg-client-0.1.1.9 lib/gotenberg.rb