Sha256: 634e42f9adc8e1215f65d18a8172e7a718453f7f993c1baf651fee3c42d19e09

Contents?: true

Size: 1.43 KB

Versions: 6

Compression:

Stored size: 1.43 KB

Contents

require "rack/request"
require "rack/response"
require "rack/file"

require File.dirname(__FILE__) + "/../resolver"
require File.dirname(__FILE__) + "/../renderer"

module Roger
  module Rack
    # Roger middleware that processe roger templates
    class Roger
      attr_reader :project

      def initialize(project)
        @project = project
        @docroot = project.html_path

        @resolver = Resolver.new(@docroot)
        @file_server = ::Rack::File.new(@docroot)
      end

      def call(env)
        url = env["PATH_INFO"]
        env["MOCKUP_PROJECT"] = env["roger.project"] || @project

        if template_path = @resolver.url_to_path(url)
          env["rack.errors"].puts "Rendering template #{template_path.inspect} (#{url.inspect})"
          build_response(template_path, env).finish
        else
          env["rack.errors"].puts "Invoking file handler for #{url.inspect}"
          @file_server.call(env)
        end
      end

      protected

      def build_response(template_path, env)
        renderer = ::Roger::Renderer.new(
          env,
          partials_path: @project.partials_path,
          layouts_path: @project.layouts_path
        )
        mime = ::Rack::Mime.mime_type(File.extname(template_path), "text/html")
        ::Rack::Response.new do |res|
          res.headers["Content-Type"] = mime if mime
          res.status = 200
          res.write renderer.render(template_path)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
roger-1.6.4 lib/roger/rack/roger.rb
roger-1.6.3 lib/roger/rack/roger.rb
roger-1.6.2 lib/roger/rack/roger.rb
roger-1.6.1 lib/roger/rack/roger.rb
roger-1.6.0 lib/roger/rack/roger.rb
roger-1.5.0 lib/roger/rack/roger.rb