Class: PhantomPDF::Middleware

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

Constant Summary collapse

REGEXP_PDF =
/\.pdf\z/i

Instance Method Summary collapse

Constructor Details

#initialize(app, output = nil) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
8
# File 'lib/phantompdf/middleware.rb', line 5

def initialize(app, output=nil)
  @app = app
  @output = output
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/phantompdf/middleware.rb', line 10

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

  return @app.call(env) unless request_pdf?

  status, headers, response = @app.call(env)
  return [status, headers, response] if status != 200 || headers['Content-Type'] != 'text/html'

  response_body = render_pdf(response.first)

  headers.merge!({
    'Content-Type' => 'application/pdf',
    'Content-Length' => response_body.size.to_s
  })

  [200, headers, [response_body]]
end