config.ru 1.4 KB

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