Sha256: 48a4ff6f98c8b585b08dda201f76ab15072446c6f93717d96ce0fb5b93918868

Contents?: true

Size: 981 Bytes

Versions: 1

Compression:

Stored size: 981 Bytes

Contents

# frozen_string_literal: true

require 'html2pdf/rails/errors'

module Html2Pdf
  module Rails
    class Client
      def self.post(*args)
        self.new(Html2Pdf.config.endpoint).post(*args)
      end

      def initialize(endpoint)
        @uri = URI.parse(endpoint)
      end

      def post(html:, put_to_storage: false, file_name: nil, disposition: null, pdf_options: {})
        http = Net::HTTP.new(@uri.host, @uri.port).tap { |h| h.use_ssl = @uri.scheme == 'https' }
        request = Net::HTTP::Post.new(@uri.request_uri, headers)
        request.body = {
          html: html,
          putToStorage: put_to_storage,
          fileName: file_name,
          responseDisposition: disposition,
          pdfOptions: pdf_options
        }.to_json
        http.request(request)
      rescue Net::ReadTimeout
        raise Html2Pdf::Rails::NetworkError
      end

      private

      def headers
        { 'Content-Type' => 'application/json' }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
html2pdf-rails-0.2.0 lib/html2pdf/rails/client.rb