Sha256: d67f8869c8092e9b1ed3aaae2c960afde1d4a8f533dece6dc8c2ceb147090fa0

Contents?: true

Size: 710 Bytes

Versions: 2

Compression:

Stored size: 710 Bytes

Contents

# frozen_string_literal: true

module BreezyPDFLite
  # Request conversion of a HTML string to PDF
  # If the response isn't a 201, raise an error
  class RenderRequest
    def initialize(body)
      @body = body
    end

    def response
      @response ||= submit.tap do |resp|
        raise RenderError, "#{resp.code}: #{resp.body}" if resp.code != "201"
      end
    end

    def to_file
      @to_file ||= Tempfile.new(%w[response .pdf]).tap do |file|
        file.binmode
        file.write response.body
        file.flush
        file.rewind
      end
    end

    private

    def submit
      client.post("/render/html", @body)
    end

    def client
      @client ||= Client.new
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
breezy_pdf_lite-0.1.1 lib/breezy_pdf_lite/render_request.rb
breezy_pdf_lite-0.1.0 lib/breezy_pdf_lite/render_request.rb