Sha256: 9c1ed836af73f2882303e896e1a24f8974abe8290bcdeb7548f8cdaaf541b2e9

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

module RubySlippers
  module Engine
    class App
      attr_reader :config, :site
      
      @@site = nil

      def initialize config = {}, &blk
        @config = config.is_a?(Config) ? config : Config.new(config)
        @config.instance_eval(&blk) if block_given?
        @site ||= @@site = Site.new(@config)
      end
      
      def self.site
        @@site
      end

      def call env
        @request  = Rack::Request.new env
        @response = Rack::Response.new

        return [400, {}, []] unless @request.get?

        path, mime = @request.path_info.split('.')
        route = (path || '/').split('/').reject {|i| i.empty? }

        response = @site.go(route, env, *(mime ? mime : []))

        @response.body = [response[:body]]
        @response['Content-Length'] = response[:body].length.to_s unless response[:body].empty?
        @response['Content-Type']   = Rack::Mime.mime_type(".#{response[:type]}")

        # Set http cache headers
        @response['Cache-Control'] = if RubySlippers::Engine.env == 'production'
          "public, max-age=#{@config[:cache]}"
        else
          "no-cache, must-revalidate"
        end

        @response['ETag'] = %("#{Digest::SHA1.hexdigest(response[:body])}")

        @response.status = response[:status]
        @response.finish
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ruby_slippers-0.2.15 lib/ruby_slippers/app.rb
ruby_slippers-0.2.8 lib/ruby_slippers/app.rb
ruby_slippers-0.2.4 lib/ruby_slippers/app.rb
ruby_slippers-0.2.3 lib/ruby_slippers/app.rb
ruby_slippers-0.2.0 lib/ruby_slippers/app.rb