Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 69 lines (55 sloc) 1.453 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
module Rack
  module Formtastic
    class Samples
      def initialize(app)
        @app = app
      end
      
      def call(env)
        @status, @headers, @body = @app.call(env)
        @env = env
  
        @body = '' if favicon?
        @body = static_file if !favicon? && static_file?
        @body = stylesheet if stylesheet?
        @headers ||= {}
        @headers['Content-Type'] = mime(extension)
        [@status, @headers, @body]
      end
      
      def static_file?
        !stylesheet?
      end
      
      def stylesheet?
        @env["PATH_INFO"] =~ /\.(css)/
      end
      
      def favicon?
        @env["PATH_INFO"] =~ /favicon.ico$/
      end
      
      def extension
        @env["PATH_INFO"].split(".").last
      end
      
      def mime(extension)
        mimes[extension] || mimes['html']
      end
      
      def mimes
        {
          'ico' => 'image/x-icon',
          'html' => 'text/html',
          'css' => 'text/css',
          'js' => 'text/javascript'
        }
      end
      
      def static_file
        ::File.open(file_path)
      end
      
      def stylesheet
        ::File.open(::File.join("../app/assets/stylesheets", file_path))
      end
      
      def file_path
        @env["PATH_INFO"].gsub(/^\//, '').gsub(/^$/, 'index.html')
      end
      
    end
  end
end

use Rack::ContentLength
use Rack::Formtastic::Samples

app = lambda { |env| [200, @headers, @body] }
run app
Something went wrong with that request. Please try again.