Class: PDFKit::Middleware
- Inherits:
-
Object
- Object
- PDFKit::Middleware
- Defined in:
- lib/pdfkit/middleware.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}, conditions = {}) ⇒ Middleware
constructor
A new instance of Middleware.
- #upload(file) ⇒ Object
Constructor Details
#initialize(app, options = {}, conditions = {}) ⇒ Middleware
Returns a new instance of Middleware.
5 6 7 8 9 |
# File 'lib/pdfkit/middleware.rb', line 5 def initialize(app, = {}, conditions = {}) @app = app @options = @conditions = conditions end |
Instance Method Details
#call(env) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/pdfkit/middleware.rb', line 25 def call(env) @request = Rack::Request.new(env) @render_pdf = false set_request_to_render_as_pdf(env) if render_as_pdf? status, headers, response = @app.call(env) if rendering_pdf? && headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/ body = response.respond_to?(:body) ? response.body : response.join body = body.join if body.is_a?(Array) pdf = PDFKit.new(translate_paths(body, env), @options) if headers["PDF-Full-File-Path"].present? file = pdf.to_file( headers["PDF-Full-File-Path"] ) upload(file) if PDFKit.configuration.[:host].present? end body = pdf.to_pdf response = [body] # Do not cache PDFs headers.delete('ETag') headers.delete('Cache-Control') headers["Content-Length"] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s headers["Content-Type"] = "application/pdf" end [status, headers, response] end |
#upload(file) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/pdfkit/middleware.rb', line 11 def upload(file) host = PDFKit.configuration.[:host] user = PDFKit.configuration.[:user] folder = PDFKit.configuration.[:folder] password = PDFKit.configuration.[:password] file_name = File.basename( file ) full_path = File.absolute_path( file ) Net::SFTP.start(host, user, :password => password) do |sftp| data = sftp.upload!( full_path, "#{folder}/#{file_name}" ) end end |