Share
Embed
Transcript
24 slides
-
ABOUT Rubyist / JavaScripter Team Member / / Works at
Blog at Twitter/Github - Google+: Yeoman @keralarb @keralajs @whatznear blog.revathskumar.com @revathskumar +RevathSKumar -
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 -
ANY RUBY OBJECT WHICH RESPONDS TO CALL AND RETURN ARRAY
OF 3 ELEMENTS -
BASIC MIDDLEWARE class BasicMiddleware def initialize next_in_chain @app = next_in_chain
end def call env @app.call(env) end end -
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 -
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 -
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) } -
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 -
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 -
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 -
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 -
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 -
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 -
USING RACKUP # config.ru run Proc.new do | env |
[200, {'Content-Type' => 'text/html'}, ['My first Rack app']] end ~$ rackup -
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} }")