Sha256: 39903c493140fb39459301a10c33d81fd7b4a1176d4b219f1399f9cf998bd016

Contents?: true

Size: 1.38 KB

Versions: 14

Compression:

Stored size: 1.38 KB

Contents

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

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

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)
        templ = ::Roger::Template.open(
          template_path,
          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 templ.render(env)
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
roger-1.4.6 lib/roger/rack/roger.rb
roger-1.4.5 lib/roger/rack/roger.rb
roger-1.4.4 lib/roger/rack/roger.rb
roger-1.4.3 lib/roger/rack/roger.rb
roger-1.4.2 lib/roger/rack/roger.rb
roger-1.3.5 lib/roger/rack/roger.rb
roger-1.4.1 lib/roger/rack/roger.rb
roger-1.4.0 lib/roger/rack/roger.rb
roger-1.3.4 lib/roger/rack/roger.rb
roger-1.3.3 lib/roger/rack/roger.rb
roger-1.3.2 lib/roger/rack/roger.rb
roger-1.3.0 lib/roger/rack/roger.rb
roger-1.2.2 lib/roger/rack/roger.rb
roger-1.2.1 lib/roger/rack/roger.rb