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

Rack

 Rack

An Introduction to rack

Revath S Kumar

December 21, 2014
Tweet

More Decks by Revath S Kumar

Other Decks in Programming

Transcript

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

    Blog at Twitter/Github - Google+: Yeoman @keralarb @keralajs @whatznear blog.revathskumar.com @revathskumar +RevathSKumar
  2. 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
  3. 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
  4. 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
  5. 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) }
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. USING RACKUP # config.ru run Proc.new do | env |

    [200, {'Content-Type' => 'text/html'}, ['My first Rack app']] end ~$ rackup
  13. 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} }")