Sha256: 0fcfa4345e9bae12d1b253136fa504d980b16babfab09a63e2231203304a5ff5

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
      case response[2].class.name
      when "ActionDispatch::Response::RackBody"
        response[2].body
      else
        if response[2].respond_to?(:join)
          response[2].join
        else
          response[2]
        end
      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.6 lib/breezy_pdf_lite/intercept/html.rb