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

Building Web Apps with Rack and Sinatra

Tom Black
October 16, 2012

Building Web Apps with Rack and Sinatra

Project files and notes at www.blacktm.com

Tom Black

October 16, 2012
Tweet

More Decks by Tom Black

Other Decks in Programming

Transcript

  1. Objectives ✔ Describe the HTTP request process ✔ Define Rack,

    describe its purpose ✔ Build a simple Rack web app ✔ Define, build, and use middleware ✔ Define DSL (compare with “framework”) ✔ Build a simple Sinatra app
  2. Things You’ll Need ✔ gem install thin ✔ gem install

    sinatra ✔ gem install heroku ✔ Project files and notes at blacktm.com
  3. What is a Rack app? A Ruby object that responds

    to a “call” method, taking a single hash parameter, and returning an array which contains the response status code, response headers, and response body as an array of strings. “In English, please.”
  4. $ rackup config.ru Run it! Open “simple_rack” and edit the

    “config.ru” file. # config.ru - rackup file require 'rack' class MyApp def call(env) headers = { "Content-Type" => "text/html" } [200, headers, ["Hello world!"]] end end run MyApp.new
  5. Who cares? ✔ You just built a web app. Damn.

    ✔ Important in understanding (and building) web frameworks (e.g. Rails) ✔ Middleware!
  6. Let’s explore Middleware! 1. Open “rack_middleware” 2. Stop the server

    (ctrl-c) 3. Run: $ rackup config.ru 4. Open: http://0.0.0.0:9292 or: http://localhost:9292
  7. # app.rb class MyApp def call(env) puts "Hello from MyAPP"

    return [200, {}, ["Hello"]] end end No “Content-Type”
  8. class ContentType def initialize(app) @app = app end def call(env)

    status, headers, body = @app.call(env) puts "Hello from ContentType" # TODO: Add "Content-Type" to the HTTP # headers and return the response. end end Open “config.ru”
  9. Add the “Content-Type” to the headers. # TODO: Add "Content-Type"

    to the HTTP # headers and return the response. headers.merge!( "Content-Type" => "text/html" ) return [status, headers, body]
  10. class FinishSentence def initialize(app) @app = app end def call(env)

    status, headers, body = @app.call(env) puts "Hello from FinishSentence" # TODO: Add " world!" to the HTTP # body and return the response. end end
  11. Add the “ world!” to the body. # TODO: Add

    " world!" to the HTTP # body and return the response. body.push(" world!") return [status, headers, body]
  12. Use the middleware. # TODO: Tell the app to use

    the "ContentType" # and "FinishSentence" middleware. use ContentType use FinishSentence
  13. rack_middleware:$ rackup config.ru >> Thin web server (v1.5.0 codename Knife)

    >> Maximum connections set to 1024 >> Listening on 0.0.0.0:9292, CTRL+C to stop Hello from MyAPP Hello from FinishSentence Hello from ContentType 127.0.0.1 - - [14/Nov/2012 17:01:07] "GET / HT {
  14. “Sinatra is a DSL for quickly creating web applications in

    Ruby with minimal e ort.” A “human interface” to Rack. “Domain Specific Language”
  15. The Showdown Plain ‘ol Rack Sinatra get '/' do "Hello

    world!" end class MyApp def call(env) headers = { "Content-Type" => "text/html" } [200, headers, ["Hello world!"]] end end
  16. $ rackup config.ru Run it! Open “simple_sinatra” and edit the

    “config.ru” file. # config.ru require 'sinatra' get '/' do "Hello Sinatra!" end run Sinatra::Application
  17. # config.ru require 'sinatra' get '/' do "Hello Sinatra!" end

    get '/hello/:name' do |n| "Hello #{n}!" end get '/wildcard/*' do request.inspect end error 404 do "OMG, 404!" end get '/breakit' do 500 end run Sinatra::Application
  18. Rails email database ajax orm csrf routes tests sessions models

    views controllers “A full-stack web application framework.” logging tasks
  19. me