Criando um Framework com Rack em 10 minutos

2,953 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,953
On SlideShare
0
From Embeds
0
Number of Embeds
1,634
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Criando um Framework com Rack em 10 minutos

  1. 1. class HelloWorld def call(env) [200, {"Content-Type" => "text/plain"}, ["Hello World!"]] endend
  2. 2. class UpcaseMiddleware def initialize(app) @app = app end def call(env) status, headers, response = @app.call(env) response.upcase! [status, headers, response] endend
  3. 3. #app.rurequire "framework"get "/user" do @name = "User Name" erb :viewendhome = get "/" do "Home"endrun home
  4. 4. #view.erbOlá <%= @name %>
  5. 5. #framework.rbrequire "erb"def get(action, &block) middleware = Class.new middleware.send(:define_method,initialize) do |app| @app = app end def erb(view_name) view = File.read("#{view_name.to_s}.erb") body = ERB.new(view) body.result(binding) end
  6. 6. middleware.send(:define_method, "call") do |env| request = Rack::Request.new(env) response = Rack::Response.new if request.path_info == action response.write(get_block) response.finish else @app.call(env) end end middleware.send(:define_method, "get_block", &block) use middleware use Rack::Staticend
  7. 7. 1) rackup app.ru2) Acessar localhost:9292 #=> Home3) Acessar localhost:9292/user #=> Olá User

×