app.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. require 'sinatra'
  2. require 'json'
  3. set :public_folder, File.dirname(settings.root)
  4. enable :static
  5. jquery_version = '3.2'
  6. helpers do
  7. def pjax?
  8. env['HTTP_X_PJAX'] && !params[:layout]
  9. end
  10. def title(str)
  11. if pjax?
  12. "<title>#{str}</title>"
  13. else
  14. @title = str
  15. nil
  16. end
  17. end
  18. define_method(:jquery_version) do
  19. jquery_version
  20. end
  21. end
  22. after do
  23. if pjax?
  24. response.headers['X-PJAX-URL'] ||= request.url
  25. response.headers['X-PJAX-Version'] = 'v1'
  26. end
  27. end
  28. get '/' do
  29. jquery_version = params[:jquery] if params[:jquery]
  30. erb :qunit
  31. end
  32. get '/env.html' do
  33. erb :env, :layout => !pjax?
  34. end
  35. post '/env.html' do
  36. erb :env, :layout => !pjax?
  37. end
  38. put '/env.html' do
  39. erb :env, :layout => !pjax?
  40. end
  41. delete '/env.html' do
  42. erb :env, :layout => !pjax?
  43. end
  44. get '/redirect.html' do
  45. if params[:anchor]
  46. path = "/hello.html##{params[:anchor]}"
  47. if pjax?
  48. response.headers['X-PJAX-URL'] = uri(path)
  49. status 200
  50. else
  51. redirect path
  52. end
  53. else
  54. redirect "/hello.html"
  55. end
  56. end
  57. get '/timeout.html' do
  58. if pjax?
  59. sleep 1
  60. erb :timeout, :layout => false
  61. else
  62. erb :timeout
  63. end
  64. end
  65. post '/timeout.html' do
  66. if pjax?
  67. sleep 1
  68. erb :timeout, :layout => false
  69. else
  70. status 500
  71. erb :boom
  72. end
  73. end
  74. get '/boom.html' do
  75. status 500
  76. erb :boom, :layout => !pjax?
  77. end
  78. get '/boom_sans_pjax.html' do
  79. status 500
  80. erb :boom_sans_pjax, :layout => false
  81. end
  82. get '/:page.html' do
  83. erb :"#{params[:page]}", :layout => !pjax?
  84. end
  85. get '/some-&-path/hello.html' do
  86. erb :hello, :layout => !pjax?
  87. end