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, *args) ⇒ Middleware

rubocop:disable Metrics/ClassLength


13
14
15
16
17
18
19
20
21
# File 'lib/grover/middleware.rb', line 13

def initialize(app, *args)
  @app = app
  @pdf_request = false
  @png_request = false
  @jpeg_request = false

  @root_url =
    args.last.is_a?(Hash) && args.last.key?(:root_url) ? args.last[:root_url] : Grover.configuration.root_url
end

Instance Method Details

#_call(env) ⇒ Object


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/grover/middleware.rb', line 27

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

  configure_env_for_grover_request(env) if grover_request?
  status, headers, response = @app.call(env)
  response = update_response response, headers if grover_request? && html_content?(headers)

  [status, headers, response]
ensure
  restore_env_from_grover_request(env) if grover_request?
end

#call(env) ⇒ Object


23
24
25
# File 'lib/grover/middleware.rb', line 23

def call(env)
  dup._call(env)
end