Class: Grover::Middleware

Inherits:
Object show all
Defined in:
lib/grover/middleware.rb

Overview

Rack middleware for catching PDF requests and returning the upstream HTML as a PDF

Much of this code was sourced from the PDFKit project

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



11
12
13
14
# File 'lib/grover/middleware.rb', line 11

def initialize(app)
  @app = app
  @render_pdf = false
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/grover/middleware.rb', line 16

def call(env)
  @request = Rack::Request.new(env)
  @render_pdf = false

  configure_env_for_pdf_request(env) if render_as_pdf?
  status, headers, response = @app.call(env)

  if rendering_pdf? && html_content?(headers)
    pdf = convert_to_pdf response
    response = [pdf]
    update_headers headers, pdf
  end

  [status, headers, response]
end