lib/grover/middleware.rb in grover-0.6.1 vs lib/grover/middleware.rb in grover-0.6.2
- old
+ new
@@ -13,13 +13,13 @@
@render_pdf = false
end
def call(env)
@request = Rack::Request.new(env)
- @render_pdf = false
+ @render_pdf = pdf_request?
- configure_env_for_pdf_request(env) if render_as_pdf?
+ configure_env_for_pdf_request(env) if rendering_pdf?
status, headers, response = @app.call(env)
if rendering_pdf? && html_content?(headers)
pdf = convert_to_pdf response
response = [pdf]
@@ -35,11 +35,11 @@
def rendering_pdf?
@render_pdf
end
- def render_as_pdf?
+ def pdf_request?
!@request.path.match(PDF_REGEX).nil?
end
def html_content?(headers)
headers['Content-Type'] =~ %r{text/html|application/xhtml\+xml}
@@ -57,11 +57,11 @@
def create_grover_for_response(response)
body = response.respond_to?(:body) ? response.body : response.join
body = body.join if body.is_a?(Array)
body = HTMLPreprocessor.process body, root_url, protocol
- Grover.new(body, display_url: request_url, cache: false)
+ Grover.new(body, display_url: request_url)
end
def add_cover_content(grover)
pdf = CombinePDF.parse grover.to_pdf
pdf >> fetch_cover_pdf(grover.front_cover_path) if grover.show_front_cover?
@@ -86,14 +86,11 @@
headers['Content-Length'] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
headers['Content-Type'] = 'application/pdf'
end
def configure_env_for_pdf_request(env)
- @render_pdf = true
-
- env['PATH_INFO'] = path_without_extension
- env['REQUEST_URI'] = path_without_extension
+ env['PATH_INFO'] = env['REQUEST_URI'] = path_without_extension
env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
env['Rack-Middleware-Grover'] = 'true'
end
def concat(accepts, type)
@@ -107,13 +104,10 @@
def protocol
env['rack.url_scheme']
end
def path_without_extension
- return @path_without_extension if defined? @path_without_extension
-
- path = @request.path.sub(PDF_REGEX, '')
- @path_without_extension = path.sub(@request.script_name, '')
+ @request.path.sub(PDF_REGEX, '').sub(@request.script_name, '')
end
def request_url
"#{root_url.sub(%r{/\z}, '')}#{path_without_extension}"
end