lib/grover/middleware.rb in grover-0.3.0 vs lib/grover/middleware.rb in grover-0.3.1

- old
+ new

@@ -45,12 +45,12 @@ def convert_to_pdf(response) body = response.respond_to?(:body) ? response.body : response.join body = body.join if body.is_a?(Array) - body = HTMLPreprocessor.process body, request_url, protocol - Grover.new(body).to_pdf + body = HTMLPreprocessor.process body, root_url, protocol + Grover.new(body, display_url: request_url).to_pdf end def update_headers(headers, body) # Do not cache PDFs headers.delete 'ETag' @@ -61,27 +61,34 @@ end def configure_env_for_pdf_request(env) @render_pdf = true - path = @request.path.sub(PDF_REGEX, '') - path = path.sub(@request.script_name, '') - - %w[PATH_INFO REQUEST_URI].each { |e| env[e] = path } - + env['PATH_INFO'] = path_without_extension + env['REQUEST_URI'] = path_without_extension env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html')) end def concat(accepts, type) (accepts || '').split(',').unshift(type).compact.join(',') end - def request_url - "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/" + def root_url + @root_url ||= "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/" end 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, '') + end + + def request_url + "#{root_url.sub(%r{/\z}, '')}#{path_without_extension}" end def env @request.env end