class Rack::ServerPages

Constants

VERSION

Public Class Methods

[](options = {}, &block) click to toggle source
# File lib/rack/server_pages.rb, line 15
def [](options = {}, &block)
  new(nil, options, &block)
end
call(env) click to toggle source
# File lib/rack/server_pages.rb, line 11
def call(env)
  new.call(env)
end
new(app = nil, options = {}) { |config| ... } click to toggle source
# File lib/rack/server_pages.rb, line 20
def initialize(app = nil, options = {})
  @config = Config.new(*options)
  yield @config if block_given?
  @app = app || @config.failure_app || NotFound

  require ::File.dirname(__FILE__) + '/server_pages/php_helper'
  @config.helpers Rack::ServerPages::PHPHelper

  @config.filter.merge_from_helpers(@config.helpers)
  @binding = Binding.extended_class(@config.helpers)

  @path_regex = %r{^#{@config.effective_path}/((?:[\w-]+/)+)?([A-z0-9][\w\.\-\@]*?\w)?(\.\w+)?$}
end

Public Instance Methods

build_response(template, scope) click to toggle source
# File lib/rack/server_pages.rb, line 60
def build_response(template, scope)
  scope.response.tap do |response|
    response.write template.render_with_layout(scope)
    response['Last-Modified'] ||= ::File.mtime(template.file).httpdate
    response['Content-Type']  ||= template.mime_type_with_charset(@config.default_charset)
    response['Cache-Control'] ||= @config.cache_control if @config.cache_control
  end
end
call(env) click to toggle source
# File lib/rack/server_pages.rb, line 34
def call(env)
  dup.serving(env)
end
find_template_files(path_info) click to toggle source
# File lib/rack/server_pages.rb, line 48
def find_template_files(path_info)
  if m = path_info.match(@path_regex)
    @config.view_paths.inject([]) do |files, path|
      files.concat Dir["#{path}/#{m[1]}#{m[2] || 'index'}#{m[3]}{.*,}"].select { |s| s.include?('.') }
    end
  end
end
select_template_file(files) click to toggle source
# File lib/rack/server_pages.rb, line 56
def select_template_file(files)
  files.first
end
server_page(template) click to toggle source
# File lib/rack/server_pages.rb, line 69
def server_page(template)
  lambda do |env|
    @binding.new(env).tap do |scope|
      catch(:halt) do
        begin
          @config.filter.invoke(scope, :before)
          build_response(template, scope)
        rescue
          if @config.show_exceptions?
            raise $!
          else
            scope.response.status = 500
            scope.response['Content-Type'] ||= 'text/html'
            @config.filter.invoke(scope, :on_error)
          end
        ensure
          @config.filter.invoke(scope, :after)
        end
      end
    end.response.finish
  end
end
serving(env) click to toggle source
# File lib/rack/server_pages.rb, line 38
def serving(env)
  files = find_template_files(env['PATH_INFO'])
  if files.nil? || files.empty?
    @app
  else
    file = select_template_file(files)
    (tpl = Template[file]) ? server_page(tpl) : File.new(file.split(env['PATH_INFO']).first)
  end.call(env)
end