Sha256: 1b3386d2e25280395840afb5df11fb8fe27fa622eb343b5757d5db4db5b5e0f3

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module BreezyPDFLite::Intercept
  # :nodoc
  class HTML < Base
    def call
      raise BreezyPDFLite::Intercept::UnRenderable unless (200..299).cover?(status)

      render_request = BreezyPDFLite::RenderRequest.new(body).submit

      if render_request.code != "201"
        raise BreezyPDFLite::RenderError, "Status: #{render_request.status} body: #{render_request.body}"
      end

      [
        201,
        {
          "Content-Type" => "application/pdf",
          "Content-Length" => render_request.header["Content-Length"],
          "Content-Disposition" => render_request.header["Content-Disposition"]
        },
        [render_request.body]
      ]
    rescue BreezyPDFLite::Intercept::UnRenderable
      response
    end

    private

    def status
      @status ||= response[0].to_i
    end

    def headers
      @headers ||= response[1]
    end

    def body
      if response[2].respond_to?(:join)
        response[2].join
      elsif response[2].respond_to?(:each)
        content = []
        response[2].each { |part| content << part }

        content.join
      else
        response[2]
      end
    end

    def response
      @response ||= app.call(doctored_env)
    end

    def doctored_env
      env.dup.tap do |hash|
        hash["HTTP_ACCEPT"] = "text/html"
        hash["PATH_INFO"]   = path
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
breezy_pdf_lite-0.0.7 lib/breezy_pdf_lite/intercept/html.rb