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



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

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

rubocop:disable Metrics/MethodLength



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

def _call(env) # rubocop:disable Metrics/MethodLength
  @request = Rack::Request.new(env)
  identify_request_type

  if grover_request?
    check_file_uri_configuration
    configure_env_for_grover_request(env)
  end
  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



21
22
23
# File 'lib/grover/middleware.rb', line 21

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