Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Small Ruby Webstack

on

  • 2,558 views

Short presentation showing Rack and Sinatra, for when Rails is just to much.

Short presentation showing Rack and Sinatra, for when Rails is just to much.

Statistics

Views

Total Views
2,558
Views on SlideShare
2,548
Embed Views
10

Actions

Likes
0
Downloads
13
Comments
0

1 Embed 10

http://www.slideshare.net 10

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • 9 lines of code and it works!
  • So how much code are we talking about for Ruby and Radiant <br /> Our small server is not visible <br /> Radiant&#x2019;s core => 174908 loc (usually extended with plugins and own code) <br /> Rails&#x2019;s core => 131990 loc
  • Rack, This is what almost all Ruby based web frameworks build on <br /> Basically it translates http to ruby
  • Rack, This is what almost all Ruby based web frameworks build on <br /> Basically it translates http to ruby
  • Even merb <br /> Default Merb <br /> 46595 loc
  • Apache + passenger, mongrel, thin, webrick
  • But there is still an other player out there...
  • But there is still an other player out there...
  • But there is still an other player out there...
  • 6 lines of code (and a Rack compatible server) and we are off
  • This is Rails&#x2019; Rack stack <br /> Each class in use stack is called Middleware
  • This is a piece of middelware, that doesn&#x2019;t do much
  • Rack::Lock executes the call in a synchronize block (Mutex) <br /> ActionController::Failsafe will render 500.html if it catches an exception <br /> Rails::Rack::Metal is basically a wrapper around middleware for ease of use in a rails app <br /> ... <br /> Your own... insert google analytic tags, filter out bad words...
  • again 6 lines of code (and a Rack compatible server) and we are off
  • Each page is defined by it&#x2019;s <br /> - verb (get, post, put, delete) <br /> - URI, supporting parameters, wildcards and regex <br /> - And it should (usually) respond
  • Rendering templates <br /> - erb + haml + builder from files, inline, or defined template methods <br /> - With layout files!
  • Rendering templates <br /> - erb + haml + builder from files, inline, or defined template methods <br /> - With layout files!
  • Before Filters! <br /> Passing allows for better structured code <br /> 404&#x2019;s <br /> and error handeling
  • Rack as a stack <br /> Sinatra for non-data driven apps (isdemax.be, design prototyping)

Small Ruby Webstack Small Ruby Webstack Presentation Transcript

  • Small ruby web stacks When Rails is just to big
  • Bert Goethals
  • Small ruby web stacks When Rails is just to big
  • The scale of our frameworks
  • How much does it take to have a working ruby served web page?
  • require 'socket'   server = TCPServer.new('127.0.0.1', 8080)   loop do   session = server.accept   request = session.gets   session.print "HTTP/1.1 200/OKrnServer: MakorsharnContent-type: text/ plainrnn"   session.write "Hello world"   session.close end
  • Rails Radiant
  • • The base of Rack (almost) all ruby web apps • 8439 lines of code
  • • Rails, great The base of Rack development (almost) all ruby platform web apps • 131990 lines code 8439 lines of of code
  • Merb Rails Radiant
  • And we still need a webserver
  • Radiant Rails Merb
  • Sinatra Rack
  • Sinatra Rack
  • Rack
  • class HelloWorld   def call(env)     [200, {"Content-Type" => "text/plain"}, ["Hello world!"]]   end end   run HelloWorld.new
  • Use a Rack to Stack
  • use Rack::Lock use ActionController::Failsafe use ActionController::Session::CookieStore, , {:secret=>"<secret>", :session_key=>"_<app>_session"} use Rails::Rack::Metal use ActionController::RewindableInput use ActionController::ParamsParser use Rack::MethodOverride use Rack::Head use ActiveRecord::QueryCache run ActionController::Dispatcher.new
  • class Exaple   def initialize(app)     @app = app   end     def call(env)     # edit environment     status, headers, body = @app.call(env)     # edit response     return status, headers, body   end end
  • use Rack::Lock use ActionController::Failsafe use ActionController::Session::CookieStore, , {:secret=>"<secret>", :session_key=>"_<app>_session"} use Rails::Rack::Metal use ActionController::RewindableInput use ActionController::ParamsParser use Rack::MethodOverride use Rack::Head use ActiveRecord::QueryCache run ActionController::Dispatcher.new
  • Don’t use Rack solo. ‘cause it don’t knows pages
  • Sinatra knows pages
  • require 'rubygems' require 'sinatra'   get '/' do   "Hello world!" end   run Sinatra::Application
  • verb 'uri_with/:params/and/*' do   # your logic logic   "Your response" end
  • There is more
  • get '/' do Templates   erb :index end get '/haml/:title' do   haml '%h1 #{params[:title]}' end get '/builder' do   builder :index end get '/css/screen.css' do content_type 'text/css', :charset => 'utf-8'   sass :screen end
  • Helpers helpers do   def pirate(name)     "#{name} Arrh!"   end end   get '/:name' do   pirate(params[:name]) end
  • before do   request.path_info = '/foo/bar/baz' if request.path_info = '/foo/baz/bar' end   get '/guess/:who' do   pass unless params[:who] == 'Pirate'   "You got me!" end   get '/guess/*' do   "You missed!" end   not_found do   'This is nowhere to be found' end   error do   'Sorry there was a nasty error' end
  • Ik zoek een collega! Shameless Plug
  • Conclusion and questions?