Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Support parameters and default out_path #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/shrimp/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def initialize(app, options = { }, conditions = { })
@options[:polling_interval] ||= 1
@options[:polling_offset] ||= 1
@options[:cache_ttl] ||= 1
@options[:out_path] ||= Dir.tmpDir
@options[:request_timeout] ||= @options[:polling_interval] * 10
end

Expand Down Expand Up @@ -54,11 +55,12 @@ def call(env)

# Private: start phantom rendering in a separate process
def fire_phantom
Process::detach fork { Phantom.new(@request.url.sub(%r{\.pdf$}, ''), @options, @request.cookies).to_pdf(render_to) }
url = @request.url.sub(%r{\.pdf(\?|$)}, '\1').sub('utf8=%E2%9C%93&', '')
Process::detach fork { Phantom.new(url, @options, @request.cookies).to_pdf(render_to) }
end

def render_to
file_name = Digest::MD5.hexdigest(@request.path) + ".pdf"
file_name = Digest::MD5.hexdigest(@request.fullpath) + ".pdf"
file_path = @options[:out_path]
"#{file_path}/#{file_name}"
end
Expand Down Expand Up @@ -92,7 +94,7 @@ def rendering_in_progress?
end

def render_as_pdf?
request_path_is_pdf = [email protected](%r{\.pdf$})
request_path_is_pdf = [email protected](%r{\.pdf(\?|$)})

if request_path_is_pdf && @conditions[:only]
rules = [@conditions[:only]].flatten
Expand Down Expand Up @@ -162,7 +164,7 @@ def error_response
<head>
</head>
<body>
<h2>Sorry request timed out... </h2>
<h2>Sorry request timed out... Please try again in a few moments</h2>
</body>
</ html>
HTML
Expand Down