Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rack

 Rack

An Introduction to rack

Fb7edc7cc7a53c903af74d07dcecf9dc?s=128

Revath S Kumar

December 21, 2014
Tweet

Transcript

  1. INTRO TO RACK @revathskumar

  2. ABOUT Rubyist / JavaScripter Team Member / / Works at

    Blog at Twitter/Github - Google+: Yeoman @keralarb @keralajs @whatznear blog.revathskumar.com @revathskumar +RevathSKumar
  3. A MODULAR RUBY WEBSERVER INTERFACE

  4. BASIC RACK APP require 'rack' app = Proc.new do |

    env | [200, {'Content-Type' => 'text/html'}, ['My first Rack app']] end Rack::Handler::WEBrick.run app
  5. MIDDLEWARES a way to filter a request and response

  6. ANY RUBY OBJECT WHICH RESPONDS TO CALL AND RETURN ARRAY

    OF 3 ELEMENTS
  7. CALL ACCEPTS ENV

  8. BASIC MIDDLEWARE class BasicMiddleware def initialize next_in_chain @app = next_in_chain

    end def call env @app.call(env) end end
  9. MIDDLEWARE CAN CALL EACH OTHERS TO CHAIN ACTIONS

  10. SO ORDER MATTERS

  11. MIDDLEWARE CHAIN class Mware def initialize next_in_chain @app = next_in_chain

    end def call env @app.call(env) end end class Mware1 def call env [200, {'content-type' => 'text/html'}, ["Hello Mware1"]] end end app = Proc.new do | env | Mware.new(Mware1.new).call(env) end
  12. RACK::BUILDER IMPLEMENTS A SMALL DSL TO ITERATIVELY CONSTRUCT RACK APPLICATIONS.

  13. require 'rack' app = Rack::Builder.new do use Rack::CommonLogger run Proc.new

    do | env | [200, {'Content-Type' => 'text/html'}, ['Using builder']] end end Rack::Handler::WEBrick.run app
  14. app = Proc.new { | env | stack = [

    proc {|app| Mware.new app }, proc {|app| Mware1.new app } ] mware_obj = stack.reverse.inject(self){ | prev, curr| curr[prev] # curr.call(prev) } mware_obj.call(env) }
  15. ROUTES require 'rack' app = Rack::Builder.new do use Rack::CommonLogger map

    '/index' do run Proc.new do | env | [200, {'Content-Type' => 'text/html'}, ['Index']] end end run Proc.new do | env | [200, {'Content-Type' => 'text/html'}, ['Root']] end end Rack::Handler::WEBrick.run app
  16. NESTED ROUTES require 'rack' app = Rack::Builder.new do # ...

    map '/index' do # ... map '/1' do run Proc.new do | env | [200, {'Content-Type' => 'text/html'}, ['Index 1 ']] end end end # ... end Rack::Handler::WEBrick.run app
  17. VIEW class Basic def initialize app @index = File.read("index.html") end

    def call env [200, {'Content-Type' => 'text/html'}, [@index]] end end require 'rack' app = Rack::Builder.new do use Rack::CommonLogger run Basic.new end Rack::Handler::WEBrick.run app
  18. RENDERING ERB require "erb" class Basicerb attr_reader :rendered_html def initialize

    name @name = name @template = File.read('./index.erb') end def render ERB.new(@template, 0, "", "@rendered_html").result binding self end def call app [200, {'Content-Type' => 'text/html'}, [render.rendered_html]] end end
  19. ERB: LAYOUT & VIEW def initialize name @name = name

    @layout = File.read('./layout.erb') @template = File.read('./index.erb') end def render templates = [@template, @layout] @rendered_html = templates.inject(nil) do | prev, temp | _render(temp) { prev } end self end def _render temp ERB.new(temp).result( binding ) end
  20. RACK::STATIC Serve static files require 'rack' builder = Rack::Builder.new do

    use Rack::CommonLogger use Rack::Static, :urls => ['/bower_components/', '/assets/'], root: '.' end Rack::Handler::WEBrick.run builder
  21. RACK CONTRIB A collection of middlewares. rack/rack-contrib

  22. USING RACKUP # config.ru run Proc.new do | env |

    [200, {'Content-Type' => 'text/html'}, ['My first Rack app']] end ~$ rackup
  23. READ CONFIG.RU OR SUPPLIED CONFIG FILE CONVERT TO A INSTANCE

    OF RACK::BUILDER config_file = File.read(config) rack_application = eval("Rack::Builder.new { #{config_file} }")
  24. QUESTIONS? Revath S Kumar Github/Twitter: @revathskumar blog.revathskumar.com