Sha256: d333ec4ecde269caf4e53850d30125148f5972f53d92bec9968504a0289c07b6

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

require 'rack/request'
require 'rack/response'
require 'rack/file'

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

module HtmlMockup
  module Rack
    
    class HtmlMockup
      
      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"] = project
        
        if template_path = @resolver.url_to_path(url)
          env["rack.errors"].puts "Rendering template #{template_path.inspect} (#{url.inspect})"
          # begin
            templ = ::HtmlMockup::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')
            resp = ::Rack::Response.new do |res|
              res.headers["Content-Type"] = mime if mime
              res.status = 200
              res.write templ.render(env)
            end
            resp.finish
          # rescue StandardError => e
          #   env["rack.errors"].puts "#{e.message}\n #{e.backtrace.join("\n")}\n\n"
          #   resp = ::Rack::Response.new do |res|
          #     res.status = 500
          #     res.write "An error occurred"
          #   end
          #   resp.finish
          # end
        else
          env["rack.errors"].puts "Invoking file handler for #{url.inspect}"
          @file_server.call(env)
        end
      end    
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
html_mockup-0.9.0 lib/html_mockup/rack/html_mockup.rb
html_mockup-0.8.4 lib/html_mockup/rack/html_mockup.rb
html_mockup-0.8.3 lib/html_mockup/rack/html_mockup.rb
html_mockup-0.8.2 lib/html_mockup/rack/html_mockup.rb
html_mockup-0.8.1 lib/html_mockup/rack/html_mockup.rb
html_mockup-0.8.0 lib/html_mockup/rack/html_mockup.rb