lib/plezi/helpers/http_sender.rb in plezi-0.11.0 vs lib/plezi/helpers/http_sender.rb in plezi-0.11.1

- old
+ new

@@ -1,30 +1,29 @@ module Plezi module Base # Sends common basic HTTP responses. module HTTPSender + class CodeContext + attr_accessor :request + def initialize request + @request = request + end + end module_function ###### ## basic responses ## (error codes and static files) # sends a response for an error code, rendering the relevent file (if exists). def send_by_code request, response, code, headers = {} begin base_code_path = request.io.params[:templates] || File.expand_path('.') - if defined?(::Slim) && Plezi.file_exists?(fn = File.join(base_code_path, "#{code}.html.slim")) - Plezi.cache_data fn, Slim::Template.new( fn ) unless Plezi.cached? fn - return send_raw_data request, response, Plezi.get_cached( fn ).render( self, request: request ), 'text/html', code, headers - elsif defined?(::Haml) && Plezi.file_exists?(fn = File.join(base_code_path, "#{code}.html.haml")) - Plezi.cache_data fn, Haml::Engine.new( IO.binread( fn ) ) unless Plezi.cached? fn - return send_raw_data request, response, Plezi.get_cached( File.join(base_code_path, "#{code}.html.haml") ).render( self ), 'text/html', code, headers - elsif defined?(::ERB) && Plezi.file_exists?(fn = File.join(base_code_path, "#{code}.html.erb")) - return send_raw_data request, response, ERB.new( Plezi.load_file( fn ) ).result(binding), 'text/html', code, headers - elsif Plezi.file_exists?(fn = File.join(base_code_path, "#{code}.html")) - return send_file(request, response, fn, code, headers) - end + fn = File.join(base_code_path, "#{code}.html") + rendered = ::Plezi::Renderer.render fn, binding #CodeContext.new(request) + return send_raw_data request, response, rendered, 'text/html', code, headers if rendered + return send_file(request, response, fn, code, headers) if Plezi.file_exists?(fn) return true if send_raw_data(request, response, response.class::STATUS_CODES[code], 'text/plain', code, headers) rescue Exception => e Plezi.error e end false