Guest User

Untitled

a guest
Jul 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. ## examples/config.ru [ruby]
  2. require File.join(File.dirname(__FILE__), 'example_app')
  3.  
  4. map "/sinatra" do
  5. run SinatraApp
  6. end
  7.  
  8. map "/restroutes" do
  9. run RestRoutesApp
  10. end
  11.  
  12. ## examples/example_app.rb [ruby]
  13. require 'rubygems'
  14. require File.join(File.dirname(__FILE__), '..', 'lego')
  15.  
  16. module NiftyHelpers
  17. def my_nifty_helper(string)
  18. "My nifty helper called with: #{string}"
  19. end
  20. end
  21.  
  22. module Sinatra
  23. def application_helpers(&block)
  24. context.evaluate(&block)
  25. end
  26.  
  27. def get(path, &block)
  28. routes.add :get, path, &block
  29. end
  30. end
  31.  
  32. module RestRoutes
  33. def application_helpers(&block)
  34. context.evaluate(&block)
  35. end
  36.  
  37. def index(&block)
  38. routes.add :get, '/', &block
  39. end
  40.  
  41. def show(&block)
  42. routes.add :get, 'show/:id', &block
  43. end
  44. end
  45.  
  46. Lego::Pieces.register :helper, NiftyHelpers
  47. Lego::Pieces.register :controller, Sinatra
  48. Lego::Pieces.register :controller, RestRoutes
  49.  
  50. class MyStupidMiddleware
  51. def initialize(app, options = {})
  52. @app = app
  53. end
  54.  
  55. def call(env)
  56. status, headers, body = @app.call(env)
  57. new_body = "Stupid... "
  58. body.each { |str| new_body << str }
  59. new_body << " ...Middleware"
  60.  
  61. headers['Content-Length'] = new_body.length.to_s
  62.  
  63. [status, headers, new_body]
  64. end
  65. end
  66.  
  67. class SinatraApp < Lego::Application
  68. controller :sinatra
  69. helpers :nifty_helpers
  70.  
  71. use MyStupidMiddleware
  72.  
  73. application_helpers do
  74. def escape_html(content)
  75. Rack::Utils::escape_html(content)
  76. end
  77. end
  78.  
  79. get "/" do
  80. my_nifty_helper "/ calling..."
  81. end
  82. end
  83.  
  84. class RestRoutesApp < Lego::Application
  85. controller :rest_routes
  86.  
  87. index do
  88. "this is the index calling"
  89. end
  90. end
Add Comment
Please, Sign In to add comment