Sha256: d7abd92efbb9420abbeabdaf1f9932e45047cf3691f10cd4b05ed783a40dd654

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

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
        response = http.request(request)

        case response.code
        when '200'
          response.body
        when '503'
          raise Html2Pdf::Rails::ServiceUnavailableError.new(response)
        else
          raise Html2Pdf::Rails::RequestError.new(response)
        end
      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.3.0 lib/html2pdf/rails/client.rb